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 Attribute Summary collapse

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:



34
35
36
37
38
39
# File 'lib/bovem/configuration.rb', line 34

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

  @i18n = Bovem::I18n.new(root: "bovem.configuration", path: Bovem::Application::LOCALE_ROOT)
  parse(file, overrides, logger)
end

Instance Attribute Details

#i18nI18n (readonly)

Returns A i18n helper.

Returns:

  • (I18n)

    A i18n helper.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bovem/configuration.rb', line 24

class Configuration < Lazier::Configuration
  attr_reader :i18n
  # Creates a new configuration.
  #
  # A configuration file is a plain Ruby file with a top-level {Configuration config} object.
  #
  # @param file [String] The file to read.
  # @param overrides [Hash] A set of values which override those set in the configuration file.
  # @param logger [Logger] The logger to use for notifications.
  # @see #parse
  def initialize(file = nil, overrides = {}, logger = nil)
    super()

    @i18n = Bovem::I18n.new(root: "bovem.configuration", path: Bovem::Application::LOCALE_ROOT)
    parse(file, overrides, logger)
  end

  # Parses a configuration file.
  #
  # A configuration file is a plain Ruby file with a top-level {Configuration config} object.
  #
  # Example:
  #
  # ```ruby
  # config.property = "VALUE"
  # ```
  #
  # @param file [String] The file to read.
  # @param logger [Logger] The logger to use for notifications.
  # @param overrides [Hash] A set of values which override those set in the configuration file.
  def parse(file = nil, overrides = {}, logger = nil)
    file = file.present? ? File.expand_path(file) : nil

    if file
      raise(Bovem::Errors::InvalidConfiguration, i18n.not_found(file)) unless File.readable?(file)
      read_configuration_file(file, logger)
    end

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

    self
  end

  private

  # :nodoc:
  # rubocop:disable RescueException
  def read_configuration_file(file, logger)
    # Open the file
    path = file =~ /^#{File::SEPARATOR}/ ? file : ::Pathname.new(file).realpath
    logger.info(i18n.configuration.using(path)) if logger
    eval_file(path)
  rescue Exception
    raise(Bovem::Errors::InvalidConfiguration, i18n.invalid(file))
  end
  # rubocop:enable RescueException

  # :nodoc:
  def eval_file(path)
    # rubocop:disable UnusedBlockArgument, Eval
    tap { |config| eval(File.read(path)) }
    # rubocop:enable UnusedBlockArgument, Eval
  end
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.



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

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

  if file
    raise(Bovem::Errors::InvalidConfiguration, i18n.not_found(file)) unless File.readable?(file)
    read_configuration_file(file, logger)
  end

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

  self
end