Class: Bovem::Configuration
- Inherits:
-
Object
- Object
- Bovem::Configuration
- Includes:
- Lazier::I18n
- Defined in:
- lib/bovem/configuration.rb
Overview
This class holds the configuration of an applicaton.
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"
Class Method Summary collapse
-
.property(name, options = {}) ⇒ Object
Defines a new property for the configuration.
Instance Method Summary collapse
-
#initialize(file = nil, overrides = {}, logger = nil) ⇒ Configuration
constructor
Creates a new configuration.
-
#parse(file = nil, overrides = {}, logger = nil) ⇒ Object
Parses a configuration file.
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.
32 33 34 35 |
# File 'lib/bovem/configuration.rb', line 32 def initialize(file = nil, overrides = {}, logger = nil) self.i18n_setup(:bovem, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")) self.parse(file, overrides, logger) end |
Class Method Details
.property(name, options = {}) ⇒ Object
Defines a new property for the configuration.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bovem/configuration.rb', line 75 def self.property(name, = {}) = {} if !.is_a?(::Hash) define_method(name.to_s) do self.instance_variable_get("@#{name}") || [:default] end define_method("#{name}=") do |value| self.instance_variable_set("@#{name}", value) 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"
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bovem/configuration.rb', line 50 def parse(file = nil, overrides = {}, logger = nil) file = file.present? ? File.(file) : nil if file then if File.readable?(file) then read_configuration_file(file, logger) else raise Bovem::Errors::InvalidConfiguration.new(self.i18n.configuration.not_found(file)) end end # Apply overrides if overrides.is_a?(::Hash) then overrides.each_pair do |k, v| self.send("#{k}=", v) if self.respond_to?("#{k}=") end end self end |