Class: Decode::Language::Ruby::Definition

Inherits:
Definition
  • Object
show all
Defined in:
lib/decode/language/ruby/definition.rb

Overview

Represents a Ruby-specific definition extracted from source code.

Instance Attribute Summary collapse

Attributes inherited from Definition

#The comment lines which directly preceeded the definition., #The language the symbol is defined within., #The path to the definition, relative to the parent., #The source file containing this definition., #comments, #language, #parent, #path, #source

Instance Method Summary collapse

Methods inherited from Definition

#:public, :private, :protected=, #The parent definition, defining lexical scope.=, #The symbol name e.g. `:Decode`.=, #container?, #convert, #coverage_relevant?, #documentation, #documented?, #full_path, #inspect, #long_form, #name, #nested?, #nested_name, #qualified_form, #qualified_name, #short_form, #start_with?

Constructor Details

#initialize(*arguments, visibility: nil, node: nil, **options) ⇒ Definition

Initialize the definition from the syntax tree node.



18
19
20
21
22
23
# File 'lib/decode/language/ruby/definition.rb', line 18

def initialize(*arguments, visibility: nil, node: nil, **options)
	super(*arguments, **options)
	
	@visibility = visibility
	@node = node
end

Instance Attribute Details

#nodeObject (readonly)

The parser syntax tree node.



27
28
29
# File 'lib/decode/language/ruby/definition.rb', line 27

def node
  @node
end

#The AST node representing this definition.(ASTnoderepresentingthisdefinition.) ⇒ Object (readonly)

The parser syntax tree node.



27
# File 'lib/decode/language/ruby/definition.rb', line 27

attr :node

#visibilityObject

The visibility of the definition.



31
32
33
# File 'lib/decode/language/ruby/definition.rb', line 31

def visibility
  @visibility
end

Instance Method Details

#locationObject

Get the location of this definition.



79
80
81
82
83
# File 'lib/decode/language/ruby/definition.rb', line 79

def location
	if @source and location = @node&.location
		Location.new(@source.path, location.start_line)
	end
end

#multiline?Boolean

Check if this definition spans multiple lines.



53
54
55
# File 'lib/decode/language/ruby/definition.rb', line 53

def multiline?
	@node.location.start_line != @node.location.end_line
end

#private?Boolean

Check if this definition is private.



47
48
49
# File 'lib/decode/language/ruby/definition.rb', line 47

def private?
	@visibility == :private
end

#protected?Boolean

Check if this definition is protected.



41
42
43
# File 'lib/decode/language/ruby/definition.rb', line 41

def protected?
	@visibility == :protected
end

#public?Boolean

Check if this definition is public.



35
36
37
# File 'lib/decode/language/ruby/definition.rb', line 35

def public?
	@visibility == :public
end

#textObject

The source code associated with the definition.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/decode/language/ruby/definition.rb', line 59

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

#The visibility level (:public, :private, or :protected).=(visibilitylevel(: public, :private,) ⇒ Object

The visibility of the definition.



31
# File 'lib/decode/language/ruby/definition.rb', line 31

attr_accessor :visibility