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.


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

def initialize
  @configuration = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


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

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


15
16
17
# File 'lib/pages_core/templates/configuration_handler.rb', line 15

def configuration
  @configuration
end

Class Method Details

.handle(method_name, &handle_block) ⇒ Object


9
10
11
12
# File 'lib/pages_core/templates/configuration_handler.rb', line 9

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

.handle_blocksObject


5
6
7
# File 'lib/pages_core/templates/configuration_handler.rb', line 5

def handle_blocks
  @handle_blocks ||= {}
end

Instance Method Details

#get(*path) ⇒ Object


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

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

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


34
35
36
37
38
# File 'lib/pages_core/templates/configuration_handler.rb', line 34

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

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/pages_core/templates/configuration_handler.rb', line 40

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

#set(stack, value) ⇒ Object


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

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|
    Hash[key => hash]
  end
  @configuration = @configuration.deep_merge(partial_hash)
  value
end