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, Ruby Sets, regular expressions, Data records, and ranges 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
Deduplication follows Ruby's hash and eql? semantics. Equal values
may differ in observable details. For example, Time values representing
the same instant can have different time zones. Deduplicating these values
or containers holding them may reuse a different timezone representation.
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.
84 85 86 87 |
# File 'lib/farce/deduper.rb', line 84 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.
52 53 54 55 |
# File 'lib/farce/deduper.rb', line 52 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.
62 63 64 65 |
# File 'lib/farce/deduper.rb', line 62 def store(*modules) @store.concat(modules) self end |