Class: Token::Resolver::Node::Token

Inherits:
Object
  • Object
show all
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.

Examples:

Single separator

token = Token::Resolver::Node::Token.new(["KJ", "GEM_NAME"], config)
token.key       # => "KJ|GEM_NAME"
token.prefix    # => "KJ"
token.segments  # => ["KJ", "GEM_NAME"]
token.to_s      # => "{KJ|GEM_NAME}"

Sequential separators

config = Config.new(separators: ["|", ":"])
token = Token::Resolver::Node::Token.new(["KJ", "SECTION", "NAME"], config)
token.key       # => "KJ|SECTION:NAME"
token.to_s      # => "{KJ|SECTION:NAME}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments, config) ⇒ Token

Returns a new instance of Token.

Parameters:

  • segments (Array<String>)

    The token segments

  • config (Config)

    The config that defined this token’s structure



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

#configConfig (readonly)

Returns The config used to parse this token.

Returns:

  • (Config)

    The config used to parse this token



32
33
34
# File 'lib/token/resolver/node/token.rb', line 32

def config
  @config
end

#segmentsArray<String> (readonly)

Returns The token segments.

Returns:

  • (Array<String>)

    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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

#hashInteger

Returns:

  • (Integer)


96
97
98
# File 'lib/token/resolver/node/token.rb', line 96

def hash
  [self.class, segments, config].hash
end

#inspectString

Returns:

  • (String)


101
102
103
# File 'lib/token/resolver/node/token.rb', line 101

def inspect
  "#<#{self.class} #{to_s.inspect} segments=#{segments.inspect}>"
end

#keyString

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

Returns:

  • (String)


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

#prefixString

The first segment (typically a prefix/namespace like “KJ”).

Returns:

  • (String)


64
65
66
# File 'lib/token/resolver/node/token.rb', line 64

def prefix
  @segments[0]
end

#text?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/token/resolver/node/token.rb', line 81

def text?
  false
end

#to_sString

Reconstruct the original token string with delimiters.

Returns:

  • (String)


71
72
73
# File 'lib/token/resolver/node/token.rb', line 71

def to_s
  "#{@config.pre}#{key}#{@config.post}"
end

#token?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/token/resolver/node/token.rb', line 76

def token?
  true
end