Class: Rucoa::NodeInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/rucoa/node_inspector.rb

Instance Method Summary collapse

Constructor Details

#initialize(definition_store:, node:) ⇒ NodeInspector

Returns a new instance of NodeInspector.

Parameters:



7
8
9
10
11
12
13
# File 'lib/rucoa/node_inspector.rb', line 7

def initialize(
  definition_store:,
  node:
)
  @definition_store = definition_store
  @node = node
end

Instance Method Details

#definitionsArray<Rucoa::Definitions::Base>

Returns:



16
17
18
19
20
21
22
23
24
25
# File 'lib/rucoa/node_inspector.rb', line 16

def definitions
  case @node
  when Nodes::ConstNode
    [constant_definition]
  when Nodes::SendNode
    method_definitions
  else
    []
  end
end

#method_definitionsArray<Rucoa::Definitions::MethodDefinition>



28
29
30
31
32
33
34
# File 'lib/rucoa/node_inspector.rb', line 28

def method_definitions
  method_receiver_types.flat_map do |type|
    @definition_store.instance_method_definitions_of(type)
  end.select do |method_definition|
    method_definition.method_name == @node.name
  end
end

#method_receiver_typesArray<String>

Returns:

  • (Array<String>)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rucoa/node_inspector.rb', line 37

def method_receiver_types
  return [] unless @node.is_a?(Nodes::SendNode)

  if @node.receiver
    self.class.new(
      definition_store: @definition_store,
      node: @node.receiver
    ).return_types
  else
    [@node.namespace]
  end
end

#return_typesArray<String>

Returns:

  • (Array<String>)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rucoa/node_inspector.rb', line 51

def return_types
  case @node.type
  when :const
    return_types_for_const
  when :lvar
    return_types_for_lvar
  when :send
    return_types_for_send
  when :array
    %w[Array]
  when :class, :module, :nil
    %w[NilClass]
  when :complex
    %w[Complex]
  when :def, :sym
    %w[Symbol]
  when :dstr, :str, :xstr
    %w[String]
  when :erange, :irange
    %w[Range]
  when false
    %w[FalseClass]
  when :float
    %w[Float]
  when :hash, :pair
    %w[Hash]
  when :int
    %w[Integer]
  when :rational
    %w[Rational]
  when :regexp, :regopt
    %w[Regexp]
  when true
    %w[TrueClass]
  else
    []
  end
end