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.43 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.42 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.39 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.38 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.37 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.36 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.35 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.34 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.33 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.32 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.31 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.30 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.29 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.28 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.27 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.26 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.25 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.24 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.23 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.22 lib/rbbt/util/misc/indiferent_hash.rb