Module: Global::Base
Instance Method Summary collapse
-
#backend(backend, options = {}) ⇒ Object
Add a backend to load configuration from.
- #configuration ⇒ Object
- #configure {|_self| ... } ⇒ Object
- #reload! ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (protected)
61 62 63 |
# File 'lib/global/base.rb', line 61 def method_missing(method, *args, &block) configuration.key?(method) ? configuration.get_configuration_value(method) : super end |
Instance Method Details
#backend(backend, options = {}) ⇒ Object
Add a backend to load configuration from.
You can define several backends; they will all be loaded and the configuration hashes will be merged.
Configure with either:
Global.backend :filesystem, path: 'config', environment: Rails.env
or:
Global.backend YourConfigurationBackend.new
backend configuration classes MUST have a ‘load` method that returns a configuration Hash
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/global/base.rb', line 42 def backend(backend, = {}) @backends ||= [] if backend.is_a?(Symbol) require "global/backend/#{backend}" backend_class = Global::Backend.const_get(camel_case(backend.to_s)) @backends.push backend_class.new() elsif backend.respond_to?(:load) @backends.push backend else raise 'Backend must be either a Global::Backend class or a symbol' end end |
#configuration ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/global/base.rb', line 15 def configuration raise 'Backend must be defined' unless @backends @configuration ||= begin configuration_hash = @backends.reduce({}) do |configuration, backend| configuration.deep_merge(backend.load.with_indifferent_access) end Configuration.new(configuration_hash) end end |
#configure {|_self| ... } ⇒ Object
11 12 13 |
# File 'lib/global/base.rb', line 11 def configure yield self end |
#reload! ⇒ Object
26 27 28 29 |
# File 'lib/global/base.rb', line 26 def reload! @configuration = nil configuration end |