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.20.4 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.20.3 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.20.2 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.20.1 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.20.0 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.37 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.36 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.35 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.34 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.32 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.31 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.30 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.29 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.28 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.27 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.26 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.25 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.19.24 lib/rbbt/util/misc/indiferent_hash.rb