module HashWiaModule

Public Class Methods

new(hash=nil) click to toggle source
# File lib/hash_wia/module.rb, line 2
def initialize hash=nil
  if hash
    hash.each { |k,v| self[k] = v }
  end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/hash_wia/module.rb, line 15
def [] key
  data = super key
  data = super key.to_s if data.nil?
  data = super key.to_sym if key.respond_to?(:to_sym) && data.nil?

  # if we are returning hash as a value, just include with wia methods hash
  data.extend HashWiaModule if data.is_a?(Hash)

  data
end
[]=(key, value) click to toggle source
Calls superclass method
# File lib/hash_wia/module.rb, line 26
def []= key, value
  delete key
  super key, value
end
clone() click to toggle source

true clone of the hash with 0 references to the old one

# File lib/hash_wia/module.rb, line 51
def clone
  Marshal.load(Marshal.dump(self))
end
delete(key) click to toggle source
Calls superclass method
# File lib/hash_wia/module.rb, line 31
def delete key
  self[key].tap do
    super key
    super key.to_s
    super key.to_sym if key.respond_to?(:to_sym)
  end
end
key(name=nil) click to toggle source

key is common id direct access allow direct get or fuction the same if name given

# File lib/hash_wia/module.rb, line 46
def key name=nil
  name.nil? ? self[:key] : self[name.to_s]
end
merge(hash) click to toggle source
# File lib/hash_wia/module.rb, line 55
def merge hash
  dup.tap do |h|
    hash.each { |k, v| h[k] = v }
  end
end
method_missing(name, *args, &block) click to toggle source
# File lib/hash_wia/module.rb, line 61
def method_missing name, *args, &block
  strname = name.to_s

  if strname.sub!(/\?$/, '')
    # h.foo?
    !!self[strname]
  elsif strname.sub!(/=$/, '')
    # h.foo = :bar
    self[strname.to_sym] = args.first
  else
    value = self[strname]

    if value.nil?
      if block
        # h.foo { rand }
        self[name] = block
      elsif !keys.include?(name.to_sym)
        # h.foo
        raise NoMethodError.new('%s not defined' % strname)
      else
        nil
      end
    else
      value
    end
  end
end
to_ary() click to toggle source

we never return array from hash, ruby internals

# File lib/hash_wia/module.rb, line 40
def to_ary
  nil
end