Sha256: c54c5da5481b736823471ca348a9b18f204d167951142055fd827b361ac24766

Contents?: true

Size: 1.79 KB

Versions: 128

Compression:

Stored size: 1.79 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[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

  def clean_version
    clean = {}
    each do |k,v|
      clean[k.to_s] = v unless clean.include? k.to_s
    end
    clean
  end
end

module CaseInsensitiveHash
  

  def self.setup(hash)
    hash.extend CaseInsensitiveHash
  end

  def downcase_keys
    @downcase_keys ||= begin
                         down = {} 
                         keys.collect{|key| 
                           down[key.to_s.downcase] = key 
                         }
                         down
                       end
  end

  def [](key, *rest)
    value = super(key, *rest)
    return value unless value.nil?
    key_downcase = key.to_s.downcase
    super(downcase_keys[key_downcase])
  end

  def values_at(*keys)
    keys.collect do |key|
      self[key]
    end
  end
  
end

Version data entries

128 entries across 128 versions & 1 rubygems

Version Path
rbbt-util-5.34.27 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.26 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.25 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.24 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.23 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.22 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.21 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.20 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.18 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.17 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.16 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.15 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.14 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.13 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.12 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.11 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.10 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.9 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.8 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.34.7 lib/rbbt/util/misc/indiferent_hash.rb