lib/baurets/optionsful/config.rb in optionsful-0.3.2 vs lib/baurets/optionsful/config.rb in optionsful-0.4.0

- old
+ new

@@ -1,54 +1,49 @@ + +require 'yaml' + module Baurets module Optionsful class Config - DEFAULT = { :link => false, :host => 'auto', :base_path => "/optionsful", :propagate => true } + DEFAULT = { :link => false, :host => 'auto', :base_path => "/api", :propagate => true } def initialize(file = nil, options = {}) - unless file.nil? - @config = load_from_file(file, get_env) - else - begin - if (defined? Rails && File.exist?(File.join(Rails.root, 'config', 'optionsful.yml'))) - envs = YAML::load_file(File.join(Rails.root, 'config', 'optionsful.yml')).symbolize_keys - @config = envs[get_env].symbolize_keys - end - rescue - end + if file.nil? + file = File.join(Rails.root, 'config', 'optionsful.yml') end - @config = DEFAULT if @config.nil? - @config + config = load_yaml(file, get_env) + config = DEFAULT if config.nil? or config.empty? + config.merge!(options) unless options.empty? + @config = config + config end def get_env - if defined? Rails - env = :test if Rails.env.test? - env = :development if Rails.env.development? - env = :production if Rails.env.production? - else - env = :development - end - env + :test if Rails.env.test? + :development if Rails.env.development? + :production if Rails.env.production? end - def load_from_file(file, environment) + def load_yaml(file, environment) config = nil - require 'yaml' if File.exist?(file) begin - envs = YAML::load_file(file).symbolize_keys - config = envs[environment].symbolize_keys - rescue => e - puts e.backtrace + envs = YAML::load_file(file) + raise "Could not parse the YAML." if (envs.empty? or (not envs.kind_of?(Hash))) + envs = envs.symbolize_keys if envs + config = envs[environment].symbolize_keys if (envs && envs[environment]) + rescue + config = nil end + config end - config - end - def method_missing(name, *args) - return @config[name.to_sym] end + def method_missing(name, *args) + return @config[name.to_sym] end + end +end end