Class: ModuleBuilder::Builder
- Inherits:
-
Object
- Object
- ModuleBuilder::Builder
- 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
-
#module ⇒ Object
readonly
The module built by the builder.
Instance Method Summary collapse
-
#defaults ⇒ Hash
The defaults for any state values that require them.
-
#extensions ⇒ Array<Module>
Lists the modules to be added into the Module#extended hook of the built module.
-
#hooks ⇒ Array<Symbol>
Lists the methods that should be called when building a module.
-
#inclusions ⇒ Array<Module>
Lists the modules to be included into the built module.
-
#initialize(state = {}) ⇒ Builder
constructor
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.
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.
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
#module ⇒ Object (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
#defaults ⇒ Hash
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.
45 46 47 |
# File 'lib/module_builder/builder.rb', line 45 def defaults {} end |
#extensions ⇒ Array<Module>
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.
57 58 59 |
# File 'lib/module_builder/builder.rb', line 57 def extensions [] end |
#hooks ⇒ Array<Symbol>
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.
68 69 70 |
# File 'lib/module_builder/builder.rb', line 68 def hooks [] end |
#inclusions ⇒ Array<Module>
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.
79 80 81 |
# File 'lib/module_builder/builder.rb', line 79 def inclusions [] end |