Class: MuchStub::Stub
- Inherits:
-
Object
- Object
- MuchStub::Stub
- Defined in:
- lib/much-stub.rb
Instance Attribute Summary collapse
-
#do ⇒ Object
Returns the value of attribute do.
-
#ivar_name ⇒ Object
readonly
Returns the value of attribute ivar_name.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #call(args, orig_caller = nil, &block) ⇒ Object
- #call_method(args, &block) ⇒ Object
-
#initialize(object, method_name, orig_caller = nil, &block) ⇒ Stub
constructor
A new instance of Stub.
- #inspect ⇒ Object
- #on_call(&on_call_block) ⇒ Object
- #teardown ⇒ Object
- #with(*args, &block) ⇒ Object
Constructor Details
#initialize(object, method_name, orig_caller = nil, &block) ⇒ Stub
Returns a new instance of Stub.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/much-stub.rb', line 93 def initialize(object, method_name, orig_caller = nil, &block) orig_caller ||= caller_locations = class << object; self; end @method_name = method_name.to_s @name = "__muchstub_stub__#{object.object_id}_#{@method_name}" @ivar_name = "@__muchstub_stub_#{object.object_id}_" \ "#{@method_name.to_sym.object_id}" setup(object, orig_caller) @do = block @lookup = {} end |
Instance Attribute Details
#do ⇒ Object
Returns the value of attribute do.
91 92 93 |
# File 'lib/much-stub.rb', line 91 def do @do end |
#ivar_name ⇒ Object (readonly)
Returns the value of attribute ivar_name.
91 92 93 |
# File 'lib/much-stub.rb', line 91 def ivar_name @ivar_name end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
91 92 93 |
# File 'lib/much-stub.rb', line 91 def method_name @method_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
91 92 93 |
# File 'lib/much-stub.rb', line 91 def name @name end |
Class Method Details
.key(object, method_name) ⇒ Object
87 88 89 |
# File 'lib/much-stub.rb', line 87 def self.key(object, method_name) "--#{object.object_id}--#{method_name}--" end |
Instance Method Details
#call(args, orig_caller = nil, &block) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/much-stub.rb', line 115 def call(args, orig_caller = nil, &block) orig_caller ||= caller_locations unless MuchStub.arity_matches?(@method, args) raise( StubArityError.new( @method, args, method_name: @method_name, backtrace: orig_caller, ), ) end lookup(args, orig_caller).call(*args, &block) rescue NotStubbedError @lookup.rehash lookup(args, orig_caller).call(*args, &block) end |
#call_method(args, &block) ⇒ Object
111 112 113 |
# File 'lib/much-stub.rb', line 111 def call_method(args, &block) @method.call(*args, &block) end |
#inspect ⇒ Object
169 170 171 172 173 |
# File 'lib/much-stub.rb', line 169 def inspect "#<#{self.class}:#{format("0x0%x", (object_id << 1))}" \ " @method_name=#{@method_name.inspect}" \ ">" end |
#on_call(&on_call_block) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/much-stub.rb', line 149 def on_call(&on_call_block) stub_block = ->(*args, &block){ on_call_block&.call(MuchStub::Call.new(*args, &block)) } if @lookup.empty? @do = stub_block elsif @lookup.value?(nil) @lookup.transform_values!{ |value| value.nil? ? stub_block : value } end self end |
#teardown ⇒ Object
162 163 164 165 166 167 |
# File 'lib/much-stub.rb', line 162 def teardown .send(:undef_method, @method_name) MuchStub.send(:remove_instance_variable, @ivar_name) .send(:alias_method, @method_name, @name) .send(:undef_method, @name) end |
#with(*args, &block) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/much-stub.rb', line 133 def with(*args, &block) orig_caller = caller_locations unless MuchStub.arity_matches?(@method, args) raise( StubArityError.new( @method, args, method_name: @method_name, backtrace: orig_caller, ), ) end @lookup[args] = block self end |