Class: MuchStub::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/much-stub.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  @metaclass   = 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

#doObject

Returns the value of attribute do.



91
92
93
# File 'lib/much-stub.rb', line 91

def do
  @do
end

#ivar_nameObject (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_nameObject (readonly)

Returns the value of attribute method_name.



91
92
93
# File 'lib/much-stub.rb', line 91

def method_name
  @method_name
end

#nameObject (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(*pargs, orig_caller: nil, **kargs, &block) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/much-stub.rb', line 115

def call(*pargs, orig_caller: nil, **kargs, &block)
  orig_caller ||= caller_locations
  args = combined_args(pargs, kargs)
  unless MuchStub.arity_matches?(@method, args)
    raise(
      StubArityError.new(
        @method,
        args,
        method_name: @method_name,
        backtrace: orig_caller,
      ),
    )
  end
  lookup(pargs, kargs, orig_caller).call(*pargs, **kargs, &block)
rescue NotStubbedError
  @lookup.rehash
  lookup(pargs, kargs, orig_caller).call(*pargs, **kargs, &block)
end

#call_method(*pargs, **kargs, &block) ⇒ Object



111
112
113
# File 'lib/much-stub.rb', line 111

def call_method(*pargs, **kargs, &block)
  @method.call(*pargs, **kargs, &block)
end

#inspectObject



171
172
173
174
175
# File 'lib/much-stub.rb', line 171

def inspect
  "#<#{self.class}:#{format("0x0%x", (object_id << 1))}" \
  " @method_name=#{@method_name.inspect}" \
  ">"
end

#on_call(&on_call_block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/much-stub.rb', line 151

def on_call(&on_call_block)
  stub_block =
    ->(*pargs, **kargs, &block){
      on_call_block&.call(MuchStub::Call.new(*pargs, **kargs, &block))
    }
  if @lookup.empty?
    @do = stub_block
  elsif @lookup.value?(nil)
    @lookup.transform_values!{ |value| value.nil? ? stub_block : value }
  end
  self
end

#teardownObject



164
165
166
167
168
169
# File 'lib/much-stub.rb', line 164

def teardown
  @metaclass.send(:undef_method, @method_name)
  MuchStub.send(:remove_instance_variable, @ivar_name)
  @metaclass.send(:alias_method, @method_name, @name)
  @metaclass.send(:undef_method, @name)
end

#with(*pargs, **kargs, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/much-stub.rb', line 134

def with(*pargs, **kargs, &block)
  orig_caller = caller_locations
  args = combined_args(pargs, kargs)
  unless MuchStub.arity_matches?(@method, args)
    raise(
      StubArityError.new(
        @method,
        args,
        method_name: @method_name,
        backtrace: orig_caller,
      ),
    )
  end
  @lookup[args] = block
  self
end