Module: Configurable::Macros

Defined in:
lib/configurable.rb

Overview

Setup macros for classes that include Configurable.

Instance Method Summary collapse

Instance Method Details

#configurable_options(*args) ⇒ Object

Declares the allowed configuration settings and (optionally) their default values. Field names should be passed as symbols. Example:

configurable_options :one, :two

This creates a new ConfigStruct::Struct (which is a subclass of Ruby’s built-in Struct) called Config within the class it’s called on. (Thus if you call it within a class called Doodad, the configuration struct will be created as Doodad::Config.)

You may also declare options with default settings, which will then be available thru the Config::defaults method on the generated struct (e.g. Doodad::Config.defaults; see documentation on ConfigStructDefaults). To declare defaults, pass them as hash parameters:

configurable_options :one => 1, :two => 2

In Ruby 1.8, this will not preserve the order of the options. To define the options in a specific order and declare defaults for them, declare the field names first and then the defaults:

configurable_options :one, :two, :one => 1

Fields not listed in order but that are passed as hash params will be added after all the ordered parameters, in unspecified order:

configurable_options :one, :two, :one => 1, :five => '3 sir'
Config.members # => ["one", "two", "five"]


85
86
87
88
89
90
# File 'lib/configurable.rb', line 85

def configurable_options(*args)
  @_config_struct, @_default_config =
    create_struct(self, 'Config', *args)
  @_config_struct.extend ConfigStructDefaults
  @_config_struct.defaults = @_default_config
end