lib/waffle/config.rb in waffle-0.3.5 vs lib/waffle/config.rb in waffle-0.4.0
- old
+ new
@@ -1,44 +1,57 @@
-require 'singleton'
require 'yaml'
module Waffle
- class Config
- include Singleton
+ module Config
+ extend self
+ attr_accessor :settings, :defaults
- attr_reader :config_hash
+ @settings = {}
+ @defaults = {}
- def initialize
- @config_hash = {'transport' => 'rabbitmq', 'url' => nil, 'encoder' => 'json'}
+ def option(name, options = {})
+ defaults[name] = settings[name] = options[:default]
- filename = "config/waffle.yml"
+ class_eval <<-RUBY
+ def #{name}
+ settings[#{name.inspect}]
+ end
- if defined? Rails
- fielname = "#{Rails.root}/config/waffle.yml"
- end
+ def #{name}=(value)
+ settings[#{name.inspect}] = value
+ end
- if File.exists?(filename)
- loaded_config = YAML.load_file filename
-
- if defined?(Rails) && loaded_config[Rails.env]
- @config_hash.merge! loaded_config[Rails.env]
- else
- @config_hash.merge! loaded_config
+ def #{name}?
+ #{name}
end
- end
+ RUBY
+ end
+ option :url, :default => nil
+ option :encoder, :default => 'json'
+ option :transport, :default => nil
+ option :connection_attempt_timeout, :default => 30
+
+ def load! options=nil
+ options[:path] ? load_from_yaml!(options[:path]) : load_from_hash!(options)
+ self
end
- class << self
+ def load_from_yaml! filename
+ filename = Rails.root.join(filename) if defined?(Rails)
+ filename = File.expand_path(filename)
- def method_missing(m, *args, &block)
- if self.instance.config_hash.has_key?(m.to_s)
- self.instance.config_hash[m.to_s]
- else
- super
- end
+ if File.exists?(filename)
+ settings_hash = YAML.load_file(filename)[environment]
+ @settings = defaults.merge(settings_hash.symbolize_keys) if settings_hash
end
-
end
+ def load_from_hash! options
+ @settings = defaults.merge(options)
+ end
+
+ def environment
+ defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : (ENV['RACK_ENV'] || 'development')
+ end
end
end