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
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/much-stub.rb', line 87 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.
85 86 87 |
# File 'lib/much-stub.rb', line 85 def do @do end |
#ivar_name ⇒ Object (readonly)
Returns the value of attribute ivar_name.
85 86 87 |
# File 'lib/much-stub.rb', line 85 def ivar_name @ivar_name end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
85 86 87 |
# File 'lib/much-stub.rb', line 85 def method_name @method_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
85 86 87 |
# File 'lib/much-stub.rb', line 85 def name @name end |
Class Method Details
.key(object, method_name) ⇒ Object
81 82 83 |
# File 'lib/much-stub.rb', line 81 def self.key(object, method_name) "--#{object.object_id}--#{method_name}--" end |
Instance Method Details
#call(args, orig_caller = nil, &block) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/much-stub.rb', line 109 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
105 106 107 |
# File 'lib/much-stub.rb', line 105 def call_method(args, &block) @method.call(*args, &block) end |
#inspect ⇒ Object
159 160 161 162 163 |
# File 'lib/much-stub.rb', line 159 def inspect "#<#{self.class}:#{"0x0%x" % (object_id << 1)}" \ " @method_name=#{@method_name.inspect}" \ ">" end |
#on_call(&on_call_block) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/much-stub.rb', line 139 def on_call(&on_call_block) stub_block = ->(*args, &block) { on_call_block.call(MuchStub::Call.new(*args, &block)) if on_call_block } if @lookup.empty? @do = stub_block elsif @lookup.has_value?(nil) @lookup.transform_values!{ |value| value.nil? ? stub_block : value } end self end |
#teardown ⇒ Object
152 153 154 155 156 157 |
# File 'lib/much-stub.rb', line 152 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
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/much-stub.rb', line 125 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 |