Class: Inquisitive::HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/inquisitive/hash_with_indifferent_access.rb

Overview

It lacks all ‘deep_` transforms since that requires patching Object and Array.

Direct Known Subclasses

Hash

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}, &block) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 23

def initialize(constructor = {}, &block)
  if constructor.is_a?(::Hash)
    super()
    steal_default_from(constructor)
    update(constructor)
  else
    super(constructor)
  end.tap do |hash|
    self.default_proc = block if block_given?
  end
end

Class Method Details

.[](*args) ⇒ Object



19
20
21
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 19

def self.[](*args)
  new.merge!(::Hash[*args])
end

Instance Method Details

#[]=(key, value) ⇒ Object Also known as: store



46
47
48
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 46

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value, for: :assignment))
end

#assert_valid_keys(*valid_keys) ⇒ Object



130
131
132
133
134
135
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 130

def assert_valid_keys(*valid_keys)
  valid_string_keys = valid_keys.flatten.map(&:to_s).uniq
  each_key do |k|
    raise ArgumentError.new("Unknown key: #{k}") unless valid_string_keys.include?(k)
  end
end

#default(key = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 35

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#delete(key) ⇒ Object



103
104
105
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 103

def delete(key)
  super(convert_key(key))
end

#dupObject



89
90
91
92
93
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 89

def dup
  self.class.new(self).tap do |new_hash|
    new_hash.default = default
  end
end

#extractable_options?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 9

def extractable_options?
  true
end

#fetch(key, *extras) ⇒ Object



76
77
78
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 76

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


68
69
70
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 68

def key?(key)
  super(convert_key(key))
end

#merge(hash, &block) ⇒ Object



95
96
97
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 95

def merge(hash, &block)
  self.dup.update(hash, &block)
end

#nested_under_indifferent_accessObject



15
16
17
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 15

def nested_under_indifferent_access
  self
end

#regular_updateObject



44
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 44

alias_method :regular_update, :update

#regular_writerObject



43
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 43

alias_method :regular_writer, :[]=

#reject(*args, &block) ⇒ Object

not sure why just this one Enumerable method fails tests without override



81
82
83
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 81

def reject(*args, &block)
  self.class[super]
end

#replace(other_hash) ⇒ Object



99
100
101
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 99

def replace(other_hash)
  super(self.class.new(other_hash))
end

#select(*args, &block) ⇒ Object



141
142
143
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 141

def select(*args, &block)
  dup.tap {|hash| hash.select!(*args, &block)}
end

#stringify_keysObject



138
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 138

def stringify_keys; dup end

#stringify_keys!Object



137
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 137

def stringify_keys!; self end

#symbolize_keysObject



122
123
124
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 122

def symbolize_keys
  transform_keys{ |key| key.to_sym rescue key }
end

#symbolize_keys!Object



126
127
128
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 126

def symbolize_keys!
  transform_keys!{ |key| key.to_sym rescue key }
end

#to_hashObject



145
146
147
148
149
150
151
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 145

def to_hash
  _new_hash= {}
  each do |key, value|
    _new_hash[convert_key(key)] = convert_value(value, for: :to_hash)
  end
  ::Hash.new(default).merge!(_new_hash)
end

#to_options!Object



139
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 139

def to_options!; self end

#transform_keysObject



107
108
109
110
111
112
113
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 107

def transform_keys
  result = {}
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end

#transform_keys!Object



115
116
117
118
119
120
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 115

def transform_keys!
  keys.each do |key|
    self[yield(key)] = delete(key)
  end
  self
end

#update(other_hash) ⇒ Object Also known as: merge!



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 52

def update(other_hash)
  if other_hash.is_a? self.class
    super(other_hash)
  else
    other_hash.each_pair do |key, value|
      if block_given? && key?(key)
        value = yield(convert_key(key), self[key], value)
      end
      regular_writer(convert_key(key), convert_value(value))
    end
    self
  end
end

#values_at(*indices) ⇒ Object



85
86
87
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 85

def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end

#with_indifferent_accessObject



12
13
14
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 12

def with_indifferent_access
  dup
end