Guest User

Untitled

a guest
May 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. class Setting
  2. class << self
  3. private
  4. def instance
  5. @instance ||= new
  6. end
  7.  
  8. def method_missing(name, *args, &block)
  9. instance.send(name, *args, &block)
  10. end
  11. end
  12.  
  13. def initialize(name_or_hash = :application)
  14. case name_or_hash
  15. when Hash
  16. @settings = name_or_hash
  17. when String, Symbol
  18. name_or_hash = name_or_hash.to_s
  19. name_or_hash = "#{name_or_hash}.yml" unless name_or_hash =~ /\.(yml|yaml)$/i
  20. @settings = YAML.load(File.read("#{RAILS_ROOT}/config/#{name_or_hash}"))
  21. @settings = @settings[RAILS_ENV] if defined?(RAILS_ENV)
  22. end
  23. @settings = @settings.with_indifferent_access
  24. end
  25.  
  26. private
  27. def method_missing(name, *args, &block)
  28. if @settings.key?(name)
  29. case @settings[name]
  30. when Hash
  31. self.class.new(@settings[name])
  32. else
  33. @settings[name]
  34. end
  35. else
  36. raise NoMethodError.new("no configuration was specified for #{name}")
  37. end
  38. end
  39. end
Add Comment
Please, Sign In to add comment