Class: Inquisitive::HashWithIndifferentAccess
- Inherits:
-
Hash
- Object
- Hash
- Inquisitive::HashWithIndifferentAccess
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object (also: #store)
- #assert_valid_keys(*valid_keys) ⇒ Object
- #default(key = nil) ⇒ Object
- #delete(key) ⇒ Object
- #dup ⇒ Object
- #extractable_options? ⇒ Boolean
- #fetch(key, *extras) ⇒ Object
-
#initialize(constructor = {}, &block) ⇒ HashWithIndifferentAccess
constructor
A new instance of HashWithIndifferentAccess.
- #key?(key) ⇒ Boolean (also: #include?, #has_key?, #member?)
- #merge(hash, &block) ⇒ Object
- #nested_under_indifferent_access ⇒ Object
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
- #replace(other_hash) ⇒ Object
- #select(*args, &block) ⇒ Object
- #steal_default_from(hash) ⇒ Object
- #stringify_keys ⇒ Object
- #stringify_keys! ⇒ Object
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
- #to_hash ⇒ Object
- #to_options! ⇒ Object
- #transform_keys ⇒ Object
- #transform_keys! ⇒ Object
- #update(other_hash) ⇒ Object (also: #merge!)
- #values_at(*indices) ⇒ Object
- #with_indifferent_access ⇒ Object
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
54 55 56 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 54 def []=(key, value) regular_writer(convert_key(key), convert_value(value, for: :assignment)) end |
#assert_valid_keys(*valid_keys) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 133 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
106 107 108 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 106 def delete(key) super(convert_key(key)) end |
#dup ⇒ Object
92 93 94 95 96 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 92 def dup self.class.new(self).tap do |new_hash| new_hash.default = default end end |
#extractable_options? ⇒ Boolean
9 10 11 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 9 def true end |
#fetch(key, *extras) ⇒ Object
84 85 86 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 84 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
76 77 78 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 76 def key?(key) super(convert_key(key)) end |
#merge(hash, &block) ⇒ Object
98 99 100 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 98 def merge(hash, &block) self.dup.update(hash, &block) end |
#nested_under_indifferent_access ⇒ Object
15 16 17 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 15 def nested_under_indifferent_access self end |
#regular_update ⇒ Object
52 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 52 alias_method :regular_update, :update |
#regular_writer ⇒ Object
51 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 51 alias_method :regular_writer, :[]= |
#replace(other_hash) ⇒ Object
102 103 104 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 102 def replace(other_hash) super(self.class.new(other_hash)) end |
#select(*args, &block) ⇒ Object
144 145 146 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 144 def select(*args, &block) dup.tap {|hash| hash.select!(*args, &block)} end |
#steal_default_from(hash) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 43 def steal_default_from(hash) if hash.default_proc self.default_proc = hash.default_proc else self.default = hash.default end end |
#stringify_keys ⇒ Object
141 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 141 def stringify_keys; dup end |
#stringify_keys! ⇒ Object
140 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 140 def stringify_keys!; self end |
#symbolize_keys ⇒ Object
125 126 127 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 125 def symbolize_keys transform_keys{ |key| key.to_sym rescue key } end |
#symbolize_keys! ⇒ Object
129 130 131 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 129 def symbolize_keys! transform_keys!{ |key| key.to_sym rescue key } end |
#to_hash ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 148 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
142 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 142 def ; self end |
#transform_keys ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 110 def transform_keys result = {} each_key do |key| result[yield(key)] = self[key] end result end |
#transform_keys! ⇒ Object
118 119 120 121 122 123 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 118 def transform_keys! keys.each do |key| self[yield(key)] = delete(key) end self end |
#update(other_hash) ⇒ Object Also known as: merge!
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 60 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
88 89 90 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 88 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |
#with_indifferent_access ⇒ Object
12 13 14 |
# File 'lib/inquisitive/hash_with_indifferent_access.rb', line 12 def with_indifferent_access dup end |