Class: GhostRb::Support::HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
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

Author:

  • Rene Hernandez

Since:

  • 0.2.3

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.

Since:

  • 0.2.3



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

Since:

  • 0.2.3



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

Since:

  • 0.2.3



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

Since:

  • 0.2.3



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

Since:

  • 0.2.3



47
48
49
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 47

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

#dupObject

Since:

  • 0.2.3



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

Since:

  • 0.2.3



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?

Returns:

  • (Boolean)

Since:

  • 0.2.3



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

Since:

  • 0.2.3



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_writerObject

Since:

  • 0.2.3



31
# File 'lib/ghost_rb/support/hash_with_indifferent_access.rb', line 31

alias regular_writer []=

#to_hashObject

Since:

  • 0.2.3



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!

Since:

  • 0.2.3



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