Module: Configurable

Extended by:
ModuleMethods
Defined in:
lib/configurable.rb,
lib/configurable/version.rb,
lib/configurable/config_hash.rb,
lib/configurable/conversions.rb,
lib/configurable/class_methods.rb,
lib/configurable/module_methods.rb,
lib/configurable/config_types/nest_type.rb,
lib/configurable/config_types/float_type.rb,
lib/configurable/config_types/object_type.rb,
lib/configurable/config_types/string_type.rb,
lib/configurable/config_types/boolean_type.rb,
lib/configurable/config_types/integer_type.rb,
lib/configurable/config_classes/list_config.rb,
lib/configurable/config_classes/nest_config.rb,
lib/configurable/config_classes/scalar_config.rb

Overview

Configurable enables the specification of configurations within a class definition. Include and declare configs as below.

class ConfigClass
  include Configurable
  config :one, 'one'
  config :two, 'two'
  config :three, 'three'
end

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

Defined Under Namespace

Modules: ClassMethods, ConfigClasses, ConfigTypes, Conversions, ModuleMethods Classes: ConfigHash

Constant Summary collapse

VERSION =
'1.0.0'
DEFAULT_CONFIG_TYPES =

Hash of default config types (bool, integer, float, string).

{
  :bool    => ConfigTypes::BooleanType,
  :integer => ConfigTypes::IntegerType,
  :float   => ConfigTypes::FloatType,
  :string  => ConfigTypes::StringType,
  :nest    => ConfigTypes::NestType,
  :obj     => ConfigTypes::ObjectType
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ModuleMethods

included

Instance Attribute Details

#configObject (readonly)

A ConfigHash bound to self. Accessing configurations through config is much slower (although sometimes more convenient) than through the config accessors.



22
23
24
# File 'lib/configurable.rb', line 22

def config
  @config
end

Instance Method Details

#initialize(*args) ⇒ Object

Initializes config, if necessary, and then calls super. If initialize is overridden without calling super, be sure to call initialize_config manually within the new initialize method.



27
28
29
30
# File 'lib/configurable.rb', line 27

def initialize(*args)
  initialize_config unless instance_variable_defined?(:@config)
  super
end

#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.



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

def initialize_copy(orig)
  super
  @config = ConfigHash.new(orig.config.store.dup, self)
end