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.
35 36 37 38 39 |
# File 'lib/token/resolver/node/token.rb', line 35 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.
31 32 33 |
# File 'lib/token/resolver/node/token.rb', line 31 def config @config end |
#segments ⇒ Array<String> (readonly)
Returns The token segments.
28 29 30 |
# File 'lib/token/resolver/node/token.rb', line 28 def segments @segments end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
Equality based on segments and config.
88 89 90 |
# File 'lib/token/resolver/node/token.rb', line 88 def eql?(other) other.is_a?(Token) && segments == other.segments && config == other.config end |
#hash ⇒ Integer
95 96 97 |
# File 'lib/token/resolver/node/token.rb', line 95 def hash [self.class, segments, config].hash end |
#inspect ⇒ String
100 101 102 |
# File 'lib/token/resolver/node/token.rb', line 100 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".
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/token/resolver/node/token.rb', line 47 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").
63 64 65 |
# File 'lib/token/resolver/node/token.rb', line 63 def prefix @segments[0] end |
#text? ⇒ Boolean
80 81 82 |
# File 'lib/token/resolver/node/token.rb', line 80 def text? false end |
#to_s ⇒ String
Reconstruct the original token string with delimiters.
70 71 72 |
# File 'lib/token/resolver/node/token.rb', line 70 def to_s "#{@config.pre}#{key}#{@config.post}" end |
#token? ⇒ Boolean
75 76 77 |
# File 'lib/token/resolver/node/token.rb', line 75 def token? true end |