Class: Naught::NullClassBuilder
- Inherits:
-
Object
- Object
- Naught::NullClassBuilder
- Defined in:
- lib/naught/null_class_builder.rb,
lib/naught/null_class_builder/command.rb,
lib/naught/null_class_builder/commands/mimic.rb,
lib/naught/null_class_builder/commands/pebble.rb,
lib/naught/null_class_builder/commands/callstack.rb,
lib/naught/null_class_builder/commands/singleton.rb,
lib/naught/null_class_builder/commands/traceable.rb,
lib/naught/null_class_builder/commands/impersonate.rb,
lib/naught/null_class_builder/commands/null_safe_proxy.rb,
lib/naught/null_class_builder/commands/predicates_return.rb,
lib/naught/null_class_builder/commands/define_explicit_conversions.rb,
lib/naught/null_class_builder/commands/define_implicit_conversions.rb
Overview
Builds customized null object classes via a small DSL
Defined Under Namespace
Modules: Commands Classes: Command
Instance Attribute Summary collapse
-
#base_class ⇒ Class
The base class for generated null objects.
-
#inspect_proc ⇒ Proc
The inspect implementation for generated null objects.
-
#interface_defined ⇒ Boolean
(also: #interface_defined?)
Whether a method-missing interface has been defined.
Instance Method Summary collapse
-
#black_hole ⇒ void
Configure method stubs to return self (black hole behavior).
-
#customization_module ⇒ Module
Returns the module that holds customization methods.
-
#customize {|builder| ... } ⇒ void
Apply a customization block to this builder.
-
#defer(options = {}) {|subject| ... } ⇒ void
Queue a deferred operation to be applied during class generation.
-
#defer_prepend_module ⇒ void
Prepend a module generated from the given block.
-
#generate_class ⇒ Class
Generate the null object class based on queued operations.
-
#initialize ⇒ NullClassBuilder
constructor
private
Create a new builder with default configuration.
-
#method_missing(method_name, *args) ⇒ void
private
Dispatch builder DSL calls to command classes.
-
#null_equivalents ⇒ Array<Object>
Returns the list of values treated as null-equivalent.
-
#respond_to_any_message ⇒ void
Make null objects respond to any message.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
private
Check if builder responds to a DSL command.
-
#stub_method(subject, name) ⇒ void
Stub a method using the current stub strategy.
Constructor Details
#initialize ⇒ NullClassBuilder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new builder with default configuration
44 45 46 47 48 49 50 |
# File 'lib/naught/null_class_builder.rb', line 44 def initialize @interface_defined = false @base_class = Naught::BasicObject @inspect_proc = -> { "<null>" } @stub_strategy = StubStrategy::ReturnNil define_basic_methods end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Dispatch builder DSL calls to command classes
158 159 160 161 |
# File 'lib/naught/null_class_builder.rb', line 158 def method_missing(method_name, *args, &) command_class = lookup_command(method_name) command_class ? command_class.new(self, *args, &).call : super end |
Instance Attribute Details
#base_class ⇒ Class
The base class for generated null objects
19 20 21 |
# File 'lib/naught/null_class_builder.rb', line 19 def base_class @base_class end |
#inspect_proc ⇒ Proc
The inspect implementation for generated null objects
26 27 28 |
# File 'lib/naught/null_class_builder.rb', line 26 def inspect_proc @inspect_proc end |
#interface_defined ⇒ Boolean Also known as: interface_defined?
Whether a method-missing interface has been defined
33 34 35 |
# File 'lib/naught/null_class_builder.rb', line 33 def interface_defined @interface_defined end |
Instance Method Details
#black_hole ⇒ void
This method returns an undefined value.
Configure method stubs to return self (black hole behavior)
101 102 103 104 105 106 107 108 |
# File 'lib/naught/null_class_builder.rb', line 101 def black_hole @stub_strategy = StubStrategy::ReturnSelf # Prepend marshal methods to avoid infinite recursion with method_missing defer_prepend_module do define_method(:marshal_dump) { nil } define_method(:marshal_load) { |*| nil } end end |
#customization_module ⇒ Module
Returns the module that holds customization methods
67 |
# File 'lib/naught/null_class_builder.rb', line 67 def customization_module = @customization_module ||= Module.new |
#customize {|builder| ... } ⇒ void
This method returns an undefined value.
Apply a customization block to this builder
58 59 60 |
# File 'lib/naught/null_class_builder.rb', line 58 def customize(&) customization_module.module_exec(self, &) if block_given? end |
#defer(options = {}) {|subject| ... } ⇒ void
This method returns an undefined value.
Queue a deferred operation to be applied during class generation
130 131 132 133 |
# File 'lib/naught/null_class_builder.rb', line 130 def defer( = {}, &operation) target = [:class] ? class_operations : operations [:prepend] ? target.unshift(operation) : target.push(operation) end |
#defer_prepend_module ⇒ void
This method returns an undefined value.
Prepend a module generated from the given block
140 141 142 |
# File 'lib/naught/null_class_builder.rb', line 140 def defer_prepend_module(&) prepend_modules << Module.new(&) end |
#generate_class ⇒ Class
Generate the null object class based on queued operations
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/naught/null_class_builder.rb', line 81 def generate_class unless interface_defined? generation_mod = Module.new apply_operations(operations, generation_mod) null_class = build_null_class(generation_mod) apply_operations(class_operations, null_class) null_class end |
#null_equivalents ⇒ Array<Object>
Returns the list of values treated as null-equivalent
74 |
# File 'lib/naught/null_class_builder.rb', line 74 def null_equivalents = @null_equivalents ||= [nil] |
#respond_to_any_message ⇒ void
This method returns an undefined value.
Make null objects respond to any message
115 116 117 118 119 120 121 |
# File 'lib/naught/null_class_builder.rb', line 115 def defer(prepend: true) do |subject| subject.define_method(:respond_to?) { |*, **| true } stub_method(subject, :method_missing) end @interface_defined = true end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if builder responds to a DSL command
169 170 171 172 173 |
# File 'lib/naught/null_class_builder.rb', line 169 def respond_to_missing?(method_name, include_private = false) !lookup_command(method_name).nil? || super rescue NameError super end |
#stub_method(subject, name) ⇒ void
This method returns an undefined value.
Stub a method using the current stub strategy
151 152 153 |
# File 'lib/naught/null_class_builder.rb', line 151 def stub_method(subject, name) @stub_strategy.apply(subject, name) end |