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 definitions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Reference

Initialize the reference.

Parameters:

  • text (String)

    The text text of the reference.



28
29
30
31
32
33
# File 'lib/decode/language/ruby/reference.rb', line 28

def initialize(text)
  @text = text
  
  @lexical_path = nil
  @path = nil
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



39
40
41
# File 'lib/decode/language/ruby/reference.rb', line 39

def text
  @text
end

Instance Method Details

#absolute?Boolean

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

Returns:

  • (Boolean)


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

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

#best(definitions) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/decode/language/ruby/reference.rb', line 50

def best(definitions)
  prefix, name = lexical_path.last
  
  definitions.select do |definition|
    prefix.nil? || definition.start_with?(prefix)
  end
end

#languageObject



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

def language
  Ruby
end

#lexical_pathObject



46
47
48
# File 'lib/decode/language/ruby/reference.rb', line 46

def lexical_path
  @lexical_path ||= @text.scan(/(::|\.|#|:)?([^:.#]+)/)
end

#pathArray(String)

The lexical path of the reference.

Returns:

  • (Array(String))


60
61
62
# File 'lib/decode/language/ruby/reference.rb', line 60

def path
  @path ||= self.lexical_path.map{|_, name| name.to_sym}
end