Class: VarBlock::VarHash
- Defined in:
- lib/var_block/var_hash.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#merge(variables) ⇒ Object
OVERRIDES Hash ‘merge`.
- #merged_with(variables = {}) ⇒ Object
- #with(variables = {}) ⇒ Object
Methods included from Support
Methods included from Globals
Class Method Details
.new_from_var_hash(var_hash: nil) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/var_block/var_hash.rb', line 9 def self.new_from_var_hash(var_hash: nil) new_var_hash = VarHash.new if var_hash raise ArgumentError.new('`instance` should be a `VarHash` object') unless var_hash.is_a? VarHash new_var_hash = new_var_hash.merge!(var_hash.to_h) end new_var_hash end |
Instance Method Details
#merge(variables) ⇒ Object
OVERRIDES Hash ‘merge`
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/var_block/var_hash.rb', line 23 def merge(variables) raise ArgumentError, '`merge` does not accept a block. Are you looking for `merged_with` instead?' if block_given? variables.each do |key, value| current_value = self[key] # if variable already has a value, we need to wrap both values into a VarArray if not yet a VarArray if self.has_key?(key) unless current_value.is_a? VarArray self[key] = VarArray.new(array_wrap(current_value) + array_wrap(value)) else self[key] = self[key].clone.concat(array_wrap(value)) end # else if new variable else self[key] = value end end end |
#merged_with(variables = {}) ⇒ Object
44 45 46 47 48 |
# File 'lib/var_block/var_hash.rb', line 44 def merged_with(variables = {}) cloned_self = self.clone cloned_self.merge(variables) cloned_self.with() { yield(cloned_self) } end |
#with(variables = {}) ⇒ Object
18 19 20 |
# File 'lib/var_block/var_hash.rb', line 18 def with(variables = {}) super(self, variables) end |