Skip to content

Preserve original collection fragments for callbacks - #282

Open
OskarEichler wants to merge 3 commits into
trailblazer:masterfrom
OskarEichler:codex/collection-fragment
Open

Preserve original collection fragments for callbacks#282
OskarEichler wants to merge 3 commits into
trailblazer:masterfrom
OskarEichler:codex/collection-fragment

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

Capture the collection before processing so setters/filters see neither the previous property nor the last hash item. Restore the parent hash fragment in ensure without a context copy per item. Addresses #231.

Reproduction

require 'representable/hash'
klass=Class.new(Representable::Decorator) do
  include Representable::Hash
  property :first
  collection :items, setter: ->(fragment:, represented:, **) { represented.items=fragment }
end
p klass.new(Struct.new(:first,:items).new).from_hash('first'=>'earlier','items'=>[1,2]).items
# 'earlier' before; [1,2] after

Verification

  • Existing master suite on this isolated branch: 545 tests / 701 assertions, zero failures/errors, three existing skips on Ruby 3.2.11. Ruby 4.0.6 retains the same eight legacy inspection-format failures, zero errors and three skips. Existing tests/expectations are unchanged.
  • 200 focused checks on Ruby 3.2.11/4.0.6 for this change. XML first-render uses a separate fresh-process check.
  • Combined release-based verification with other focused fixes and attributed Use Setter in OverwriteOnNil if a :setter is defined #177/Hash value of null breaks parsing #264/Update MultiJSON usage to address deprecations #273 source backports: 544 tests / 700 assertions pass on Ruby 3.2.11, plus 4,410 focused checks and 36 boundary checks. Package retains 104 paths and unchanged compared metadata. All 36 runtime files compile on both Rubies; no newly introduced default Lint offenses.

The initial per-item dup prototype was rejected after it regressed hash rendering. The final restore-in-ensure approach preserves allocations for the 100-entry hash-collection workload (202,000 per 1,000 calls); combined-patch median time 0.021622→0.020985s. A separate hash-render workload is 0.010262→0.010458s with one extra options Hash per render from the separate caller-options fix. These are local YJIT measurements, not a production performance guarantee.

Breaking changes / limitations

Post-collection callbacks receive the original collection fragment. Item fragments still work; other existing shared hash callback state remains shared. Fragment restoration also occurs when an item raises.

No new/modified checked-in tests. Ruby 2.4–3.1, JRuby, Windows and Linux were not executed locally. Ruby >=2.4 and runtime dependency ranges remain unchanged. External test tooling supplies a legacy MiniTest alias and ruby-prof 1.7.x; no assertions were changed. No production verification is claimed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant