Class: Token::Resolver::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/token/resolver/config.rb

Overview

Configuration object defining the token structure for parsing.

A Config describes what tokens look like: their opening/closing delimiters,
segment separators, and segment count constraints. Configs are frozen after
initialization and implement #hash/#eql? for grammar caching.

Examples:

Default config (tokens like Y)
config = Token::Resolver::Config.default
config.pre          # => "{"
config.post         # => "}"
config.separators   # => ["|"]
config.min_segments # => 2

Custom config (tokens like «X:Y»)

config = Token::Resolver::Config.new(
  pre: "<<",
  post: ">>",
  separators: [":"],
)
Multi-separator config (tokens like SECTION:NAME)
config = Token::Resolver::Config.new(
  separators: ["|", ":"],
)
# First boundary uses "|", second uses ":", rest repeat ":"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil) ⇒ Config

Create a new Config.

Parameters:

  • pre (String) (defaults to: "{")

    Opening delimiter (default: “{“)

  • post (String) (defaults to: "}")

    Closing delimiter (default: “}”)

  • separators (Array<String>) (defaults to: ["|"])
    Segment separators (default: [” ”])
  • min_segments (Integer) (defaults to: 2)

    Minimum segment count (default: 2)

  • max_segments (Integer, nil) (defaults to: nil)

    Maximum segment count (default: nil)

Raises:

  • (ArgumentError)

    If any delimiter is empty or constraints are invalid



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/token/resolver/config.rb', line 56

def initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil)
  validate!(pre, post, separators, min_segments, max_segments)

  @pre = pre.dup.freeze
  @post = post.dup.freeze
  @separators = separators.map { |s| s.dup.freeze }.freeze
  @min_segments = min_segments
  @max_segments = max_segments

  freeze
end

Instance Attribute Details

#max_segmentsInteger? (readonly)

Returns Maximum number of segments (nil = unlimited).

Returns:

  • (Integer, nil)

    Maximum number of segments (nil = unlimited)



45
46
47
# File 'lib/token/resolver/config.rb', line 45

def max_segments
  @max_segments
end

#min_segmentsInteger (readonly)

Returns Minimum number of segments for a valid token.

Returns:

  • (Integer)

    Minimum number of segments for a valid token



42
43
44
# File 'lib/token/resolver/config.rb', line 42

def min_segments
  @min_segments
end

#postString (readonly)

Returns Closing delimiter for tokens.

Returns:

  • (String)

    Closing delimiter for tokens



36
37
38
# File 'lib/token/resolver/config.rb', line 36

def post
  @post
end

#preString (readonly)

Returns Opening delimiter for tokens.

Returns:

  • (String)

    Opening delimiter for tokens



33
34
35
# File 'lib/token/resolver/config.rb', line 33

def pre
  @pre
end

#separatorsArray<String> (readonly)

Returns Separators between segments (used sequentially; last repeats).

Returns:

  • (Array<String>)

    Separators between segments (used sequentially; last repeats)



39
40
41
# File 'lib/token/resolver/config.rb', line 39

def separators
  @separators
end

Class Method Details

.defaultConfig

Default config suitable for kettle-jem style tokens like GEM_NAME.

Returns:



72
73
74
# File 'lib/token/resolver/config.rb', line 72

def default
  @default ||= new # rubocop:disable ThreadSafety/ClassInstanceVariable
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Equality based on all attributes.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
# File 'lib/token/resolver/config.rb', line 81

def eql?(other)
  return false unless other.is_a?(Config)

  pre == other.pre &&
    post == other.post &&
    separators == other.separators &&
    min_segments == other.min_segments &&
    max_segments == other.max_segments
end

#hashInteger

Hash based on all attributes (for use as Hash key / grammar cache).

Returns:

  • (Integer)


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

def hash
  [pre, post, separators, min_segments, max_segments].hash
end

#separator_at(index) ⇒ String

Get the separator for a given boundary index.

When there are more segment boundaries than separators, the last separator repeats.

Parameters:

  • index (Integer)

    Zero-based boundary index

Returns:

  • (String)

    The separator to use



106
107
108
109
110
111
112
# File 'lib/token/resolver/config.rb', line 106

def separator_at(index)
  if index < separators.length
    separators[index]
  else
    separators.last
  end
end