lib/baurets/optionsful/config.rb in optionsful-0.2.3 vs lib/baurets/optionsful/config.rb in optionsful-0.3.0
- old
+ new
@@ -1,49 +1,54 @@
module Baurets
module Optionsful
class Config
- def initialize(options = {})
- @config = configure_options(options)
- setup
- self
- end
+ DEFAULT = { :link => false, :host => 'auto', :base_path => "/optionsful", :propagate => true }
- def base_path
- @config[:http][:base_path]
+ 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
+ end
+ @config = DEFAULT if @config.nil?
+ @config
end
- private
-
- def configure_options(options = {})
- default_opts = { :http => { :base_path => "/optionsful"}, :file => "", :environment => "development" }
- conf = {}
- if defined? RAILS_ROOT
- conf = default_opts.merge!({:file => (File.join(RAILS_ROOT, 'config', 'optionsful.yml'))})
+ 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
- conf = default_opts.merge!({ :http => { :base_path => "/optionsful"} })
+ env = :development
end
- conf = conf.merge!(options) unless options.empty?
- conf
+ env
end
- def setup
- require "yaml"
- yaml_file = @config[:file]
- begin
- if File.exist? yaml_file
- conf = YAML::load_file(yaml_file)[@config[:environment]].symbolize_keys
- configure(conf) if conf
+ def load_from_file(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
end
- rescue
end
+ config
end
- def configure(conf)
- @config[:http][:base_path] = conf[:http]["base_path"] if (conf[:http] && conf[:http]["base_path"])
- @config[:file] = conf[:file] if conf[:file]
- @config[:environment] = conf[:environment] if conf[:environment]
+ def method_missing(name, *args)
+ return @config[name.to_sym]
end
end
end
-end
\ No newline at end of file
+end