lib/bovem/configuration.rb in bovem-1.2.6 vs lib/bovem/configuration.rb in bovem-2.0.0

- old
+ new

@@ -1,8 +1,8 @@ # encoding: utf-8 # -# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>. +# This file is part of the bovem gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>. # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php. # module Bovem # This class holds the configuration of an applicaton. @@ -17,10 +17,12 @@ # # # Configuration file # config.property = "VALUE" # ``` class Configuration + include Lazier::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. @@ -45,24 +47,15 @@ # @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 then # We have a file to parse + if file then 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 + read_configuration_file(file, logger) else - raise Bovem::Errors::InvalidConfiguration.new("Config file #{file} is not existing or not readable.") + raise Bovem::Errors::InvalidConfiguration.new(self.i18n.configuration.not_found(file)) end end # Apply overrides if overrides.is_a?(::Hash) then @@ -87,7 +80,25 @@ define_method("#{name}=") do |value| self.instance_variable_set("@#{name}", value) end end + + private + # Reads a configuration file. + # + # @param file [String] The file to read. + # @param logger [Logger] The logger to use for notifications. + def read_configuration_file(file, logger) + begin + # Open the file + path = ::Pathname.new(file).realpath + logger.info(self.i18n.using(path)) if logger + self.tap do |config| + eval(::File.read(path)) + end + rescue ::Exception => e + raise Bovem::Errors::InvalidConfiguration.new(self.i18n.configuration.invalid(file)) + end + end end end \ No newline at end of file