class Definition
Represents a Ruby-specific definition extracted from source code.
Definitions
def initialize(*arguments, visibility: nil, node: nil, **options)
Initialize the definition from the syntax tree node.
Signature
-
parameter
arguments
Array
Arguments passed to the parent class.
-
parameter
visibility
Symbol
The visibility of the definition (:public, :private, :protected).
-
parameter
node
Parser::AST::Node
The syntax tree node representing this definition.
-
parameter
options
Hash
Additional options passed to the parent class.
Implementation
def initialize(*arguments, visibility: nil, node: nil, **options)
super(*arguments, **options)
@visibility = visibility
@node = node
end
attr :node
The parser syntax tree node.
Signature
-
attribute
Parser::AST::Node
The AST node representing this definition.
attr_accessor :visibility
The visibility of the definition.
Signature
-
attribute
Symbol
The visibility level (:public, :private, or :protected).
def public?
Check if this definition is public.
Signature
-
returns
Boolean
True if the definition is public.
Implementation
def public?
@visibility == :public
end
def multiline?
Check if this definition spans multiple lines.
Signature
-
returns
Boolean
True if the definition spans multiple lines.
Implementation
def multiline?
@node.location.start_line != @node.location.end_line
end
def text
The source code associated with the definition.
Signature
-
returns
String
Implementation
def text
location = @node.location
source_text = location.slice_lines
lines = source_text.split("\n")
if lines.count == 1
return lines.first
else
# Get the indentation from the first line of the node in the original source
if indentation = source_text[/\A\s+/]
# Remove the base indentation from all lines
lines.each{|line| line.sub!(indentation, "")}
end
return lines.join("\n")
end
end
def location
Get the location of this definition.
Signature
-
returns
Location | Nil
The location object if source is available.
Implementation
def location
if @source and location = @node&.location
Location.new(@source.path, location.start_line)
end
end