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



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
  @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.



85
86
87
# File 'lib/much-stub.rb', line 85

def do
  @do
end

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

Returns the value of attribute method_name.



85
86
87
# File 'lib/much-stub.rb', line 85

def method_name
  @method_name
end

#nameObject (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

#inspectObject



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

#teardownObject



152
153
154
155
156
157
# File 'lib/much-stub.rb', line 152

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(*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