Class: Farce::Deduper
- Inherits:
-
Object
- Object
- Farce::Deduper
- Includes:
- Shareable::Immutable
- Defined in:
- lib/farce/deduper.rb
Overview
Reuse equal frozen values throughout object graphs. Plain strings use Ruby's string deduplication. Arrays, hashes, and Ruby Sets are frozen and cached by default. Other objects are traversed but are not cached unless their class matches #store. Modules and classes are skipped by default.
Cached objects are held weakly. Keep a reference to a returned object if its identity matters. Shareable results can be reused across Ractors. Other results are cached within the current Ractor.
Cyclic containers are frozen after their references are connected and are not cached. This keeps canonical replacements from breaking back references.
Instance Method Summary collapse
-
#dedup(object, copy: false, skip: nil) ⇒ Object
Replace equal values with cached instances, including nested keys and values.
-
#skip(*modules) ⇒ self
Exclude matching objects and their children from subsequent calls.
-
#store(*modules) ⇒ self
Also freeze and cache objects matching these classes or modules.
Methods included from Shareable
Instance Method Details
#dedup(object, copy: false, skip: nil) ⇒ Object
Replace equal values with cached instances, including nested keys and values. By default, mutable containers are updated and cached containers are frozen. Use copy: true to preserve the input. Skipped objects are always returned unchanged. Strings are deduplicated before skip rules are checked. Noncopyable coordination objects follow Walker.modify's in-place behavior.
76 77 78 79 |
# File 'lib/farce/deduper.rb', line 76 def dedup(object, copy: false, skip: nil) skip = skip ? [*@skip, *skip] : @skip.to_a dedup!(object, skip, @store.to_a, copy) end |
#skip(*modules) ⇒ self
Exclude matching objects and their children from subsequent calls.
51 52 53 54 |
# File 'lib/farce/deduper.rb', line 51 def skip(*modules) @skip.concat(modules) self end |
#store(*modules) ⇒ self
Also freeze and cache objects matching these classes or modules. Define consistent hash, eql?, and == methods for custom value classes. These methods must remain stable after deduplication.
61 62 63 64 |
# File 'lib/farce/deduper.rb', line 61 def store(*modules) @store.concat(modules) self end |