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.66 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.64 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.63 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.62 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.61 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.60 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.59 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.58 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.57 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.56 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.55 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.54 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.53 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.51 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.50 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.49 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.47 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.46 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.45 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.21.44 lib/rbbt/util/misc/indiferent_hash.rb