Class: Flex::Variables
- Inherits:
-
Struct::Hash
- Object
- Hash
- Struct::Hash
- Flex::Variables
- Defined in:
- lib/flex/variables.rb,
lib/flex/deprecation.rb
Instance Method Summary collapse
- #add(*hashes) ⇒ Object
-
#fetch_nested(key) ⇒ Object
allows to fetch values for tag names like ‘a.3.c’ fetching vars[3][:c].
- #finalize ⇒ Object
-
#get_prunable(key) ⇒ Object
returns Prunable::Value if the value is in VALUES (called from stringified).
-
#initialize(*hashes) ⇒ Variables
constructor
A new instance of Variables.
-
#store_nested(key, value) ⇒ Object
allows to store keys like ‘a.3.c’ into vars[3][:c].
Methods inherited from Struct::Hash
#[], #deep_merge, #deep_merge!, #fetch, #merge, #merge!, #store, #try, #try_delete
Methods included from Struct::Symbolize
Constructor Details
#initialize(*hashes) ⇒ Variables
Returns a new instance of Variables.
4 5 6 |
# File 'lib/flex/variables.rb', line 4 def initialize(*hashes) deep_merge! super(), *hashes end |
Instance Method Details
#add(*hashes) ⇒ Object
87 88 89 90 |
# File 'lib/flex/deprecation.rb', line 87 def add(*hashes) Deprecation.warn 'Flex::Variables#add', 'Flex::Variables#deep_merge!' replace deep_merge(*hashes) end |
#fetch_nested(key) ⇒ Object
allows to fetch values for tag names like ‘a.3.c’ fetching vars[3][:c]
45 46 47 48 49 |
# File 'lib/flex/variables.rb', line 45 def fetch_nested(key) unnest(key).inject(self, :fetch) rescue NoMethodError, KeyError raise MissingVariableError, "the required #{key.inspect} variable is missing." end |
#finalize ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/flex/variables.rb', line 8 def finalize self[:index] = self[:index].uniq.join(',') if self[:index].is_a?(Array) self[:type] = self[:type].uniq.join(',') if self[:type].is_a?(Array) # so you can pass :fields => [:field_one, :field_two] self[:params!].each{|k,v| self[:params][k] = v.uniq.join(',') if v.is_a?(Array)} if self[:page] self[:page] = self[:page].to_i self[:page] = 1 unless self[:page] > 0 self[:params][:from] ||= ((self[:page] - 1) * (self[:params][:size] || 10)).ceil unless self[:page] == 1 else self[:page] = 1 end self end |
#get_prunable(key) ⇒ Object
returns Prunable::Value if the value is in VALUES (called from stringified)
24 25 26 27 28 |
# File 'lib/flex/variables.rb', line 24 def get_prunable(key) val = fetch_nested(key) return val if self[:no_pruning].include?(key) Prunable::VALUES.include?(val) ? Prunable::Value : val end |
#store_nested(key, value) ⇒ Object
allows to store keys like ‘a.3.c’ into vars[3][:c]
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flex/variables.rb', line 31 def store_nested(key, value) var = unnest(key).reverse.inject(value) do |memo,k| if k.is_a?(Symbol) {k => memo} else ar = [] ar[k] = memo ar end end deep_merge! var end |