module Global::Base

Public Instance Methods

backend(backend, options = {}) click to toggle source

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

# File lib/global/base.rb, line 42
def backend(backend, options = {})
  @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(options)
  elsif backend.respond_to?(:load)
    @backends.push backend
  else
    raise 'Backend must be either a Global::Backend class or a symbol'
  end
end
configuration() click to toggle source
# 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| ... } click to toggle source
# File lib/global/base.rb, line 11
def configure
  yield self
end
reload!() click to toggle source
# File lib/global/base.rb, line 26
def reload!
  @configuration = nil
  configuration
end

Protected Instance Methods

camel_case(str) click to toggle source

from Bundler::Thor::Util.camel_case

# File lib/global/base.rb, line 66
def camel_case(str)
  return str if str !~ /_/ && str =~ /[A-Z]+.*/

  str.split('_').map(&:capitalize).join
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/global/base.rb, line 61
def method_missing(method, *args, &block)
  configuration.key?(method) ? configuration.get_configuration_value(method) : super
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/global/base.rb, line 57
def respond_to_missing?(method, include_private = false)
  configuration.key?(method) || super
end