class HashWithIndifferentAccess

Public Class Methods

new(data=nil) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 2
def initialize data=nil
  @data = {}
  merge! data if data
end

Public Instance Methods

[](key) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 18
def [] key
  @data[convert_key(key)]
end
[]=(key, value) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 22
def []= key, value
  @data[convert_key(key)] = convert_value(value)
end
Also aliased as: store
clear() click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 56
def clear
  @data.keys.each { |key| @data.delete(key) }
end
delete(key) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 42
def delete key
  @data.delete convert_key(key)
end
delete_if(&block) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 38
def delete_if &block
  @data.delete_if(&block)
end
dig(*args) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 46
def dig *args
  list = args.map{ |it| it.is_a?(Symbol) ? it.to_s : it }
  @data.dig *list
end
each() { |k,v| ... } click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 60
def each;    @data.each { |k,v| yield(k,v) }; end
has_key?(name)
Alias for: key?
include?(name)
Alias for: key?
key?(name) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 27
def key? name
  @data.key? convert_key(name)
end
Also aliased as: include?, has_key?, member?
keys() click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 61
def keys;    @data.keys; end
member?(name)
Alias for: key?
merge(data) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 12
def merge data
  copy = self.class.new @data
  copy.merge! data
  copy
end
merge!(data) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 7
def merge! data
  data.each { |key, value| @data[convert_key(key)] = convert_value(value) }
end
Also aliased as: update
pluck(*args) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 51
def pluck *args
  args = args.map(&:to_s)
  @data.select { |k,v| args.include?(k) }
end
store(key, value)
Alias for: []=
to_hash() click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 63
def to_hash; @data; end
to_json(opts=nil) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 34
def to_json opts=nil
  @data.to_json opts
end
update(data)
Alias for: merge!
values() click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 62
def values;  @data.keys; end

Private Instance Methods

convert_key(key) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 67
def convert_key key
  key.kind_of?(Symbol) ? key.to_s : key
end
convert_value(value) click to toggle source
# File lib/common/hash_with_indifferent_access.rb, line 71
def convert_value value
  value.is_a?(Hash) ? self.class.new(value) : value
end