Module: Tap::Support::Configurable

Included in:
Env, Root, Join, Task
Defined in:
lib/tap/support/configurable.rb

Overview

Configurable enables the specification of configurations within a class definition.

class ConfigClass
  include Configurable

  config :one, 'one'
  config :two, 'two'
  config :three, 'three'

  def initialize(overrides={})
    initialize_config(overrides)
  end
end

c = ConfigClass.new
c.config.class         # => InstanceConfiguration
c.config               # => {:one => 'one', :two => 'two', :three => 'three'}

The config object acts as a forwarding hash; declared configurations map to accessors while undeclared configurations are stored internally:

c.config[:one] = 'ONE'
c.one                  # => 'ONE'

c.one = 1           
c.config               # => {:one => 1, :two => 'two', :three => 'three'}

c.config[:undeclared] = 'value'
c.config.store         # => {:undeclared => 'value'}

The writer for a configuration can be defined by providing a block to config.

The Validation module provides a number of common validation/transform blocks which can be accessed through the class method ‘c’:

class SubClass < ConfigClass
  config(:one, 'one') {|v| v.upcase }
  config :two, 2, &c.integer
end

s = SubClass.new
s.config               # => {:one => 'ONE', :two => 2, :three => 'three'}

s.one = 'aNothER'             
s.one                  # => 'ANOTHER'

s.two = -2
s.two                  # => -2
s.two = "3"
s.two                  # => 3
s.two = nil            # !> ValidationError
s.two = 'str'          # !> ValidationError

As shown above, configurations are inherited from the parent and may be overridden in subclasses. See ConfigurableClass for more details.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

An InstanceConfiguration with configurations for self



69
70
71
# File 'lib/tap/support/configurable.rb', line 69

def config
  @config
end

Class Method Details

.included(mod) ⇒ Object

Extends including classes with ConfigurableClass



64
65
66
# File 'lib/tap/support/configurable.rb', line 64

def self.included(mod) # :nodoc:
  mod.extend Support::ConfigurableClass if mod.kind_of?(Class)
end

Instance Method Details

#initialize_copy(orig) ⇒ Object

Reinitializes configurations in the copy such that the new object has it’s own set of configurations, separate from the original object.



87
88
89
90
# File 'lib/tap/support/configurable.rb', line 87

def initialize_copy(orig)
  super
  initialize_config(orig.config)
end

#reconfigure(overrides = {}) ⇒ Object

Reconfigures self with the given overrides. Only the specified configs are modified. Keys are symbolized.

Returns self.



75
76
77
78
79
80
81
82
# File 'lib/tap/support/configurable.rb', line 75

def reconfigure(overrides={})
  keys = (config.class_config.ordered_keys + overrides.keys) & overrides.keys
  keys.each do |key|
    config[key.to_sym] = overrides[key] 
  end

  self
end