Class: Rucoa::Nodes::DefNode

Inherits:
Base
  • Object
show all
Includes:
Rucoa::NodeConcerns::Body, Rucoa::NodeConcerns::Rescue
Defined in:
lib/rucoa/nodes/def_node.rb

Instance Method Summary collapse

Methods included from Rucoa::NodeConcerns::Rescue

#ensure, #rescue

Methods included from Rucoa::NodeConcerns::Body

#body, #body_children

Methods inherited from Base

#ancestors, #child_nodes, #descendant_nodes, #each_ancestor, #each_child_node, #each_descendant_node, #include_position?, #initialize, #module_nesting, #namespace, #next_sibling_nodes, #parent, #parent=, #previous_sibling_nodes, #updated

Constructor Details

This class inherits a constructor from Rucoa::Nodes::Base

Instance Method Details

#argumentsArray<Rucoa::Nodes::ArgNode>

Examples:

returns arguments

node = Rucoa::Source.new(
  content: <<~RUBY,
    def foo(bar, baz)
    end
  RUBY
  uri: 'file:///path/to/example.rb'
).root_node
expect(node.arguments.map(&:name)).to eq(%w[bar baz])

Returns:



19
20
21
# File 'lib/rucoa/nodes/def_node.rb', line 19

def arguments
  children[-2]
end

#method_markerString

Returns:

  • (String)


24
25
26
27
28
29
30
# File 'lib/rucoa/nodes/def_node.rb', line 24

def method_marker
  if singleton?
    '.'
  else
    '#'
  end
end

#nameString

Examples:

returns method name

node = Rucoa::Source.new(
  content: <<~RUBY,
    module Foo
      class Bar
        def baz
        end
      end
    end
  RUBY
  uri: 'file:///path/to/foo/bar.rb'
).node_at(
  Rucoa::Position.new(
    column: 4,
    line: 3
  )
)
expect(node.name).to eq('baz')

Returns:

  • (String)


51
52
53
# File 'lib/rucoa/nodes/def_node.rb', line 51

def name
  children[-3].to_s
end

#qualified_nameString

Examples:

returns full qualified name

node = Rucoa::Source.new(
  content: <<~RUBY,
    module Foo
      class Bar
        def baz
        end
      end
    end
  RUBY
  uri: 'file:///path/to/foo/bar.rb'
).node_at(
  Rucoa::Position.new(
    column: 4,
    line: 3
  )
)
expect(node.qualified_name).to eq('Foo::Bar#baz')

Returns:

  • (String)


74
75
76
77
78
79
80
# File 'lib/rucoa/nodes/def_node.rb', line 74

def qualified_name
  [
    namespace,
    method_marker,
    name
  ].join
end

#singleton?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rucoa/nodes/def_node.rb', line 83

def singleton?
  type == :defs || each_ancestor(:sclass).any?
end