Class: Naught::NullClassBuilder::Commands::Mimic

Inherits:
Naught::NullClassBuilder::Command show all
Defined in:
lib/naught/null_class_builder/commands/mimic.rb

Direct Known Subclasses

Impersonate

Constant Summary collapse

NULL_SINGLETON_CLASS =
(class << Object.new; self; end)

Instance Attribute Summary collapse

Attributes inherited from Naught::NullClassBuilder::Command

#builder

Instance Method Summary collapse

Methods inherited from Naught::NullClassBuilder::Command

#defer

Constructor Details

#initialize(builder, class_to_mimic_or_options, options = {}) ⇒ Mimic

Returns a new instance of Mimic.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/naught/null_class_builder/commands/mimic.rb', line 12

def initialize(builder, class_to_mimic_or_options, options = {})
  super(builder)

  if class_to_mimic_or_options.is_a?(Hash)
    options          = class_to_mimic_or_options.merge(options)
    instance         = options.fetch(:example)
    @singleton_class = (class << instance; self; end)
    @class_to_mimic  = instance.class
  else
    @singleton_class = NULL_SINGLETON_CLASS
    @class_to_mimic  = class_to_mimic_or_options
  end
  @include_super = options.fetch(:include_super) { true }

  builder.base_class   = root_class_of(@class_to_mimic)
  class_to_mimic       = @class_to_mimic
  builder.inspect_proc = lambda { "<null:#{class_to_mimic}>" }
  builder.interface_defined = true
end

Instance Attribute Details

#class_to_mimicObject (readonly)

Returns the value of attribute class_to_mimic.



10
11
12
# File 'lib/naught/null_class_builder/commands/mimic.rb', line 10

def class_to_mimic
  @class_to_mimic
end

#include_superObject (readonly)

Returns the value of attribute include_super.



10
11
12
# File 'lib/naught/null_class_builder/commands/mimic.rb', line 10

def include_super
  @include_super
end

#singleton_classObject (readonly)

Returns the value of attribute singleton_class.



10
11
12
# File 'lib/naught/null_class_builder/commands/mimic.rb', line 10

def singleton_class
  @singleton_class
end

Instance Method Details

#callObject



32
33
34
35
36
37
38
# File 'lib/naught/null_class_builder/commands/mimic.rb', line 32

def call
  defer do |subject|
    methods_to_stub.each do |method_name|
      builder.stub_method(subject, method_name)
    end
  end
end