Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Setting
- class << self
- private
- def instance
- @instance ||= new
- end
- def method_missing(name, *args, &block)
- instance.send(name, *args, &block)
- end
- end
- def initialize(name_or_hash = :application)
- case name_or_hash
- when Hash
- @settings = name_or_hash
- when String, Symbol
- name_or_hash = name_or_hash.to_s
- name_or_hash = "#{name_or_hash}.yml" unless name_or_hash =~ /\.(yml|yaml)$/i
- @settings = YAML.load(File.read("#{RAILS_ROOT}/config/#{name_or_hash}"))
- @settings = @settings[RAILS_ENV] if defined?(RAILS_ENV)
- end
- @settings = @settings.with_indifferent_access
- end
- private
- def method_missing(name, *args, &block)
- if @settings.key?(name)
- case @settings[name]
- when Hash
- self.class.new(@settings[name])
- else
- @settings[name]
- end
- else
- raise NoMethodError.new("no configuration was specified for #{name}")
- end
- end
- end
Add Comment
Please, Sign In to add comment