Sha256: 5588bab73ab5d865b5a1e6a3b785072e30822b2ac7629a56d9796a24244654ac

Contents?: true

Size: 1.06 KB

Versions: 118

Compression:

Stored size: 1.06 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.delete k
      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

118 entries across 118 versions & 1 rubygems

Version Path
rbbt-util-5.21.21 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.20 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.19 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.17 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.16 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.14 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.13 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.12 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.11 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.9 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.8 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.7 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.6 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.5 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.4 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.3 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.2 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.1 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.0 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.20.26 lib/rbbt/util/misc/indiferent_hash.rb