Class: Crispy::CrispyInternal::SpyBase
- Inherits:
-
Module
- Object
- Module
- Crispy::CrispyInternal::SpyBase
- Defined in:
- lib/crispy/crispy_internal/spy_base.rb
Constant Summary collapse
- BLACK_LISTED_METHODS =
[ :__CRISPY_SPY__, ]
- COMMON_RECEIVED_MESSAGE_METHODS_DEFINITION =
{ 'received?' => 'include? %s', 'received_once?' => 'one? {|self_thing| self_thing == %s }', 'count_received' => 'count %s', }
Class Method Summary collapse
Instance Method Summary collapse
- #append_received_message(receiver, method_name, *arguments, &attached_block) ⇒ Object
- #append_received_message_when_spying(receiver, method_name, *arguments, &attached_block) ⇒ Object
- #assert_symbol!(maybe_symbol) ⇒ Object
- #erase_log ⇒ Object
-
#initialize(target, stubs_map = {}) ⇒ SpyBase
constructor
A new instance of SpyBase.
- #received_messages ⇒ Object
- #reinitialize(stubs_map = {}) ⇒ Object
- #reinitialize_stubber(stubs_map = {}) ⇒ Object
- #restart ⇒ Object
- #stop ⇒ Object
- #stub(method_name_or_hash, returned_value = nil, &definition) ⇒ Object
- #target_to_class(target) ⇒ Object
Constructor Details
#initialize(target, stubs_map = {}) ⇒ SpyBase
Returns a new instance of SpyBase.
13 14 15 16 17 18 19 20 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 13 def initialize target, stubs_map = {} prepend_features target_to_class(target) @stubbed_methods = [] stub stubs_map @spying = true end |
Class Method Details
.new(target, stubs_map = {}) ⇒ Object
22 23 24 25 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 22 def self.new target, stubs_map = {} spy = self.of_target(target) spy ? spy.reinitialize(stubs_map) : super end |
.of_target(target) ⇒ Object
27 28 29 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 27 def self.of_target target raise NotImplementedError end |
Instance Method Details
#append_received_message(receiver, method_name, *arguments, &attached_block) ⇒ Object
43 44 45 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 43 def receiver, method_name, *arguments, &attached_block raise NotImplementedError end |
#append_received_message_when_spying(receiver, method_name, *arguments, &attached_block) ⇒ Object
73 74 75 76 77 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 73 def receiver, method_name, *arguments, &attached_block if @spying receiver, method_name, *arguments, &attached_block end end |
#assert_symbol!(maybe_symbol) ⇒ Object
152 153 154 155 156 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 152 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 ⇒ Object
39 40 41 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 39 def erase_log raise NotImplementedError end |
#received_messages ⇒ Object
35 36 37 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 35 def raise NotImplementedError end |
#reinitialize(stubs_map = {}) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 47 def reinitialize stubs_map = {} restart erase_log reinitialize_stubber stubs_map self end |
#reinitialize_stubber(stubs_map = {}) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 127 def reinitialize_stubber stubs_map = {} remove_method(*@stubbed_methods) @stubbed_methods.each {|stubbed_method| define_wrapper stubbed_method } @stubbed_methods.clear stub stubs_map end |
#restart ⇒ Object
58 59 60 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 58 def restart @spying = true end |
#stop ⇒ Object
54 55 56 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 54 def stop @spying = false end |
#stub(method_name_or_hash, returned_value = nil, &definition) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 101 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. self, method_name, *arguments, &block returned_value end end end self end |
#target_to_class(target) ⇒ Object
31 32 33 |
# File 'lib/crispy/crispy_internal/spy_base.rb', line 31 def target_to_class target raise NotImplementedError end |