class NavigableHash

Public Class Methods

new(constructor = {}) { |self| ... } click to toggle source
Calls superclass method
# File lib/navigable_hash.rb, line 3
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/navigable_hash.rb, line 17
def ==(other_hash)
  to_hash == self.class.new(other_hash).to_hash
end
[](key) click to toggle source
# File lib/navigable_hash.rb, line 32
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 = NavigableHash.new
hash[:key] = "value"
# File lib/navigable_hash.rb, line 26
def []=(key, value)
  set_value convert_key(key), navigate(value)
end
Also aliased as: set_value, store
convert_array_for_to_hash(value) click to toggle source
# File lib/navigable_hash.rb, line 140
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/navigable_hash.rb, line 121
def convert_for_to_hash(value)
  case value
  when NavigableHash
    convert_navigable_hash_for_to_hash value
  when Array
    convert_array_for_to_hash value
  else
    convert_value_for_to_hash value
  end
end
convert_key(key) click to toggle source
# File lib/navigable_hash.rb, line 132
def convert_key(key)
  key.kind_of?(Symbol) ? key.to_s : key
end
convert_navigable_hash_for_to_hash(value) click to toggle source
# File lib/navigable_hash.rb, line 136
def convert_navigable_hash_for_to_hash(value)
  value.to_hash
end
convert_value_for_to_hash(value) click to toggle source
# File lib/navigable_hash.rb, line 144
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/navigable_hash.rb, line 37
def delete(key)
  super(convert_key(key))
end
dup() click to toggle source

Returns an exact copy of the hash.

# File lib/navigable_hash.rb, line 42
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 = HashWithIndifferentAccess.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/navigable_hash.rb, line 57
def fetch(key, *extras)
  super(convert_key(key), *extras)
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source

Checks the hash for a key matching the argument passed in:

hash = HashWithIndifferentAccess.new
hash["key"] = "value"
hash.key? :key  # => true
hash.key? "key" # => true
Calls superclass method
# File lib/navigable_hash.rb, line 68
def key?(key)
  super(convert_key(key))
end
Also aliased as: include?, has_key?, member?
member?(key)
Alias for: key?
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/navigable_hash.rb, line 78
def merge(hash)
  self.dup.update(hash)
end
merge!(other_hash)
Alias for: update
navigate(value) click to toggle source
navigate_array(value) click to toggle source
navigate_hash(value) click to toggle source
navigate_hash_from_block(key, &block) click to toggle source
navigate_value(value) click to toggle source
respond_to?(m, include_private = false) click to toggle source
Calls superclass method
# File lib/navigable_hash.rb, line 82
def respond_to?(m, include_private = false)
  has_key?(m) || super
end
store(key, value)
Alias for: []=
to_hash() click to toggle source
# File lib/navigable_hash.rb, line 86
def to_hash
  reduce({}) do |hash, (key, value)|
    hash.merge key => convert_for_to_hash(value)
  end
end
update(other_hash) click to toggle source

Updates the instantized hash with values from the second:

hash_1 = NavigableHash.new
hash_1[:key] = "value"

hash_2 = NavigableHash.new
hash_2[:key] = "New Value!"

hash_1.update(hash_2) # => {"key"=>"New Value!"}
# File lib/navigable_hash.rb, line 102
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 = HashWithIndifferentAccess.new
hash[:a] = "x"
hash[:b] = "y"
hash.values_at("a", "b") # => ["x", "y"]
# File lib/navigable_hash.rb, line 115
def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end

Protected Instance Methods

get_value(key)
Alias for: []
set_value(key, value)
Alias for: []=

Private Instance Methods

cache_getter!(key) click to toggle source
# File lib/navigable_hash.rb, line 189
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/navigable_hash.rb, line 184
def get_and_cache_value(key)
  cache_getter! key
  self[key]
end
method_missing(m, *args, &block) click to toggle source
# File lib/navigable_hash.rb, line 193
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/navigable_hash.rb, line 179
def set_and_cache_value(key, value)
  cache_getter! key
  self[key] = value
end