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.89 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.87 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.86 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.85 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.84 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.83 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.81 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.80 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.78 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.77 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.76 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.75 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.74 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.73 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.72 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.71 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.70 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.69 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.68 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.67 lib/rbbt/util/misc/indiferent_hash.rb