Class: VarBlock::VarHash
- Defined in:
- lib/var_block/var_hash.rb
Instance Method Summary collapse
-
#initialize(var_hash: nil) ⇒ VarHash
constructor
A new instance of VarHash.
- #merge(variables) ⇒ Object
- #merged_with(variables = {}) ⇒ Object
- #with(variables = {}) ⇒ Object
Methods included from Support
Methods included from Globals
Constructor Details
#initialize(var_hash: nil) ⇒ VarHash
Returns a new instance of VarHash.
9 10 11 12 13 14 15 |
# File 'lib/var_block/var_hash.rb', line 9 def initialize(var_hash: nil) if var_hash raise ArgumentError.new('`instance` should be a `VarHash` object') unless var_hash.is_a? VarHash self.merge!(var_hash) end self end |
Instance Method Details
#merge(variables) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/var_block/var_hash.rb', line 21 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)) unless current_value.is_a? VarArray else self[key].concat(array_wrap(value)) end # else if new variable else self[key] = value end end end |
#merged_with(variables = {}) ⇒ Object
42 43 44 45 46 |
# File 'lib/var_block/var_hash.rb', line 42 def merged_with(variables = {}) cloned_self = self.clone cloned_self.merge(variables) cloned_self.with() { yield(cloned_self) } end |
#with(variables = {}) ⇒ Object
17 18 19 |
# File 'lib/var_block/var_hash.rb', line 17 def with(variables = {}) super(self, variables) end |