Class: ModuleBuilder::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/module_builder/builder.rb

Overview

Builds a module based on instance-level state and class-level configuration.

This class is intended to be subclassed, not used as-is. There are several methods to override in order to give the builder the behavior you want. You can also define setup methods that are specified in the #hooks array for arbitrary setup based on the state passed into the builder’s constructor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state = {}) ⇒ Builder

Creates a new module builder that uses the specified base module as a foundation for its built module and sets any other specified key/value pairs as instance variables on the builder.

Examples:

ModuleBuilder::Builder.new(base: Module.new)

Parameters:

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

    the state to use for defined hooks.

Options Hash (state):

  • :base (Module) — default: Module.new

    the module to use as a base on which to build.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/module_builder/builder.rb', line 26

def initialize(state = {})
  state = [builder_defaults, defaults, state].reduce(&:merge)
  @module = state.delete(:base)

  state.each_pair do |attr, value|
    instance_variable_set("@#{attr}", value)
  end

  add_extended_hook
  add_inclusions
  add_defined_hooks
end

Instance Attribute Details

#moduleObject (readonly)

The module built by the builder.



14
15
16
# File 'lib/module_builder/builder.rb', line 14

def module
  @module
end

Instance Method Details

#defaultsHash

Note:

This can be overridden in a subclass with any default values for state variables that are needed in defined hooks.

The defaults for any state values that require them.

Returns:

  • (Hash)

    the default state for defined hooks.



45
46
47
# File 'lib/module_builder/builder.rb', line 45

def defaults
  {}
end

#extensionsArray<Module>

Note:

This can be overridden in a subclass with any modules that should be extended onto modules that extend the built module.

Lists the modules to be added into the Module#extended hook of the built module.

Returns:

  • (Array<Module>)

    the modules to be extended onto any modules that extend the built module.



57
58
59
# File 'lib/module_builder/builder.rb', line 57

def extensions
  []
end

#hooksArray<Symbol>

Note:

This can be overridden in a subclass with any methods that should be called to build the built module.

Lists the methods that should be called when building a module.

Returns:

  • (Array<Symbol>)

    the methods to call when building the module.



68
69
70
# File 'lib/module_builder/builder.rb', line 68

def hooks
  []
end

#inclusionsArray<Module>

Note:

This can be overridden in a subclass to automatically include modules in the built module.

Lists the modules to be included into the built module.

Returns:

  • (Array<Module>)

    the modules to be included into the built module.



79
80
81
# File 'lib/module_builder/builder.rb', line 79

def inclusions
  []
end