Class: Bovem::Configuration
- Inherits:
-
Object
- Object
- Bovem::Configuration
- 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)
-
+ (Object) property(name, options = {})
Defines a new property for the configuration.
Instance Method Summary (collapse)
-
- (Configuration) initialize(file = nil, overrides = {}, logger = nil)
constructor
Creates a new configuration.
-
- (Object) parse(file = nil, overrides = {}, logger = nil)
Parses a configuration file.
Constructor Details
- (Configuration) initialize(file = nil, overrides = {}, logger = nil)
Creates a new configuration.
A configuration file is a plain Ruby file with a top-level config object.
30 31 32 |
# File 'lib/bovem/configuration.rb', line 30 def initialize(file = nil, overrides = {}, logger = nil) self.parse(file, overrides, logger) end |
Class Method Details
+ (Object) property(name, options = {})
Defines a new property for the configuration.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bovem/configuration.rb', line 81 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
- (Object) parse(file = nil, overrides = {}, logger = nil)
Parses a configuration file.
A configuration file is a plain Ruby file with a top-level config object.
Example:
config.property = "VALUE"
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 |
# File 'lib/bovem/configuration.rb', line 47 def parse(file = nil, overrides = {}, logger = nil) file = file.present? ? File.(file) : nil if file then # We have a file to parse if File.readable?(file) then begin # Open the file path = ::Pathname.new(file).realpath logger.info("Using configuration file #{path}.") if logger self.tap do |config| eval(::File.read(path)) end rescue ::Exception => e raise Bovem::Errors::InvalidConfiguration.new("Config file #{file} is not valid.") end else raise Bovem::Errors::InvalidConfiguration.new("Config file #{file} is not existing or not readable.") 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 |