Class: Token::Resolver::Node::Token
- Inherits:
-
Object
- Object
- Token::Resolver::Node::Token
- Defined in:
- lib/token/resolver/node/token.rb
Overview
Represents a structured token found in the input.
A token consists of segments separated by configured separators,
wrapped in pre/post delimiters. For example, with default config,
{KJ|GEM_NAME} has segments ["KJ", "GEM_NAME"].
Token nodes are frozen after creation.
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
The config used to parse this token.
-
#segments ⇒ Array<String>
readonly
The token segments.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
Equality based on segments and config.
-
#hash ⇒ Integer
-
#initialize(segments, config) ⇒ Token
constructor
A new instance of Token.
-
#inspect ⇒ String
-
#key ⇒ String
The canonical key for this token, suitable for use as a replacement map key.
-
#prefix ⇒ String
The first segment (typically a prefix/namespace like “KJ”).
-
#text? ⇒ Boolean
-
#to_s ⇒ String
Reconstruct the original token string with delimiters.
-
#token? ⇒ Boolean
Constructor Details
#initialize(segments, config) ⇒ Token
Returns a new instance of Token.
36 37 38 39 40 |
# File 'lib/token/resolver/node/token.rb', line 36 def initialize(segments, config) @segments = segments.map { |s| s.frozen? ? s : s.dup.freeze }.freeze @config = config freeze end |
Instance Attribute Details
#config ⇒ Config (readonly)
Returns The config used to parse this token.
32 33 34 |
# File 'lib/token/resolver/node/token.rb', line 32 def config @config end |
#segments ⇒ Array<String> (readonly)
Returns The token segments.
29 30 31 |
# File 'lib/token/resolver/node/token.rb', line 29 def segments @segments end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
Equality based on segments and config.
89 90 91 |
# File 'lib/token/resolver/node/token.rb', line 89 def eql?(other) other.is_a?(Token) && segments == other.segments && config == other.config end |
#hash ⇒ Integer
96 97 98 |
# File 'lib/token/resolver/node/token.rb', line 96 def hash [self.class, segments, config].hash end |
#inspect ⇒ String
101 102 103 |
# File 'lib/token/resolver/node/token.rb', line 101 def inspect "#<#{self.class} #{to_s.inspect} segments=#{segments.inspect}>" end |
#key ⇒ String
The canonical key for this token, suitable for use as a replacement map key.
Joins segments using the actual separators in order. For separators: ["|", ":"]
and segments ["KJ", "SECTION", "NAME"], returns "KJ|SECTION:NAME".
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/token/resolver/node/token.rb', line 48 def key return @segments[0] if @segments.length == 1 result = +"" @segments.each_with_index do |seg, i| if i > 0 result << @config.separator_at(i - 1) end result << seg end result.freeze end |
#prefix ⇒ String
The first segment (typically a prefix/namespace like “KJ”).
64 65 66 |
# File 'lib/token/resolver/node/token.rb', line 64 def prefix @segments[0] end |
#text? ⇒ Boolean
81 82 83 |
# File 'lib/token/resolver/node/token.rb', line 81 def text? false end |
#to_s ⇒ String
Reconstruct the original token string with delimiters.
71 72 73 |
# File 'lib/token/resolver/node/token.rb', line 71 def to_s "#{@config.pre}#{key}#{@config.post}" end |
#token? ⇒ Boolean
76 77 78 |
# File 'lib/token/resolver/node/token.rb', line 76 def token? true end |