Class: Decode::Language::Ruby::Method

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

Overview

A Ruby-specific method.

Direct Known Subclasses

Function

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, #visibility

Instance Method Summary collapse

Methods inherited from Definition

#:public, :private, :protected=, #The parent definition, defining lexical scope.=, #The symbol name e.g. `:Decode`.=, #container?, #coverage_relevant?, #documentation, #documented?, #full_path, #inspect, #location, #multiline?, #name, #nested?, #public?, #start_with?, #text

Constructor Details

#initialize(*arguments, receiver: nil, **options) ⇒ Method

Initialize a new method definition.



17
18
19
20
# File 'lib/decode/language/ruby/method.rb', line 17

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

Instance Attribute Details

#receiverObject (readonly)

Returns the value of attribute receiver.



22
23
24
# File 'lib/decode/language/ruby/method.rb', line 22

def receiver
  @receiver
end

Instance Method Details

#arguments_nodeObject

The node which contains the function arguments.



44
45
46
# File 'lib/decode/language/ruby/method.rb', line 44

def arguments_node
	@node.parameters
end

#convert(kind) ⇒ Object

Convert the method to a different kind of definition.



81
82
83
84
85
86
87
88
89
90
# File 'lib/decode/language/ruby/method.rb', line 81

def convert(kind)
	case kind
	when :attribute
		Attribute.new(@node, @name,
			comments: @comments, parent: @parent, language: @language
		)
	else
		super
	end
end

#long_formObject

The long form of the method. e.g. ‘def puts(*lines, separator: “n”)` or `def self.puts(*lines, separator: “n”)`.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/decode/language/ruby/method.rb', line 50

def long_form
	if arguments_node = self.arguments_node
		if @receiver
			"def #{@receiver}.#{@node.name}(#{arguments_node.location.slice})"
		else
			"def #{@node.name}(#{arguments_node.location.slice})"
		end
	else
		self.short_form
	end
end

#nested_nameObject

Generate a nested name for the method.



25
26
27
28
29
30
31
# File 'lib/decode/language/ruby/method.rb', line 25

def nested_name
	if @receiver
		".#{self.name}"
	else
		"##{self.name}"
	end
end

#qualified_formObject

The fully qualified name of the block. e.g. ‘::Barnyard#foo`.



64
65
66
# File 'lib/decode/language/ruby/method.rb', line 64

def qualified_form
	self.qualified_name
end

#qualified_nameObject

Override the qualified_name method to handle method name joining correctly



69
70
71
72
73
74
75
76
77
# File 'lib/decode/language/ruby/method.rb', line 69

def qualified_name
	@qualified_name ||= begin
		if @parent
			[@parent.qualified_name, self.nested_name].join("")
		else
			self.nested_name
		end
	end
end

#short_formObject

The short form of the method. e.g. ‘def puts` or `def self.puts`.



35
36
37
38
39
40
41
# File 'lib/decode/language/ruby/method.rb', line 35

def short_form
	if @receiver
		"def #{@receiver}.#{@node.name}"
	else
		"def #{@node.name}"
	end
end