Class: Rubyzen::Declarations::CallSiteDeclaration

Inherits:
Object
  • Object
show all
Includes:
Providers::ClassNameProvider, Providers::FilePathProvider, Providers::LineNumberProvider, Providers::SourceCodeProvider
Defined in:
lib/rubyzen/declarations/call_site_declaration.rb

Overview

Represents a method call site (a send node in the AST).

Examples:

call_site = method.call_sites.first
call_site.method_name   #=> "find"
call_site.receiver      #=> "User"
call_site.keyword_args  #=> [:id, :name]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers::SourceCodeProvider

#source_code

Methods included from Providers::ClassNameProvider

#class_name

Methods included from Providers::LineNumberProvider

#line

Methods included from Providers::FilePathProvider

#file_path

Constructor Details

#initialize(node, parent) ⇒ CallSiteDeclaration

Returns a new instance of CallSiteDeclaration.

Parameters:



25
26
27
28
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 25

def initialize(node, parent)
  @node = node
  @parent = parent
end

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


18
19
20
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 18

def node
  @node
end

#parentMethodDeclaration, ... (readonly)



21
22
23
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 21

def parent
  @parent
end

Instance Method Details

#keyword_arg_value_pairsHash{Symbol => Object}

Returns a hash mapping keyword argument keys to their literal values.

Returns:

  • (Hash{Symbol => Object})

    values are nil for non-literal expressions



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 67

def keyword_arg_value_pairs
  result = {}
  node.arguments.each do |arg|
    next unless arg.hash_type?

    arg.pairs.each do |pair|
      next unless pair.key.type == :sym

      value_node = pair.value
      result[pair.key.value] = value_node.respond_to?(:value) ? value_node.value : nil
    end
  end
  result
end

#keyword_argsArray<Symbol>

Returns the keyword argument keys passed in the call.

Returns:

  • (Array<Symbol>)

    e.g. [:level, :details]



54
55
56
57
58
59
60
61
62
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 54

def keyword_args
  node.arguments.flat_map do |arg|
    next [] unless arg.hash_type?

    arg.pairs.filter_map do |pair|
      pair.key.value if pair.key.type == :sym
    end
  end.uniq
end

#method_nameString

Returns the called method name.

Returns:

  • (String)


47
48
49
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 47

def method_name
  node.method_name.to_s
end

#nameString

Returns the called method name. Alias for #method_name.

Returns:

  • (String)


33
34
35
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 33

def name
  method_name
end

#receiverString?

Returns the constant name of the receiver, if any.

Returns:

  • (String, nil)

    e.g. “User” for User.find(1), nil for save



40
41
42
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 40

def receiver
  node.receiver&.type == :const ? node.receiver.const_name : nil
end

#stringsArray<String>

Returns positional string arguments.

Returns:

  • (Array<String>)


92
93
94
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 92

def strings
  node.arguments.select { |arg| arg.type == :str }.map(&:value)
end

#symbolsArray<Symbol>

Returns positional symbol arguments.

Returns:

  • (Array<Symbol>)

    e.g. [:name, :email]



85
86
87
# File 'lib/rubyzen/declarations/call_site_declaration.rb', line 85

def symbols
  node.arguments.select { |arg| arg.type == :sym }.map(&:value)
end