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.38.0 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.16 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.15 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.14 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.13 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.12 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.11 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.10 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.9 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.8 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.6 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.4 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.3 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.1 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.37.0 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.36.0 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.35.4 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.35.3 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.35.2 lib/rbbt/util/misc/indiferent_hash.rb
rbbt-util-5.35.1 lib/rbbt/util/misc/indiferent_hash.rb