Class: Lookbook::Store

Inherits:
ActiveSupport::OrderedOptions
  • Object
show all
Defined in:
lib/lookbook/support/store.rb

Overview

Generic hash-like key/value store.

Properties can be get/set using hash access syntax (‘data`) or dot-notation syntax (`data.key`).

Based on [ActiveSupport::OrderedOptions](api.rubyonrails.org/classes/ActiveSupport/OrderedOptions.html)

Instance Method Summary collapse

Constructor Details

#initialize(initial_data = nil, opts = {}) ⇒ Store

Returns a new instance of Store.



12
13
14
15
16
# File 'lib/lookbook/support/store.rb', line 12

def initialize(initial_data = nil, opts = {})
  @recursive = opts[:recursive] || false
  super()
  initial_data.to_h.each { |k, v| self[k] = v }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



26
27
28
29
30
31
# File 'lib/lookbook/support/store.rb', line 26

def method_missing(name, *args)
  if name.to_s.end_with?("=")
    args[0] = normalize_value(args[0])
  end
  super(name, *args)
end

Instance Method Details

#[]=(key, value) ⇒ Object



18
19
20
# File 'lib/lookbook/support/store.rb', line 18

def []=(key, value)
  super(key, normalize_value(value))
end

#fetch(name, *args) ⇒ Object



22
23
24
# File 'lib/lookbook/support/store.rb', line 22

def fetch(name, *args)
  super(name.to_sym, *args)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/lookbook/support/store.rb', line 33

def respond_to_missing?(name, include_private)
  true
end

#to_hObject



37
38
39
40
41
# File 'lib/lookbook/support/store.rb', line 37

def to_h
  transform_values do |value|
    value.is_a?(Store) ? value.to_h : value
  end
end

#to_hashObject



43
44
45
# File 'lib/lookbook/support/store.rb', line 43

def to_hash
  to_h
end