Class: Betamax::MethodRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/betamax/method_recorder.rb

Constant Summary collapse

PRIMITIVE_TYPES =
[Integer, Float, String, Symbol, TrueClass, FalseClass, NilClass].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, recording: []) ⇒ MethodRecorder

Returns a new instance of MethodRecorder.



7
8
9
10
# File 'lib/betamax/method_recorder.rb', line 7

def initialize object:, recording: []
  @object = object
  @recording = recording
end

Instance Attribute Details

#recordingObject (readonly)

Returns the value of attribute recording.



5
6
7
# File 'lib/betamax/method_recorder.rb', line 5

def recording
  @recording
end

Instance Method Details

#call(method_name, *args, **kwargs) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/betamax/method_recorder.rb', line 12

def call(method_name, *args, **kwargs, &) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  block_yieldings = []

  result = if block_given?
             @object.send(method_name, *args, **kwargs) do |*block_args, **block_kwargs|
               wrapped_args = block_args.map { |arg| wrap_object arg }
               wrapped_kwargs = block_kwargs.transform_values { |value| wrap_object value }

               yielding = RecordedYielding.new args: wrapped_args, kwargs: wrapped_kwargs
               block_yieldings << yielding

               yield(*wrapped_args, **wrapped_kwargs)
             end
           else
             @object.send(method_name, *args, **kwargs, &)
           end

  wrapped_result = wrap_object result

  @recording << RecordedMethod.new(
    method_name:,
    args:,
    kwargs:,
    block_given: block_given?,
    block_yieldings:,
    result: wrapped_result
  )

  wrapped_result
end