Module: Omniconf

Defined in:
lib/omniconf.rb,
lib/omniconf/errors.rb,
lib/omniconf/version.rb,
lib/omniconf/settings.rb,
lib/omniconf/blank_state.rb,
lib/omniconf/adapters/base.rb,
lib/omniconf/adapters/yaml.rb,
lib/omniconf/configuration.rb,
lib/omniconf/adapters/read_only.rb,
lib/omniconf/adapters/active_record.rb,
lib/rails/generators/omniconf/install/install_generator.rb

Defined Under Namespace

Modules: Adapter, Generators Classes: BlankSlate, Configuration, ReadOnlyConfigurationValue, Settings, UnknownConfigurationValue

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject (readonly) Also known as: conf

global merged configuration



11
12
13
# File 'lib/omniconf.rb', line 11

def configuration
  @configuration
end

.settingsObject (readonly)

Omniconf settings



10
11
12
# File 'lib/omniconf.rb', line 10

def settings
  @settings
end

Class Method Details

.load_configuration!Object Also known as: reload_configuration!



40
41
42
43
44
45
46
47
# File 'lib/omniconf.rb', line 40

def load_configuration!
  @configuration = Omniconf::Configuration.new self
  @settings.sources.each do |source_id, params|
    params[:adapter].load_configuration!
    Omniconf.logger.info "Loaded configuration from #{source_id.inspect} source"
  end
  Omniconf.logger.info "Global configuration: #{configuration.inspect}"
end

.loggerObject



24
25
26
27
28
29
30
# File 'lib/omniconf.rb', line 24

def logger
  return @logger ||= Rails.logger if defined? Rails

  @logger ||= Logger.new(STDOUT)
  @logger.level = @settings.logger_level || Logger::INFO
  @logger
end

.merge_configuration!(source_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/omniconf.rb', line 51

def merge_configuration! source_id
  source_config = Omniconf.sources[source_id].to_hash
  Omniconf.logger.debug "Merging #{source_config.inspect} from #{source_id.inspect} source"
  global_config = Omniconf.configuration.to_hash
  global_config.recursive_merge!(source_config) do |key, old_val, new_val|
    Omniconf.logger.warn \
      "'#{key}' has been overriden with value from #{source_id.inspect} source " <<
      "(old value: #{old_val.inspect}, new value: #{new_val.inspect})"
  end
end

.set_value(key, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/omniconf.rb', line 62

def set_value(key, value)
  found = false
  @settings.sources.to_a.reverse.each do |source_id, source|
    # We try to find the original source overriding the last merged one,
    # hence we scan sources backwards
    adapter = source[:adapter]
    config = adapter.configuration.to_hash
    element = key[0..-2].inject(config) { |result, el| result[el] }
    if element and element[key.last] # we've found it in the current source
      element[key.last] = value
      adapter.set_value(key, value)
      found = true
      break
    end
  end
  raise UnknownConfigurationValue,
        "cannot set a configuration value with no parents" unless found
end

.setup {|@settings| ... } ⇒ Object

Yields:



14
15
16
17
18
19
20
21
22
# File 'lib/omniconf.rb', line 14

def setup
  @settings ||= Settings.new
  yield @settings

  register_sources

  @settings.load_configuration = true if @settings.load_configuration.nil?
  load_configuration! if @settings.load_configuration
end

.sourcesObject

Returns a mapping on source adapters



33
34
35
36
37
38
# File 'lib/omniconf.rb', line 33

def sources
  @settings.sources.to_a.inject({}) do |result, (source_id, source)|
    result[source_id] = source[:adapter].configuration
    result
  end
end