Module: Global::Base

Extended by:
Base
Included in:
Global, Base
Defined in:
lib/global/base.rb

Instance Method Summary collapse

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, directory: '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, 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

#configurationObject



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

Yields:

  • (_self)

Yield Parameters:

  • _self (Global::Base)

    the object that the method was called on



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