Class: Democritus::ClassBuilder
- Inherits:
-
Object
- Object
- Democritus::ClassBuilder
- Defined in:
- lib/democritus/class_builder.rb,
lib/democritus/class_builder/command.rb,
lib/democritus/class_builder/commands/attribute.rb,
lib/democritus/class_builder/commands/attributes.rb
Overview
Responsible for building a class based on the customization’s applied through the #customize method.
:reek:UnusedPrivateMethod: { exclude: [ !ruby/regexp /(method_missing|respond_to_missing)/ ] }
Defined Under Namespace
Modules: Commands Classes: Command
Class Method Summary collapse
-
.command_name_for_method(method_name) ⇒ Object
Convert the given :method_name into a “constantized” method name.
Instance Method Summary collapse
-
#customize {|Democritus::ClassBuilder| ... } ⇒ Object
Responsible for executing the customization block against the customization module with the builder class as a parameter.
-
#defer(options = {}, &deferred_operation) ⇒ Object
rubocop:enable MethodLength.
-
#generate_class ⇒ Object
Responsible for generating a Class object based on the customizations applied via a customize block.
-
#initialize(command_namespaces: default_command_namespaces) ⇒ ClassBuilder
constructor
A new instance of ClassBuilder.
Constructor Details
#initialize(command_namespaces: default_command_namespaces) ⇒ ClassBuilder
Returns a new instance of ClassBuilder.
10 11 12 13 14 15 16 |
# File 'lib/democritus/class_builder.rb', line 10 def initialize(command_namespaces: default_command_namespaces) self.customization_module = Module.new self.generation_module = Module.new self.class_operations = [] self.instance_operations = [] self.command_namespaces = command_namespaces end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kargs, &block) ⇒ Object (private)
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/democritus/class_builder.rb', line 124 def method_missing(method_name, *args, **kargs, &block) command_name = self.class.command_name_for_method(method_name) command_namespace = command_namespace_for(command_name) if command_namespace command_class = command_namespace.const_get(command_name) command_class.new(*args, **kargs.merge(builder: self), &block).call else super end end |
Class Method Details
.command_name_for_method(method_name) ⇒ Object
Convert the given :method_name into a “constantized” method name.
162 163 164 |
# File 'lib/democritus/class_builder.rb', line 162 def command_name_for_method(method_name) method_name.to_s.gsub(/(?:^|_)([a-z])/) { Regexp.last_match[1].upcase } end |
Instance Method Details
#customize {|Democritus::ClassBuilder| ... } ⇒ Object
Responsible for executing the customization block against the customization module with the builder class as a parameter.
68 69 70 71 72 |
# File 'lib/democritus/class_builder.rb', line 68 def customize(&customization_block) return unless customization_block customization_module.module_exec(self, &customization_block) return nil end |
#defer(options = {}, &deferred_operation) ⇒ Object
rubocop:enable MethodLength
102 103 104 105 106 107 108 |
# File 'lib/democritus/class_builder.rb', line 102 def defer( = {}, &deferred_operation) if [:prepend] instance_operations.unshift(deferred_operation) else instance_operations << deferred_operation end end |
#generate_class ⇒ Object
Responsible for generating a Class object based on the customizations applied via a customize block.
rubocop:disable MethodLength :reek:TooManyStatements: { exclude: [ ‘Democritus::ClassBuilder#generate_class’ ] }
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/democritus/class_builder.rb', line 87 def generate_class generation_mod = generation_module # get a local binding customization_mod = customization_module # get a local binding apply_operations(instance_operations, generation_mod) generated_class = Class.new do const_set :GeneratedMethods, generation_mod const_set :Customizations, customization_mod include DemocritusObjectTag include generation_mod include customization_mod end generated_class end |