Module: Configurable

Defined in:
lib/configurable.rb

Overview

Include this module in a class to make it configurable, and declare allowed settings with the Macros.configurable_options method:

require 'configurable'
class Doodad
  include Configurable
  configurable_options :one, :two, :three,  # keep options in order
    :one => 1, :two => 'zwei'               # set defaults
end

This creates a ConfigStruct::Struct called Doodad::Config, along with a frozen instance of it containing the default options accessible with the Doodad::Config.defaults method (see ConfigStructDefaults).

You can then use Doodad.config (see ConfigAccessors.config) to store the configuration for your class. See ConfigStruct::Struct for the available methods. The config object is lazily created when Doodad.config is called.

Each instance of Doodad can have its own configuration object as well:

aDoodad = Doodad.new
aDoodad.config                    # => a copy of Doodad.config
aDoodad.config.one = 'eins'
aDoodad.config.one                # => 'eins'
Doodad.config.one                 # => 1

Defined Under Namespace

Modules: ConfigAccessors, ConfigStructDefaults, Macros

Constant Summary collapse

VERSION =

:nodoc:

'1.0.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



35
36
37
38
# File 'lib/configurable.rb', line 35

def self.included(klass) #:nodoc:
  klass.extend Macros
  klass.extend ConfigAccessors
end

Instance Method Details

#configObject

Returns the object’s current configuration object. This object is lazily created the first time the method is called as a copy of the class’ current config.



47
48
49
# File 'lib/configurable.rb', line 47

def config
  @_config ||= self.class.config.dup
end

#default_configObject

:nodoc:



40
41
42
# File 'lib/configurable.rb', line 40

def default_config #:nodoc:
  self.class.default_config
end