Sha256: 64ef8e1582298905333ae1f8bd57c3825022c82764073413180779fa38b0c06d

Contents?: true

Size: 1.35 KB

Versions: 6

Compression:

Stored size: 1.35 KB

Contents

class HashWithIndifferentAccess
  def initialize data=nil
    @data = {}
    merge! data if data
  end

  def merge! data
    data.each { |key, value| @data[convert_key(key)] = convert_value(value) }
  end
  alias_method :update, :merge!

  def merge data
    copy = self.class.new @data
    copy.merge! data
    copy
  end

  def [] key
    @data[convert_key(key)]
  end

  def []= key, value
    @data[convert_key(key)] = convert_value(value)
  end
  alias_method :store, :[]=

  def key? name
    @data.key? convert_key(name)
  end
  alias_method :include?, :key?
  alias_method :has_key?, :key?
  alias_method :member?, :key?

  def to_json opts=nil
    @data.to_json opts
  end

  def delete_if &block
    @data.delete_if(&block)
  end

  def delete key
    @data.delete convert_key(key)
  end

  def dig *args
    list = args.map{ |it| it.is_a?(Symbol) ? it.to_s : it }
    @data.dig *list
  end

  def pluck *args
    args = args.map(&:to_s)
    @data.select { |k,v| args.include?(k) }
  end

  def clear
    @data.keys.each { |key| @data.delete(key) }
  end

  def each;    @data.each { |k,v| yield(k,v) }; end
  def keys;    @data.keys; end
  def values;  @data.keys; end
  def to_hash; @data; end

  private

  def convert_key key
    key.kind_of?(Symbol) ? key.to_s : key
  end

  def convert_value value
    value.is_a?(Hash) ? self.class.new(value) : value
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./lib/common/hash_with_indifferent_access.rb
lux-fw-0.5.36 ./lib/common/hash_with_indifferent_access.rb
lux-fw-0.5.35 ./lib/common/hash_with_indifferent_access.rb
lux-fw-0.5.34 ./lib/common/hash_with_indifferent_access.rb
lux-fw-0.5.33 ./lib/common/hash_with_indifferent_access.rb
lux-fw-0.5.32 ./lib/common/hash_with_indifferent_access.rb