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.



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

def initialize(**return_values)
  @call_spy_return_values = return_values.transform_keys(&: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)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/much-stub/call_spy.rb', line 98

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
  elsif (match = name.match(/(\w+)(_last_called_with)\z/))
    call_spy_define_query_method(match) do |method_name|
      __send__("#{method_name}_calls").last
    end
  elsif (match = name.match(/(\w+)(_called_with)\z/))
    call_spy_define_query_method(match) do |method_name|
      __send__("#{method_name}_last_called_with")
    end
  elsif (match = name.match(/(\w+)(_call_count)\z/))
    call_spy_define_query_method(match) do |method_name|
      __send__("#{method_name}_calls").size
    end
  elsif (match = name.match(/(\w+)(_called\?)\z/))
    call_spy_define_query_method(match) do |method_name|
      __send__("#{method_name}_call_count") > 0
    end
  else
    call_spy_define_spied_method(name)
  end
  __send__(name, *args, &block)
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
# File 'lib/much-stub/call_spy.rb', line 24

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

#===(other) ⇒ Object



28
29
30
# File 'lib/much-stub/call_spy.rb', line 28

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

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

Yields:

  • (_self)

Yield Parameters:



19
20
21
22
# File 'lib/much-stub/call_spy.rb', line 19

def call_spy_tap
  yield self
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/much-stub/call_spy.rb', line 32

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

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/much-stub/call_spy.rb', line 36

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

#hashObject



40
41
42
# File 'lib/much-stub/call_spy.rb', line 40

def hash
  __id__
end

#inspectObject



48
49
50
# File 'lib/much-stub/call_spy.rb', line 48

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

#respond_to?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/much-stub/call_spy.rb', line 44

def respond_to?(*)
  true
end