Class: Naught::NullClassBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/naught/null_class_builder.rb,
lib/naught/null_class_builder/command.rb,
lib/naught/null_class_builder/commands/pebble.rb

Defined Under Namespace

Modules: Commands Classes: Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNullClassBuilder

Returns a new instance of NullClassBuilder.



12
13
14
15
16
17
18
# File 'lib/naught/null_class_builder.rb', line 12

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



66
67
68
69
70
71
72
73
74
# File 'lib/naught/null_class_builder.rb', line 66

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_classObject

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_procObject

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_definedObject

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_holeObject

Builder API

See also the contents of lib/naught/null_class_builder/commands



98
99
100
# File 'lib/naught/null_class_builder.rb', line 98

def black_hole
  @stub_strategy = :stub_method_returning_self
end

#customization_moduleObject



29
30
31
# File 'lib/naught/null_class_builder.rb', line 29

def customization_module
  @customization_module ||= Module.new
end

#customize(&customization_block) ⇒ Object



24
25
26
27
# File 'lib/naught/null_class_builder.rb', line 24

def customize(&customization_block)
  return unless customization_block
  customization_module.module_exec(self, &customization_block)
end

#defer(options = {}, &deferred_operation) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/naught/null_class_builder.rb', line 114

def defer(options = {}, &deferred_operation)
  list = options[:class] ? class_operations : operations
  if options[:prepend]
    list.unshift(deferred_operation)
  else
    list << deferred_operation
  end
end

#generate_classObject



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
62
63
64
# File 'lib/naught/null_class_builder.rb', line 37

def generate_class
  respond_to_any_message 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

#interface_defined?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/naught/null_class_builder.rb', line 20

def interface_defined?
  !!@interface_defined
end

#null_equivalentsObject



33
34
35
# File 'lib/naught/null_class_builder.rb', line 33

def null_equivalents
  @null_equivalents ||= [nil]
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/naught/null_class_builder.rb', line 84

def respond_to?(method_name, include_private = false)
  command_name = command_name_for_method(method_name)
  Commands.const_defined?(command_name) || super
rescue NameError
  super
end

#respond_to_any_messageObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/naught/null_class_builder.rb', line 102

def respond_to_any_message
  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

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'lib/naught/null_class_builder.rb', line 77

def respond_to_missing?(method_name, include_private = false)
  command_name = command_name_for_method(method_name)
  Commands.const_defined?(command_name) || super
rescue NameError
  super
end

#stub_method(subject, name) ⇒ Object



123
124
125
# File 'lib/naught/null_class_builder.rb', line 123

def stub_method(subject, name)
  send(@stub_strategy, subject, name)
end