Class: Elastics::Variables
- Inherits:
-
Struct::Hash
- Object
- Hash
- Struct::Hash
- Elastics::Variables
- Defined in:
- lib/elastics/variables.rb
Instance Method Summary collapse
- #fetch_nested(key) ⇒ Object
-
#fetch_value(key) ⇒ Object
returns Prunable::Value if the value is in VALUES (called from stringified).
- #finalize ⇒ Object
-
#initialize(*hashes) ⇒ Variables
constructor
A new instance of Variables.
- #store_nested(key, value) ⇒ Object
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/elastics/variables.rb', line 4 def initialize(*hashes) deep_merge! super(), *hashes end |
Instance Method Details
#fetch_nested(key) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/elastics/variables.rb', line 45 def fetch_nested(key) unnest(key).inject(self, :fetch) rescue NoMethodError, KeyError # return the milliseconds for :now if it is not set explicitly return (Time.now.to_f * 1000).to_i if key == :now raise MissingVariableError, "the required #{key.inspect} variable is missing." end |
#fetch_value(key) ⇒ Object
returns Prunable::Value if the value is in VALUES (called from stringified)
24 25 26 27 28 |
# File 'lib/elastics/variables.rb', line 24 def fetch_value(key) val = fetch_nested(key) return val if self[:no_pruning].include?(key) Prunable::VALUES.include?(val) ? Prunable::Value : val end |
#finalize ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/elastics/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 |
#store_nested(key, value) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/elastics/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 |