Class: Petra::Configuration::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/petra/configuration/configurator.rb

Direct Known Subclasses

ClassConfigurator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurator

Returns a new instance of Configurator.



38
39
40
# File 'lib/petra/configuration/configurator.rb', line 38

def initialize
  @options = Petra.configuration.__configuration_hash(*__namespaces).deep_dup
end

Class Method Details

.base_config(name, accept_value: true) ⇒ Object

Generates a very basic configuration method which accepts a block or input value (see options)

Parameters:

  • name (Object)

    The configuration’s name which will become the method name

  • accept_value (Boolean) (defaults to: true)

    If set to true, the resulting method accepts not only a block, but also a direct value. If both, a value and a block are given, the block takes precedence



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/petra/configuration/configurator.rb', line 19

def self.base_config(name, accept_value: true)
  if accept_value
    define_method name do |value = nil, &proc|
      if proc
        __configuration[name.to_sym] = proc
      elsif !value.nil?
        __configuration[name.to_sym] = value
      else
        fail ArgumentError, 'Either a value or a configuration block have to be given.'
      end
    end
  else
    define_method name do |&proc|
      fail(ArgumentError, 'A configuration block has to be given.') unless proc
      __configuration[name.to_sym] = proc
    end
  end
end

Instance Method Details

#__persist!Object

Persists the new configuration values in the global configuration, meaning that it merges its options into the specific configuration hash under a certain key



47
48
49
# File 'lib/petra/configuration/configurator.rb', line 47

def __persist!
  Petra.configuration.__configuration_hash(*__namespaces).deep_merge!(__configuration)
end