module Elastictastic::Util

Public Instance Methods

call_or_each(object, &block) click to toggle source
# File lib/elastictastic/util.rb, line 37
def call_or_each(object, &block)
  if Array === object then object.each(&block)
  else
    block.call(object)
    object
  end
end
call_or_map(object, &block) click to toggle source
# File lib/elastictastic/util.rb, line 45
def call_or_map(object, &block)
  if Array === object then object.map(&block)
  else block.call(object)
  end
end
deep_merge(l, r) click to toggle source
# File lib/elastictastic/util.rb, line 13
def deep_merge(l, r)
  if l.nil? then r
  elsif r.nil? then l
  elsif Hash === l && Hash === r
    {}.tap do |merged|
      (l.keys | r.keys).each do |key|
        merged[key] = deep_merge(l[key], r[key])
      end
    end
  elsif Array === l && Array === r then l + r
  elsif Array === l then l + [r]
  elsif Array === r then [l] + r
  else [l, r]
  end
end
deep_stringify(hash) click to toggle source
# File lib/elastictastic/util.rb, line 5
def deep_stringify(hash)
  {}.tap do |stringified|
    hash.each_pair do |key, value|
      stringified[key.to_s] = Hash === value ? deep_stringify(value) : value
    end
  end
end
ensure_array(object) click to toggle source
# File lib/elastictastic/util.rb, line 29
def ensure_array(object)
  case object
  when nil then []
  when Array then object
  else [object]
  end
end
unflatten_hash(hash) click to toggle source
# File lib/elastictastic/util.rb, line 51
def unflatten_hash(hash)
  {}.tap do |unflattened|
    hash.each_pair do |key, value|
      namespace = key.split('.')
      field_name = namespace.pop
      namespace.inject(unflattened) do |current, component|
        current[component] ||= {}
      end[field_name] = value
    end
  end
end