Sha256: e2403cdd6ee3ed98b4a9045432054333bf2ddfedde890b9191bdd9b857fdfb25

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module HashWiaModule
  def initialize hash=nil
    if hash
      hash.each { |k,v| self[k] = v }
    end
  end

  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

  def []= key, value
    delete key
    super key, value
  end

  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

  # we never return array from hash, ruby internals
  def to_ary
    nil
  end

  # key is common id direct access
  # allow direct get or fuction the same if name given
  def key name=nil
    name ? self[name] : self[:key]
  end

  # true clone of the hash with 0 references to the old one
  def clone
    Marshal.load(Marshal.dump(self))
  end

  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
        else
          # h.foo
          raise ArgumentError.new('%s not defined' % strname)
        end
      else
        value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_wia-0.7.1 ./lib/hash_wia/module.rb