class Elastics::Struct::Hash

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/elastics/struct/hash.rb, line 6
def initialize
  super do |hash, key|
    if key[-1] == '!'
      klass = (key[0] == '_' ? Array : Hash)
      hash[clean_key(key)] = klass.new
    end
  end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/elastics/struct/hash.rb, line 37
def [](key)
  cleaned = clean_key(key)
  super has_key?(cleaned) ? cleaned : key.to_sym
end
[]=(key, val)
Alias for: store
deep_merge(*hashes) click to toggle source
# File lib/elastics/struct/hash.rb, line 42
def deep_merge(*hashes)
  dupe = deep_dup(self)
  hashes.each {|h2| dupe.replace(deep_merge_hash(dupe,h2))}
  dupe
end
deep_merge!(*hashes) click to toggle source
# File lib/elastics/struct/hash.rb, line 48
def deep_merge!(*hashes)
  replace deep_merge(*hashes)
end
fetch(key, *rest, &block) click to toggle source
Calls superclass method
# File lib/elastics/struct/hash.rb, line 32
def fetch(key, *rest, &block)
  cleaned = clean_key(key)
  super has_key?(cleaned) ? cleaned : key.to_sym, *rest, &block
end
merge(hash) click to toggle source
Calls superclass method
# File lib/elastics/struct/hash.rb, line 15
def merge(hash)
  super symbolize(hash)
end
merge!(hash) click to toggle source
Calls superclass method
# File lib/elastics/struct/hash.rb, line 19
def merge!(hash)
  super symbolize(hash)
end
store(key, val) click to toggle source
Calls superclass method
# File lib/elastics/struct/hash.rb, line 23
def store(key, val)
  if key[-1] == '='
    super key[0..-2].to_sym, val.extend(AsIs)
  else
    super clean_key(key), symbolize(val)
  end
end
Also aliased as: []=
try(key) click to toggle source
# File lib/elastics/struct/hash.rb, line 58
def try(key)
  has_key?(key) ? self[key] : nil.extend(Nil)
end
try_delete(key, *rest, &block) click to toggle source
# File lib/elastics/struct/hash.rb, line 62
def try_delete(key, *rest, &block)
  val = delete clean_key(key), *rest, &block
  val.nil? ? nil.extend(Nil) : val
end

Private Instance Methods

clean_key(key) click to toggle source
# File lib/elastics/struct/hash.rb, line 70
def clean_key(key)
  key[-1] == '!' ? key[0..-2].to_sym : key.to_sym
end
deep_dup(obj) click to toggle source
# File lib/elastics/struct/hash.rb, line 88
def deep_dup(obj)
  case obj
  when ::Hash, Elastics::Struct::Hash
    h = obj.dup
    h.each_pair do |k,v|
      h[k] = deep_dup(v)
    end
    h
  when ::Array, Elastics::Struct::Array
    obj.map{|i| deep_dup(i)}
  else
    obj
  end
end
deep_merge_hash(h1, h2) click to toggle source
# File lib/elastics/struct/hash.rb, line 74
def deep_merge_hash(h1, h2)
  h2 ||= {}
  h1.merge(h2) do |key, oldval, newval|
    case
    when oldval.is_a?(Hash) && newval.is_a?(Hash)
      deep_merge_hash(oldval, newval)
    when oldval.is_a?(Array) && newval.is_a?(Array)
      oldval | newval
    else
      newval
    end
  end
end