Class: Decode::Language::Ruby::Reference

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

Overview

An Ruby-specific reference which can be resolved to zero or more symbols.

Constant Summary collapse

KIND =
{
	':' => :def,
	'.' => :defs,
}.freeze
METHOD =
/\A(?<scope>.*?)?(?<kind>:|\.)(?<name>.+?)\z/

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Reference

Initialize the reference.

Parameters:

  • value (String)

    The string value of the reference.



33
34
35
36
37
38
# File 'lib/decode/language/ruby/reference.rb', line 33

def initialize(value)
	@value = value
	
	@path = nil
	@kind = nil
end

Instance Method Details

#absolute?Boolean

Whether the reference starts at the base of the lexical tree.

Returns:

  • (Boolean)


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

def absolute?
	@value.start_with?('::')
end

#kindObject

The kind of symbol to match.



75
76
77
78
79
# File 'lib/decode/language/ruby/reference.rb', line 75

def kind
	self.path
	
	return @kind
end

#pathArray(String)

The lexical path of the reference.

Returns:

  • (Array(String))


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/decode/language/ruby/reference.rb', line 49

def path
	if @path.nil?
		@path = @value.split(/::/)
		
		if last = @path.pop
			if match = last.match(METHOD)
				@kind = KIND[match[:kind]]
				
				if scope = match[:scope]
					@path << scope
				end
				
				@path << match[:name]
			else
				@path << last
			end
		end
		
		@path = @path.map(&:to_sym)
		@path.freeze
	end
	
	return @path
end