Class: Configuration

Inherits:
Section show all
Defined in:
lib/madvertise/configuration.rb

Overview

The Configuration class provides a simple interface to configuration stored inside of YAML files.

Constant Summary collapse

DEFAULTS =
{
  log_backend: :stdout,
  log_caller: false,
  log_format: "%{time} %{progname}(%{pid}) [%{severity}] %{msg}\n",
  log4j_format: "%d %c(%t) [%p] %m%n",
  log_level: :info,
}

Instance Method Summary collapse

Methods inherited from Section

from_hash

Methods inherited from Hash

#deep_merge, #deep_merge!, #delta_merge!

Constructor Details

#initialize {|config| ... } ⇒ Configuration

Create a new Configuration object.

Yields:

  • (config)

    The new configuration object.



93
94
95
96
97
# File 'lib/madvertise/configuration.rb', line 93

def initialize
  @mixins = OrderedSet.new
  @callbacks = []
  mixin(DEFAULTS)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Section

Instance Method Details

#callback(&block) ⇒ void

This method returns an undefined value.

Register a callback for config mixins.



144
145
146
# File 'lib/madvertise/configuration.rb', line 144

def callback(&block)
  @callbacks << block
end

#mixin(value) ⇒ void

This method returns an undefined value.

Mixin a configuration snippet into the current section.

Parameters:

  • value (Hash, String)

    A hash to merge into the current configuration. If a string is given a filename is assumed and the given file is expected to contain a YAML hash.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/madvertise/configuration.rb', line 106

def mixin(value)
  @mixins << value

  if value.is_a?(String)
    value = YAML.load(File.read(value))
  end

  return unless value

  value = Section.from_hash(value)

  deep_merge!(value[:generic]) if value.has_key?(:generic)

  if value.has_key?(Env.to_sym)
    deep_merge!(value[Env.to_sym])
  else
    deep_merge!(value)
  end
end

#reload!void

This method returns an undefined value.

Reload all mixins.



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/madvertise/configuration.rb', line 129

def reload!
  clear

  @mixins.each do |file|
    mixin(file)
  end

  @callbacks.each do |callback|
    callback.call(self)
  end
end