class Mu::Hash

Constants

VERSION

Attributes

value[RW]

Public Class Methods

new(value = {}) click to toggle source
# File lib/mu/hash.rb, line 9
def initialize(value = {})
  self.value = value
end

Public Instance Methods

[](key) click to toggle source
# File lib/mu/hash.rb, line 50
def [](key)
  if value.kind_of?(Array) && key.kind_of?(Numeric)
    if key >= value.length
      raise Error.new("Can't access index #{key} on a list of #{value.length} items.")
    end

    new_value = value[key]
    wrap(new_value)
  else
    self.send(key)
  end
end
each() { |wrap(item)| ... } click to toggle source
Calls superclass method
# File lib/mu/hash.rb, line 27
def each
  return super unless value.kind_of?(Array)

  value.each do |item|
    yield wrap(item)
  end
end
has_key?(key) click to toggle source
# File lib/mu/hash.rb, line 43
def has_key?(key)
  return false unless is_a_dictionary?

  stringified_key = key.to_s
  value.keys.include?(stringified_key)
end
map() { |wrap(item)| ... } click to toggle source
Calls superclass method
# File lib/mu/hash.rb, line 35
def map
  return super unless value.kind_of?(Array)

  value.map do |item|
    yield wrap(item)
  end
end
method_missing(key) click to toggle source
Calls superclass method
# File lib/mu/hash.rb, line 13
def method_missing(key)
  super unless is_a_dictionary?

  stringified_key = key.to_s
  unless value.keys.include?(stringified_key)
    raise Error.new("Key #{stringified_key} doesn't exist at the current path.")
  end

  new_value = value[stringified_key]

  # returning the wrapped sub hash to allow nested chaining
  wrap(new_value)
end
unwrap() click to toggle source
# File lib/mu/hash.rb, line 63
def unwrap
  self.value
end

Private Instance Methods

is_a_dictionary?() click to toggle source
# File lib/mu/hash.rb, line 73
def is_a_dictionary?
  return false unless value.respond_to?(:keys)
  value.keys.respond_to?(:include?)
end
wrap(item) click to toggle source
# File lib/mu/hash.rb, line 69
def wrap(item)
  self.class.new(item)
end