lib/glue/configuration.rb in glue-0.31.0 vs lib/glue/configuration.rb in glue-0.40.0

- old
+ new

@@ -1,11 +1,12 @@ require 'yaml' -require 'facet/kernel/constant' -require 'facet/synchash' -require 'facet/dictionary' -require 'facet/string/capitalized' +require 'facets/core/kernel/constant' +require 'facets/more/synchash' +require 'facets/more/dictionary' +require 'facets/core/string/capitalized' +require 'facets/core/class/cattr' module Glue # A Configuration holds a group of Settings organized by # Owners. An owner is a class that represents the system to be @@ -96,10 +97,55 @@ end end end class << self + + # The configuration mode, typical modes include :debug, + # :stage, :live. + # + # [:debug] + # useful when debugging, extra debug information + # is emmited, actions, templates and shaders are + # reloaded, etc. The execution speed of the application + # is impaired. + # + # [:stage] + # test the application with live parameters + # (typically on a staging server). + # + # [:live] + # use the parameters for the live (production) + # server. Optimized for speed. + # + # Tries to set the default value from the CONFIGURATION_MODE + # environment variable. + + attr_accessor :mode + + def mode + @mode || ENV.fetch('CONFIGURATION_MODE', :debug).to_sym + end + + alias kernel_load load + + # Attempt to load external configuration in Ruby or + # YAML format. The files: + # + # * conf/mode.rb + # * conf/mode.yaml + # + # are considered. + + def load(m = mode) + ruby_conf = "conf/#{m}.rb" + kernel_load(ruby_conf) if File.exist?(ruby_conf) + + # Try to configure from a yaml file. + yml_conf = "conf/#{m}.yml" + Configuration.load_yaml(yml_conf) if File.exist?(yml_conf) + end # Inject the configuration parameters provided as a hash # (dictionary, ordered) to classes to be configured. # # === Warning: Pass an ordered hash (dictionary) @@ -113,28 +159,30 @@ end end # Parse configuration parameters in yaml format. - def parse(options) + def parse_yaml(options) temp = YAML::load(options) options = Dictionary.new temp.each do |k, v| - begin - options[k.gsub(/\./, '::').to_sym] = v - rescue Object - options[k] = v - end + options[k.gsub(/\./, '::').to_sym] = v end setup(options) end + # Load an external ruby configuration file. Constant missing + # errors are not reported for extra flexibility. + + def load_ruby(filename) + end + # Load and parse an external yaml configuration file. - def load(filename) - parse(File.read(filename)) + def load_yaml(filename) + parse_yaml(File.read(filename)) end # Manually add a configuration setting. The class key can # be the actual class name constant or a symbol. If the # setting is already defined it updates it. @@ -172,11 +220,10 @@ end end alias_method :all, :settings alias_method :[], :settings - #-- # FIXME: this does not work as expected. #++ def method_missing(sym) @@ -222,7 +269,5 @@ end } end end - -# * George Moschovitis <gm@navel.gr>