Module: RecordInvocation

Included in:
Controls::Recorder::Example, Controls::Recorder::RecordMacro::Example, Controls::Recorder::RecordMacro::IgnoredParameters::Example
Defined in:
lib/record_invocation/controls/recorder.rb,
lib/record_invocation/record_invocation.rb,
lib/record_invocation/controls/invocation.rb

Defined Under Namespace

Modules: Controls, Record

Constant Summary collapse

Error =
::Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(object, method, *methods) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/record_invocation/record_invocation.rb', line 10

def self.call(object, method, *methods)
  methods = [method, *methods]

  object.singleton_class.class_exec do
    include RecordInvocation

    methods.each do |method|
      record method
    end
  end
end

.included(cls) ⇒ Object



4
5
6
7
8
# File 'lib/record_invocation/record_invocation.rb', line 4

def self.included(cls)
  cls.class_exec do
    extend Record
  end
end

Instance Method Details

#__invocation(method_name, **parameters) ⇒ Object Also known as: invocation



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/record_invocation/record_invocation.rb', line 43

def __invocation(method_name, **parameters)
  once = parameters.delete(:once)
  once ||= false

  invocations = __invocations(method_name, **parameters)

  if invocations.empty?
    return nil
  end

  if once && invocations.length > 1
    raise Error, "More than one invocation record matches (Method Name: #{method_name.inspect}, Parameters: #{parameters.inspect})"
  end

  invocations.first
end

#__invocations(method_name = nil, **parameters) ⇒ Object Also known as: invocations



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/record_invocation/record_invocation.rb', line 67

def __invocations(method_name=nil, **parameters)
  if method_name.nil? && parameters.empty?
    return __records
  end

  invocations = __records.select { |invocation| invocation.method_name == method_name }

  if parameters.nil?
    return invocations
  end

  if invocations.empty?
    return []
  end

  invocations = invocations.select do |invocation|
    parameters.all? do |match_parameter_name, match_parameter_value|
      invocation_value = invocation.arguments[match_parameter_name]

      invocation_value == match_parameter_value
    end
  end

  invocations
end

#__invoked?(method_name = nil, **parameters) ⇒ Boolean Also known as: invoked?

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/record_invocation/record_invocation.rb', line 94

def __invoked?(method_name=nil, **parameters)
  if method_name.nil? && parameters.empty?
    return !__records.empty?
  end

  if not parameters.key?(:once)
    parameters[:once] = false
  end

  invocation = __invocation(method_name, **parameters)
  !invocation.nil?
end

#__invoked_once?(method_name, **parameters) ⇒ Boolean Also known as: invoked_once?

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/record_invocation/record_invocation.rb', line 108

def __invoked_once?(method_name, **parameters)
  parameters[:once] = true
  __invoked?(method_name, **parameters)
end

#__one_invocation(method_name, **parameters) ⇒ Object Also known as: one_invocation



61
62
63
64
# File 'lib/record_invocation/record_invocation.rb', line 61

def __one_invocation(method_name, **parameters)
  parameters[:once] = true
  __invocation(method_name, **parameters)
end

#__record(invocation) ⇒ Object Also known as: record



27
28
29
30
# File 'lib/record_invocation/record_invocation.rb', line 27

def __record(invocation)
  __records << invocation
  records
end

#__record_invocation(invocation_or_binding) ⇒ Object Also known as: record_invocation



33
34
35
36
37
38
39
40
# File 'lib/record_invocation/record_invocation.rb', line 33

def __record_invocation(invocation_or_binding)
  if invocation_or_binding.is_a?(Binding)
    invocation = Invocation.build(invocation_or_binding)
  end

  __record(invocation)
  invocation
end

#__recordsObject Also known as: records



22
23
24
# File 'lib/record_invocation/record_invocation.rb', line 22

def __records
  @__records ||= []
end