class Crispy::CrispyInternal::SpyBase

Constants

BLACK_LISTED_METHODS
COMMON_RECEIVED_MESSAGE_METHODS_DEFINITION

Public Class Methods

new(target, except: []) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 13
def initialize target, except: []
  @exceptions = Array(except).map(&:to_sym)

  prepend_features target_to_class(target)

  @stubbed_methods = []

  @spying = true
end
new(target, except: []) click to toggle source
Calls superclass method
# File lib/crispy/crispy_internal/spy_base.rb, line 23
def self.new target, except: []
  spy = self.of_target(target)
  if spy
    spy.update_exceptions(target, except)
    spy.reinitialize
  else
    super
  end
end
of_target(target) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 33
def self.of_target target
  raise NotImplementedError
end

Public Instance Methods

append_received_message(receiver, method_name, *arguments, &attached_block) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 49
def append_received_message receiver, method_name, *arguments, &attached_block
  raise NotImplementedError
end
append_received_message_when_spying(receiver, method_name, *arguments, &attached_block) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 93
def append_received_message_when_spying receiver, method_name, *arguments, &attached_block
  if @spying
    append_received_message receiver, method_name, *arguments, &attached_block
  end
end
assert_symbol!(maybe_symbol) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 185
def assert_symbol! maybe_symbol
  unless maybe_symbol.respond_to?(:to_sym) && maybe_symbol.to_sym.instance_of?(::Symbol)
    raise TypeError, "TypeError: no implicit conversion from #{maybe_symbol.inspect} to symbol"
  end
end
erase_log() click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 45
def erase_log
  raise NotImplementedError
end
received_messages() click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 41
def received_messages
  raise NotImplementedError
end
redefine_wrappers(klass, method_names) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 177
def redefine_wrappers klass, method_names
  self.module_eval do
    define_public_wrappers_only(klass, method_names)
    define_protected_wrappers_only(klass, method_names)
    define_private_wrappers_only(klass, method_names)
  end
end
reinitialize() click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 53
def reinitialize
  restart
  erase_log
  reinitialize_stubber
  self
end
reinitialize_stubber() click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 147
def reinitialize_stubber
  remove_method(*@stubbed_methods)
  @stubbed_methods.each {|stubbed_method| define_wrapper stubbed_method }
  @stubbed_methods.clear
end
restart() click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 78
def restart
  @spying = true
end
stop() click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 74
def stop
  @spying = false
end
stub(method_name_or_hash, returned_value = nil, &definition) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 121
def stub method_name_or_hash, returned_value = nil, &definition
  case method_name_or_hash
  when Hash
    hash = method_name_or_hash
    hash.each do|method_name, value|
      stub method_name, value
    end
  when ::Symbol, ::String
    @stubbed_methods << method_name_or_hash

    self.module_exec method_name_or_hash do|method_name|
      spy = self

      # remove methods already defined (maybe by define_wrapper) to avoid warning.
      remove_method method_name if public_method_defined? method_name

      # TODO: should not ignore arguments?
      define_method(method_name) do|*arguments, &block|
        spy.append_received_message_when_spying self, method_name, *arguments, &block
        returned_value
      end
    end
  end
  self
end
target_to_class(target) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 37
def target_to_class target
  raise NotImplementedError
end
update_exceptions(target, exceptions) click to toggle source
# File lib/crispy/crispy_internal/spy_base.rb, line 60
def update_exceptions target, exceptions
  return if exceptions.empty?

  given_exceptions = Array(exceptions).map(&:to_sym)

  new_exceptions = given_exceptions - @exceptions
  remove_method(*new_exceptions)

  old_exceptions = @exceptions - given_exceptions
  redefine_wrappers target_to_class(target), old_exceptions

  @exceptions.replace given_exceptions
end

Private Instance Methods

define_wrapper(method_name) click to toggle source
Calls superclass method
# File lib/crispy/crispy_internal/spy_base.rb, line 82
def define_wrapper method_name
  spy = self
  define_method method_name do|*arguments, &attached_block|
    spy.append_received_message_when_spying(self, method_name, *arguments, &attached_block)

    super(*arguments, &attached_block)
  end
  method_name
end
prepend_features(klass) click to toggle source
Calls superclass method
# File lib/crispy/crispy_internal/spy_base.rb, line 166
def prepend_features klass
  super

  self.module_eval do
    define_public_wrappers_except(klass, @exceptions)
    define_protected_wrappers_except(klass, @exceptions)
    define_private_wrappers_except(klass, @exceptions)
  end
end