Module: Simple::Service::Action::MethodReflection

Extended by:
MethodReflection
Included in:
MethodReflection
Defined in:
lib/simple/service/action/method_reflection.rb

Overview

rubocop:disable Metrics/AbcSize

Instance Method Summary collapse

Instance Method Details

#parameters(service, method_id) ⇒ Object

returns an array with entries like the following:

[ :key, name, default_value ]
[ :keyreq, name [, nil ] ]
[ :req, name [, nil ] ]
[ :opt, name [, nil ] ]
[ :rest, name [, nil ] ]


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
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple/service/action/method_reflection.rb', line 15

def parameters(service, method_id)
  method = service.instance_method(method_id)
  parameters = method.parameters

  # method parameters with a :key mode are optional named arguments. We only
  # support defaults for those - if there are none we abort here already.
  keys = parameters.map { |mode, name| name if mode == :key }.compact
  return parameters if keys.empty?

  # We are now doing a fake call to the method, with a minimal viable set of
  # arguments, to let the ruby runtime fill in default values for arguments.
  # We do not, however, let the call complete. Instead we use a TracePoint to
  # abort as soon as the method is called, and use the its binding to determine
  # the default values.

  fake_recipient = Object.new.extend(service)
  fake_call_args = minimal_arguments(method)

  trace_point = TracePoint.trace(:call) do |tp|
    throw :received_fake_call, tp.binding if tp.defined_class == service && tp.method_id == method_id
  end

  bnd = catch(:received_fake_call) do
    fake_recipient.send(method_id, *fake_call_args)
  end

  trace_point.disable

  # extract default values from the received binding, and merge with the
  # parameters array.
  default_values = keys.each_with_object({}) do |key_parameter, hsh|
    hsh[key_parameter] = bnd.local_variable_get(key_parameter)
  end

  parameters.map do |mode, name|
    [mode, name, default_values[name]]
  end
end