Class: TagOptions::Hash

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Includes:
ActiveSupport::Callbacks, ConvertKey
Defined in:
lib/tag_options/hash.rb

Instance Method Summary collapse

Methods included from ConvertKey

#convert_key

Constructor Details

#initialize(hash = {}) ⇒ Hash

Returns a new instance of Hash.



14
15
16
17
18
19
20
# File 'lib/tag_options/hash.rb', line 14

def initialize(hash = {})
  run_callbacks :initialize do
    hash.each do |key, value|
      self[convert_key(key)] = value.is_a?(::Hash) ? self.class.new(value) : value
    end
  end
end

Instance Method Details

#at(key, *nested_keys, as: :default) ⇒ Object



22
23
24
# File 'lib/tag_options/hash.rb', line 22

def at(key, *nested_keys, as: :default)
  TagOptions::HashAt.new(opt_hash: self, keys: [key, *nested_keys], as: as)
end

#dig(*keys) ⇒ Object



26
27
28
29
# File 'lib/tag_options/hash.rb', line 26

def dig(*keys)
  keys = keys.map { |key| convert_key(key) }
  keys.size.zero? ? self : super(*keys)
end

#populate!(*keys) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tag_options/hash.rb', line 31

def populate!(*keys)
  populated_keys = []
  data = self
  keys.each do |key|
    data[convert_key(key)] ||= self.class.new
    data = data[convert_key(key)]
    unless data.is_a?(self.class)
      raise TagOptions::Errors::NotHashError.new(populated_keys, type: data.class)
    end
  end
  self
end