Module: ModuleBuilder::Buildable::ClassMethods

Defined in:
lib/module_builder/buildable.rb

Overview

Gives a module that mixes in Buildable a small DSL for specifying the class to use when building the module and the ability to easily build a new copy of the module using a given state.

Instance Method Summary collapse

Instance Method Details

#builder(builder_class = :not_set) ⇒ Class

Note:

When used as a class method, it declaratively sets the builder class. When used without a parameter, it uses the ‘:not_set` symbol as a marker to set itself into reader mode.

Sets and accesses the builder class for a buildable module.

Examples:

class MyBuilder < ModuleBuilder::Builder
end

module MyBuiltModule
  include ModuleBuilder::Buildable

  builder MyBuilder
end

Parameters:

  • builder_class (Class) (defaults to: :not_set)

    the class to instantiate when building the module.

Returns:

  • (Class)

    the builder class.

Raises:



37
38
39
40
41
42
43
# File 'lib/module_builder/buildable.rb', line 37

def builder(builder_class = :not_set)
  if builder_class.equal?(:not_set)
    builder_or_fail
  else
    @builder = builder_class
  end
end

#included(descendant) ⇒ void

This method returns an undefined value.

Includes the default version of the built module when included without a constructor.

Examples:

module MyBuildableModule
  include ModuleBuilder::Buildable

  class Builder < ModuleBuilder::Builder
  end
end

class MyUncustomizedClass
  include MyBuildableModule
end

Parameters:

  • descendant (Class, Module)

    the including class or module.



62
63
64
# File 'lib/module_builder/buildable.rb', line 62

def included(descendant)
  descendant.__send__(:include, new)
end

#new(state = {}) ⇒ Module

Builds a module with the configured builder class using the given state.

Examples:

module MyBuildableModule
  include ModuleBuilder::Buildable

  class Builder < ModuleBuilder::Builder
  end
end

class MyClass
  include MyBuildableModule.new(:example => "value")
end

Parameters:

  • state (Hash) (defaults to: {})

    the state to use when configuring the builder.

Returns:

  • (Module)

    the newly built module.



83
84
85
# File 'lib/module_builder/buildable.rb', line 83

def new(state = {})
  builder.new(state).module
end