class Wedge::IndifferentHash
Public Class Methods
new(constructor = {}) { |self| ... }
click to toggle source
Calls superclass method
# File lib/wedge/utilis/indifferent_hash.rb, line 4 def initialize(constructor = {}, &block) if block_given? yield self elsif constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end
Public Instance Methods
==(other_hash)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 18 def ==(other_hash) to_hash == self.class.new(other_hash).to_hash end
[](key)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 33 def [](key) get_value convert_key(key) end
Also aliased as: get_value
[]=(key, value)
click to toggle source
Assigns a new value to the hash:
hash = IndifferentHash.new hash[:key] = "value"
# File lib/wedge/utilis/indifferent_hash.rb, line 27 def []=(key, value) set_value convert_key(key), navigate(value) end
convert_array_for_to_hash(value)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 142 def convert_array_for_to_hash(value) value.map { |item| convert_for_to_hash item } end
convert_for_to_hash(value)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 123 def convert_for_to_hash(value) case value when IndifferentHash convert_indifferent_hash_for_to_hash value when Array convert_array_for_to_hash value else convert_value_for_to_hash value end end
convert_indifferent_hash_for_to_hash(value)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 138 def convert_indifferent_hash_for_to_hash(value) value.to_hash end
convert_key(key)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 134 def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end
convert_value_for_to_hash(value)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 146 def convert_value_for_to_hash(value) value end
delete(key)
click to toggle source
Removes a specified key from the hash.
Calls superclass method
# File lib/wedge/utilis/indifferent_hash.rb, line 38 def delete(key) super(convert_key(key)) end
dup()
click to toggle source
Returns an exact copy of the hash.
# File lib/wedge/utilis/indifferent_hash.rb, line 43 def dup self.class.new to_hash end
fetch(key, *extras)
click to toggle source
Same as Hash#fetch
where the key passed as argument can be either a string or a symbol:
counters = IndifferentHash.new counters[:foo] = 1 counters.fetch("foo") # => 1 counters.fetch(:bar, 0) # => 0 counters.fetch(:bar) {|key| 0} # => 0 counters.fetch(:zoo) # => KeyError: key not found: "zoo"
Calls superclass method
# File lib/wedge/utilis/indifferent_hash.rb, line 58 def fetch(key, *extras) super(convert_key(key), *extras) end
key?(key)
click to toggle source
Checks the hash for a key matching the argument passed in:
hash = IndifferentHash.new hash["key"] = "value" hash.key? :key # => true hash.key? "key" # => true
Calls superclass method
# File lib/wedge/utilis/indifferent_hash.rb, line 69 def key?(key) super(convert_key(key)) end
merge(hash)
click to toggle source
Merges the instantiated and the specified hashes together, giving precedence to the values from the second hash. Does not overwrite the existing hash.
# File lib/wedge/utilis/indifferent_hash.rb, line 79 def merge(hash) self.dup.update(hash) end
respond_to?(m, include_private = false)
click to toggle source
Calls superclass method
# File lib/wedge/utilis/indifferent_hash.rb, line 83 def respond_to?(m, include_private = false) has_key?(m) || super end
to_hash()
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 87 def to_hash reduce({}) do |hash, (key, value)| hash.merge key.to_sym => convert_for_to_hash(value) end end
Also aliased as: to_h
update(other_hash)
click to toggle source
Updates the instantized hash with values from the second:
hash_1 = IndifferentHash.new hash_1[:key] = "value" hash_2 = IndifferentHash.new hash_2[:key] = "New Value!" hash_1.update(hash_2) # => {"key"=>"New Value!"}
# File lib/wedge/utilis/indifferent_hash.rb, line 104 def update(other_hash) other_hash.reduce(self) { |hash, (k, v)| hash[k] = navigate(v) ; hash } end
Also aliased as: merge!
values_at(*indices)
click to toggle source
Returns an array of the values at the specified indices:
hash = IndifferentHash.new hash[:a] = "x" hash[:b] = "y" hash.values_at("a", "b") # => ["x", "y"]
# File lib/wedge/utilis/indifferent_hash.rb, line 117 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end
Protected Instance Methods
Private Instance Methods
cache_getter!(key)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 191 def cache_getter!(key) define_singleton_method(key) { self[key] } unless respond_to? key end
get_and_cache_value(key)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 186 def get_and_cache_value(key) cache_getter! key self[key] end
method_missing(m, *args, &block)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 195 def method_missing(m, *args, &block) m = m.to_s if m.chomp!('=') && args.count == 1 set_and_cache_value(m, *args) elsif args.empty? && block_given? self.navigate_hash_from_block m, &block elsif args.empty? get_and_cache_value(m) else fail ArgumentError, "wrong number of arguments (#{args.count} for 0)" end end
set_and_cache_value(key, value)
click to toggle source
# File lib/wedge/utilis/indifferent_hash.rb, line 181 def set_and_cache_value(key, value) cache_getter! key self[key] = value end