Sha256: 772bc47f8240554289b0851ab97272847e5a0a8a09a069ad4f8cdd31638b9148

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

require_relative 'indiferent_hash/options'
require_relative 'indiferent_hash/case_insensitive'

module IndiferentHash

  def self.setup(hash)
    hash.extend IndiferentHash 
    hash
  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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scout-gear-7.1.0 lib/scout/indiferent_hash.rb
scout-gear-6.0.0 lib/scout/indiferent_hash.rb
scout-gear-5.2.0 lib/scout/indiferent_hash.rb
scout-gear-5.1.1 lib/scout/indiferent_hash.rb
scout-gear-2.0.0 lib/scout/indiferent_hash.rb