Module: Configurations::Configurable

Extended by:
Configurable
Included in:
Configurations, Configurable
Defined in:
lib/configurations/configurable.rb

Overview

Module configurable provides the API of configurations

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

Once included, Configurations installs three methods in the host module: configure, configuration_defaults and configurable



10
11
12
13
14
15
# File 'lib/configurations/configurable.rb', line 10

def included(base)
  install_configure_in(base)
  base.class_eval do
    extend ClassMethods
  end
end

#install_configure_in(base) ⇒ Object

Installs #configure in base, and makes sure that it will instantiate configuration as a subclass of the host module



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/configurations/configurable.rb', line 20

def install_configure_in(base)
  base.class_eval <<-EOF
    # The central configure method
    # @params [Proc] block the block to configure host module with
    # @raise [ArgumentError] error when not given a block
    # @example Configure a configuration
    #   MyGem.configure do |c|
    #     c.foo = :bar
    #   end
    #
    def self.configure(&block)
      fail ArgumentError, "configure needs a block" unless block_given?
      @configuration = #{base.name}.const_get(configuration_type).new(
                                                      configuration_options,
                                                      &block
                                                    )
    end
  EOF
end