Class: PagesCore::Templates::ConfigurationHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pages_core/templates/configuration_handler.rb

Direct Known Subclasses

Configuration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurationHandler

Returns a new instance of ConfigurationHandler.



19
20
21
# File 'lib/pages_core/templates/configuration_handler.rb', line 19

def initialize
  @configuration = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pages_core/templates/configuration_handler.rb', line 23

def method_missing(method_name, *args, &block)
  if self.class.handle_blocks.key?(method_name)
    proxy = PagesCore::Templates::ConfigurationProxy.new(
      self.class.handle_blocks[method_name],
      self
    )
    yield(proxy) if block
    proxy
  else
    super
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



17
18
19
# File 'lib/pages_core/templates/configuration_handler.rb', line 17

def configuration
  @configuration
end

Class Method Details

.handle(method_name, &handle_block) ⇒ Object



11
12
13
14
# File 'lib/pages_core/templates/configuration_handler.rb', line 11

def handle(method_name, &handle_block)
  @handle_blocks ||= {}
  @handle_blocks[method_name] = handle_block
end

.handle_blocksObject



7
8
9
# File 'lib/pages_core/templates/configuration_handler.rb', line 7

def handle_blocks
  @handle_blocks ||= {}
end

Instance Method Details

#get(*path) ⇒ Object



58
59
60
61
62
63
# File 'lib/pages_core/templates/configuration_handler.rb', line 58

def get(*path)
  @configuration ||= {}
  path.inject(@configuration) do |value, key|
    value.is_a?(Hash) && value&.key?(key) ? value[key] : nil
  end
end

#proxy(proxy_block = nil, &callback) ⇒ Object



36
37
38
39
40
# File 'lib/pages_core/templates/configuration_handler.rb', line 36

def proxy(proxy_block = nil, &callback)
  proxy_object = PagesCore::Templates::ConfigurationProxy.new(callback)
  proxy_block&.call(proxy_object)
  proxy_object
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/pages_core/templates/configuration_handler.rb', line 42

def respond_to_missing?(method_name)
  self.class.handle_blocks.key?(method_name)
end

#set(stack, value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pages_core/templates/configuration_handler.rb', line 46

def set(stack, value)
  @configuration ||= {}
  value = true  if value == :enabled
  value = false if value == :disabled
  stack = [stack] unless stack.is_a?(Enumerable)
  partial_hash = stack.reverse.inject(value) do |hash, key|
    { key => hash }
  end
  @configuration = @configuration.deep_merge(partial_hash)
  value
end