Class: Bovem::Configuration

Inherits:
Lazier::Configuration
  • Object
show all
Defined in:
lib/bovem/configuration.rb

Overview

This class holds the configuration of an application.

Extend this class and add valid properties via property method. Example:

class MyConfiguration << Bovem::Configuration
  property :property, default: "VALUE"
end

# Configuration file
config.property = "VALUE"

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, overrides = {}, logger = nil) ⇒ Configuration

Creates a new configuration.

A configuration file is a plain Ruby file with a top-level config object.

Parameters:

  • file (String) (defaults to: nil)

    The file to read.

  • overrides (Hash) (defaults to: {})

    A set of values which override those set in the configuration file.

  • logger (Logger) (defaults to: nil)

    The logger to use for notifications.

See Also:



30
31
32
33
34
35
# File 'lib/bovem/configuration.rb', line 30

def initialize(file = nil, overrides = {}, logger = nil)
  super()

  i18n_setup(:bovem, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
  parse(file, overrides, logger)
end

Instance Method Details

#parse(file = nil, overrides = {}, logger = nil) ⇒ Object

Parses a configuration file.

A configuration file is a plain Ruby file with a top-level config object.

Example:

config.property = "VALUE"

Parameters:

  • file (String) (defaults to: nil)

    The file to read.

  • logger (Logger) (defaults to: nil)

    The logger to use for notifications.

  • overrides (Hash) (defaults to: {})

    A set of values which override those set in the configuration file.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bovem/configuration.rb', line 50

def parse(file = nil, overrides = {}, logger = nil)
  file = file.present? ? File.expand_path(file) : nil

  if file then
    if File.readable?(file) then
      read_configuration_file(file, logger)
    else
      raise Bovem::Errors::InvalidConfiguration.new(i18n.configuration.not_found(file))
    end
  end

  # Apply overrides
  overrides.each_pair { |k, v| send("#{k}=", v) if self.respond_to?("#{k}=") } if overrides.is_a?(::Hash)

  self
end