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/singleton.rb,
lib/naught/null_class_builder/commands/traceable.rb,
lib/naught/null_class_builder/commands/impersonate.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
rubocop:disable ClassLength
Defined Under Namespace
Modules: Commands Classes: Command
Instance Attribute Summary collapse
-
#base_class ⇒ Object
Returns the value of attribute base_class.
-
#inspect_proc ⇒ Object
Returns the value of attribute inspect_proc.
-
#interface_defined ⇒ Object
(also: #interface_defined?)
Returns the value of attribute interface_defined.
Instance Method Summary collapse
-
#black_hole ⇒ Object
Builder API.
- #customization_module ⇒ Object
- #customize(&customization_block) ⇒ Object
- #defer(options = {}, &deferred_operation) ⇒ Object
-
#generate_class ⇒ Object
rubocop:disable AbcSize.
-
#initialize ⇒ NullClassBuilder
constructor
A new instance of NullClassBuilder.
- #method_missing(method_name, *args, &block) ⇒ Object
- #null_equivalents ⇒ Object
- #respond_to?(method_name, include_private = false) ⇒ Boolean
- #respond_to_any_message ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #stub_method(subject, name) ⇒ Object
Constructor Details
#initialize ⇒ NullClassBuilder
Returns a new instance of NullClassBuilder.
13 14 15 16 17 18 19 |
# File 'lib/naught/null_class_builder.rb', line 13 def initialize @interface_defined = false @base_class = Naught::BasicObject @inspect_proc = lambda { '<null>' } @stub_strategy = :stub_method_returning_nil define_basic_methods end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/naught/null_class_builder.rb', line 98 def method_missing(method_name, *args, &block) command_name = command_name_for_method(method_name) if Commands.const_defined?(command_name) command_class = Commands.const_get(command_name) command_class.new(self, *args, &block).call else super end end |
Instance Attribute Details
#base_class ⇒ Object
Returns the value of attribute base_class.
10 11 12 |
# File 'lib/naught/null_class_builder.rb', line 10 def base_class @base_class end |
#inspect_proc ⇒ Object
Returns the value of attribute inspect_proc.
10 11 12 |
# File 'lib/naught/null_class_builder.rb', line 10 def inspect_proc @inspect_proc end |
#interface_defined ⇒ Object Also known as: interface_defined?
Returns the value of attribute interface_defined.
10 11 12 |
# File 'lib/naught/null_class_builder.rb', line 10 def interface_defined @interface_defined end |
Instance Method Details
#black_hole ⇒ Object
Builder API
See also the contents of lib/naught/null_class_builder/commands
69 70 71 |
# File 'lib/naught/null_class_builder.rb', line 69 def black_hole @stub_strategy = :stub_method_returning_self end |
#customization_module ⇒ Object
26 27 28 |
# File 'lib/naught/null_class_builder.rb', line 26 def customization_module @customization_module ||= Module.new end |
#customize(&customization_block) ⇒ Object
21 22 23 24 |
# File 'lib/naught/null_class_builder.rb', line 21 def customize(&customization_block) return unless customization_block customization_module.module_exec(self, &customization_block) end |
#defer(options = {}, &deferred_operation) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/naught/null_class_builder.rb', line 85 def defer( = {}, &deferred_operation) list = [:class] ? class_operations : operations if [:prepend] list.unshift(deferred_operation) else list << deferred_operation end end |
#generate_class ⇒ Object
rubocop:disable AbcSize
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/naught/null_class_builder.rb', line 34 def generate_class # rubocop:disable AbcSize unless interface_defined? generation_mod = Module.new customization_mod = customization_module # get a local binding builder = self apply_operations(operations, generation_mod) null_class = Class.new(@base_class) do const_set :GeneratedMethods, generation_mod const_set :Customizations, customization_mod const_set :NULL_EQUIVS, builder.null_equivalents include Conversions remove_const :NULL_EQUIVS Conversions.instance_methods.each do |instance_method| undef_method(instance_method) end const_set :Conversions, Conversions include NullObjectTag include generation_mod include customization_mod end apply_operations(class_operations, null_class) null_class end |
#null_equivalents ⇒ Object
30 31 32 |
# File 'lib/naught/null_class_builder.rb', line 30 def null_equivalents @null_equivalents ||= [nil] end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
113 114 115 |
# File 'lib/naught/null_class_builder.rb', line 113 def respond_to?(method_name, include_private = false) respond_to_definition(method_name, include_private, :respond_to?) end |
#respond_to_any_message ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/naught/null_class_builder.rb', line 73 def defer(:prepend => true) do |subject| subject.module_eval do def respond_to?(*) true end end stub_method(subject, :method_missing) end @interface_defined = true end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
109 110 111 |
# File 'lib/naught/null_class_builder.rb', line 109 def respond_to_missing?(method_name, include_private = false) respond_to_definition(method_name, include_private, :respond_to_missing?) end |
#stub_method(subject, name) ⇒ Object
94 95 96 |
# File 'lib/naught/null_class_builder.rb', line 94 def stub_method(subject, name) send(@stub_strategy, subject, name) end |