Class: MuchStub::CallSpy

Inherits:
BasicObject
Defined in:
lib/much-stub/call_spy.rb

Constant Summary collapse

METHOD_NAME_REPLACEMENTS =
{
  "!" => "_bang",
  "?" => "_predicate"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(**return_values) ⇒ CallSpy

Returns a new instance of CallSpy.



10
11
12
13
14
15
# File 'lib/much-stub/call_spy.rb', line 10

def initialize(**return_values)
  @call_spy_return_values = return_values.transform_keys{ |key| key.to_s }
  @call_spy_method_calls = ::Hash.new { |hash, key| hash[key] = [] }
  @call_spy_method_return_values =
    ::Hash.new { |hash, key| hash[key] = call_spy_return_value_proc(key) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/much-stub/call_spy.rb', line 90

def method_missing(name, *args, &block)
  if (match = name.match(/(\w+)(_calls)\z/))
    call_spy_define_query_method(match) do |method_name|
      @call_spy_method_calls[method_name]
    end
    self.__send__(name, *args, &block)
  elsif (match = name.match(/(\w+)(_last_called_with)\z/))
    call_spy_define_query_method(match) do |method_name|
      self.__send__("#{method_name}_calls").last
    end
    self.__send__(name, *args, &block)
  elsif (match = name.match(/(\w+)(_called_with)\z/))
    call_spy_define_query_method(match) do |method_name|
      self.__send__("#{method_name}_last_called_with")
    end
    self.__send__(name, *args, &block)
  elsif (match = name.match(/(\w+)(_call_count)\z/))
    call_spy_define_query_method(match) do |method_name|
      self.__send__("#{method_name}_calls").size
    end
    self.__send__(name, *args, &block)
  elsif (match = name.match(/(\w+)(_called\?)\z/))
    call_spy_define_query_method(match) do |method_name|
      self.__send__("#{method_name}_call_count") > 0
    end
    self.__send__(name, *args, &block)
  else
    call_spy_define_spied_method(name)
    self.__send__(name, *args, &block)
  end
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/much-stub/call_spy.rb', line 22

def ==(other)
  self.equal?(other)
end

#===(other) ⇒ Object



26
27
28
# File 'lib/much-stub/call_spy.rb', line 26

def ===(other)
  self.equal?(other)
end

#call_spy_tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
# File 'lib/much-stub/call_spy.rb', line 17

def call_spy_tap
  yield self
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/much-stub/call_spy.rb', line 30

def eql?(other)
  self.equal?(other)
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/much-stub/call_spy.rb', line 34

def equal?(other)
  self.__id__ == other.__id__
end

#hashObject



38
39
40
# File 'lib/much-stub/call_spy.rb', line 38

def hash
  self.__id__
end

#inspectObject



46
47
48
# File 'lib/much-stub/call_spy.rb', line 46

def inspect
  "#<MuchStub::CallSpy:#{"0x0%x" % (self.__id__ << 1)}>"
end

#respond_to?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/much-stub/call_spy.rb', line 42

def respond_to?(*)
  true
end