Class: Decode::Language::Reference
- Inherits:
-
Object
- Object
- Decode::Language::Reference
- Defined in:
- lib/decode/language/reference.rb
Overview
An reference which can be resolved to zero or more definitions.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
The identifier part of the reference.
-
#language ⇒ Object
readonly
The language associated with this reference.
Instance Method Summary collapse
-
#absolute? ⇒ Boolean
Whether the reference starts at the base of the lexical tree.
- #best(definitions) ⇒ Object
-
#initialize(identifier, language) ⇒ Reference
constructor
Initialize the reference.
- #lexical_path ⇒ Object
-
#path ⇒ Array(String)
The lexical path of the reference.
- #relative? ⇒ Boolean
- #split(identifier) ⇒ Object
Constructor Details
#initialize(identifier, language) ⇒ Reference
Initialize the reference.
27 28 29 30 31 32 33 |
# File 'lib/decode/language/reference.rb', line 27 def initialize(identifier, language) @identifier = identifier @language = language @lexical_path = nil @path = nil end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
The identifier part of the reference.
37 38 39 |
# File 'lib/decode/language/reference.rb', line 37 def identifier @identifier end |
#language ⇒ Object (readonly)
The language associated with this reference.
40 41 42 |
# File 'lib/decode/language/reference.rb', line 40 def language @language end |
Instance Method Details
#absolute? ⇒ Boolean
Whether the reference starts at the base of the lexical tree.
43 44 45 |
# File 'lib/decode/language/reference.rb', line 43 def absolute? !self.relative? end |
#best(definitions) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/decode/language/reference.rb', line 61 def best(definitions) prefix, name = lexical_path.last definitions.select do |definition| definition.language == @language && ( prefix.nil? || definition.start_with?(prefix) ) end end |
#lexical_path ⇒ Object
57 58 59 |
# File 'lib/decode/language/reference.rb', line 57 def lexical_path @lexical_path ||= self.split(@identifier) end |
#path ⇒ Array(String)
The lexical path of the reference.
73 74 75 |
# File 'lib/decode/language/reference.rb', line 73 def path @path ||= self.lexical_path.map{|_, name| name.to_sym} end |
#relative? ⇒ Boolean
47 48 49 50 51 |
# File 'lib/decode/language/reference.rb', line 47 def relative? prefix, name = self.lexical_path.first return prefix.nil? end |
#split(identifier) ⇒ Object
53 54 55 |
# File 'lib/decode/language/reference.rb', line 53 def split(identifier) identifier.scan(/(\W+)?(\w+)/) end |