class GhostRb::Support::HashWithIndifferentAccess

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]{api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html} @author Rene Hernandez @since 0.2.3 rubocop:enable Metrics/LineLength

Public Class Methods

new(data = {}) click to toggle source
Calls superclass method
# 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

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 27
def [](key)
  super(convert_key(key))
end
[]=(key, value) click to toggle source
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 33
def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end
Also aliased as: regular_writer
default(*args) click to toggle source
Calls superclass method
# 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) click to toggle source
Calls superclass method
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 47
def delete(key)
  super(convert_key(key))
end
dup() click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 51
def fetch(key, *extras)
  super(convert_key(key), *extras)
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source
Calls superclass method
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 55
def key?(key)
  super(convert_key(key))
end
Also aliased as: has_key?, include?
merge(hash, &block) click to toggle source
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 79
def merge(hash, &block)
  dup.update(hash, &block)
end
merge!(other_hash)
Alias for: update
regular_writer(key, value)
Alias for: []=
to_hash() click to toggle source
# 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) { |convert_key(key), self, value| ... } click to toggle source
Calls superclass method
# 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
Also aliased as: merge!

Private Instance Methods

add_defaults(target) click to toggle source
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 107
def add_defaults(target) # :doc:
  if default_proc
    target.default_proc = default_proc.dup
  else
    target.default = default
  end
end
convert_key(key) click to toggle source
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 95
def convert_key(key)
  key.is_a?(Symbol) ? key.to_s : key
end
convert_value(value) click to toggle source
# File lib/ghost_rb/support/hash_with_indifferent_access.rb, line 99
def convert_value(value)
  return self.class.new(value) if value.is_a? Hash

  return value.map { |e| convert_value(e) } if value.is_a?(Array)

  value
end