Method: AppMap::Event::MethodCall.build_from_invocation

Defined in:
lib/appmap/event.rb

.build_from_invocation(defined_class, method, receiver, arguments, event: MethodCall.new) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/appmap/event.rb', line 104

def build_from_invocation(defined_class, method, receiver, arguments, event: MethodCall.new)
  event ||= MethodCall.new
  defined_class ||= 'Class'
  event.tap do
    static = receiver.is_a?(Module)
    event.defined_class = defined_class
    event.method_id = method.name.to_s
    if method.source_location
      path = method.source_location[0]
      path = path[Dir.pwd.length + 1..-1] if path.index(Dir.pwd) == 0
      event.path = path
      event.lineno = method.source_location[1]
    else
      event.path = [ defined_class, static ? '.' : '#', method.name ].join
    end

    # Check if the method has key parameters. If there are any they'll always be last.
    # If yes, then extract it from arguments.
    has_key = [[:dummy], *method.parameters].last.first.to_s.start_with?('key') && arguments[-1].is_a?(Hash)
    kwargs = has_key && arguments[-1].dup || {}

    event.parameters = method.parameters.map.with_index do |method_param, idx|
      param_type, param_name = method_param
      param_name ||= 'arg'
      value = case param_type
        when :keyrest
          kwargs
        when /^key/
          kwargs.delete param_name
        when :rest
          arguments[idx..(has_key ? -2 : -1)]
        else
          arguments[idx]
        end
      {
        name: param_name,
        class: best_class_name(value),
        object_id: value.__id__,
        value: display_string(value),
        kind: param_type
      }
    end
    event.receiver = {
      class: best_class_name(receiver),
      object_id: receiver.__id__,
      value: display_string(receiver)
    }
    event.static = static
    MethodEvent.build_from_invocation(:call, event: event)
  end
end