Sha256: b519347dd58f978254230128c9f438852e5e80996ed2dff1718091764429dd75

Contents?: true

Size: 1.04 KB

Versions: 21

Compression:

Stored size: 1.04 KB

Contents

module IndiferentHash

  def self.setup(hash)
    hash.extend IndiferentHash 
  end

  def merge(other)
    new = self.dup
    IndiferentHash.setup(new)
    other.each do |k,value|
      new[k] = value
    end
    new
  end

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

  def _default?
    @_default ||= self.default or self.default_proc
  end

  def [](key)
    res = super(key) 
    return res unless res.nil? or (_default? and not keys.include? key)

    case key
    when Symbol, Module
      super(key.to_s)
    when String
      super(key.to_sym)
    else
      res
    end
  end

  def values_at(*key_list)
    key_list.inject([]){|acc,key| acc << self[key]}
  end

  def include?(key)
    case key
    when Symbol, Module
      super(key) || super(key.to_s)
    when String
      super(key) || super(key.to_sym)
    else
      super(key)
    end
  end

  def delete(key)
    case key
    when Symbol, Module
      super(key) || super(key.to_s)
    when String
      super(key) || super(key.to_sym)
    else
      super(key)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
rbbt-util-5.21.112 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.111 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.110 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.109 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.107 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.106 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.104 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.103 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.102 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.101 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.100 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.99 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.98 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.97 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.96 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.95 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.94 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.93 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.92 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.91 lib/rbbt/util/misc/indiferent_hash.rb