Class: Declarative::Variables
- Inherits:
-
Object
- Object
- Declarative::Variables
- Defined in:
- lib/declarative/variables.rb
Overview
Implements the pattern of maintaining a hash of key/values (usually “defaults”) that are mutated several times by user and library code (override defaults).
The Variables instance then represents the configuration data to be processed by the using library (e.g. Representable or Trailblazer).
Defined Under Namespace
Classes: Proc
Class Method Summary collapse
- .Append(appended_array) ⇒ Object
- .Merge(merged_hash) ⇒ Object
-
.merge(defaults, overrides) ⇒ Object
Hash hash where ‘overrides` is merged onto `defaults` respecting Merge, Append etc.
Class Method Details
.Append(appended_array) ⇒ Object
32 33 34 35 36 |
# File 'lib/declarative/variables.rb', line 32 def self.Append(appended_array) Variables::Proc.new do |original| (original || []) + appended_array end end |
.Merge(merged_hash) ⇒ Object
26 27 28 29 30 |
# File 'lib/declarative/variables.rb', line 26 def self.Merge(merged_hash) Variables::Proc.new do |original| (original || {}).merge( merged_hash ) end end |
.merge(defaults, overrides) ⇒ Object
Returns Hash hash where ‘overrides` is merged onto `defaults` respecting Merge, Append etc.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/declarative/variables.rb', line 12 def self.merge(defaults, overrides) defaults = defaults.merge({}) # todo: use our DeepDup. # TODO: or how could we provide immutability? overrides.each do |k, v| if v.is_a?(Variables::Proc) defaults[k] = v.( defaults[k] ) else defaults[k] = v end end defaults end |