Class: GhostRb::Support::HashWithIndifferentAccess
- Inherits:
-
Hash
- Object
- Hash
- GhostRb::Support::HashWithIndifferentAccess
- Defined in:
- lib/ghost_rb/support/hash_with_indifferent_access.rb
Overview
rubocop:disable Metrics/LineLength Provides indifferent access for symbol and string keys. Both :bar and “bar” are considered to be the same key. This is implementation is heavily based on the [ActiveSupport implementation]http://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html rubocop:enable Metrics/LineLength
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #default(*args) ⇒ Object
- #delete(key) ⇒ Object
- #dup ⇒ Object
- #fetch(key, *extras) ⇒ Object
-
#initialize(data = {}) ⇒ HashWithIndifferentAccess
constructor
A new instance of HashWithIndifferentAccess.
- #key?(key) ⇒ Boolean (also: #has_key?, #include?)
- #merge(hash, &block) ⇒ Object
- #regular_writer ⇒ Object
- #to_hash ⇒ Object
- #update(other_hash) ⇒ Object (also: #merge!)
Constructor Details
#initialize(data = {}) ⇒ HashWithIndifferentAccess
Returns a new instance of HashWithIndifferentAccess.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 14 def initialize(data = {}) if data.respond_to?(:to_hash) super() update(data) hash = data.to_hash self.default = hash.default if hash.default self.default_proc = hash.default_proc if hash.default_proc else super(data) end end |
Instance Method Details
#[](key) ⇒ Object
27 28 29 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 27 def [](key) super(convert_key(key)) end |
#[]=(key, value) ⇒ Object
33 34 35 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 33 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(*args) ⇒ Object
43 44 45 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 43 def default(*args) super(*args.map { |arg| convert_key(arg) }) end |
#delete(key) ⇒ Object
47 48 49 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 47 def delete(key) super(convert_key(key)) end |
#dup ⇒ Object
37 38 39 40 41 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 37 def dup self.class.new(self).tap do |new_hash| add_defaults(new_hash) end end |
#fetch(key, *extras) ⇒ Object
51 52 53 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 51 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: has_key?, include?
55 56 57 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 55 def key?(key) super(convert_key(key)) end |
#merge(hash, &block) ⇒ Object
79 80 81 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 79 def merge(hash, &block) dup.update(hash, &block) end |
#regular_writer ⇒ Object
31 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 31 alias regular_writer []= |
#to_hash ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 83 def to_hash new_hash = {} add_defaults(new_hash) each do |key, value| new_hash[key] = convert_value(value) end new_hash end |
#update(other_hash) ⇒ Object Also known as: merge!
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 63 def update(other_hash) if other_hash.is_a? HashWithIndifferentAccess super(other_hash) else other_hash.to_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 |