Class: Token::Resolver::Config
- Inherits:
-
Object
- Object
- Token::Resolver::Config
- 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.
Instance Attribute Summary collapse
-
#max_segments ⇒ Integer?
readonly
Maximum number of segments (nil = unlimited).
-
#min_segments ⇒ Integer
readonly
Minimum number of segments for a valid token.
-
#post ⇒ String
readonly
Closing delimiter for tokens.
-
#pre ⇒ String
readonly
Opening delimiter for tokens.
-
#segment_pattern ⇒ String
readonly
Parslet-compatible character class for segment content.
-
#separators ⇒ Array<String>
readonly
Separators between segments (used sequentially; last repeats).
Class Method Summary collapse
-
.default ⇒ Config
Default config suitable for kettle-jem style tokens like KJ|GEM_NAME.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
Equality based on all attributes.
-
#hash ⇒ Integer
Hash based on all attributes (for use as Hash key / grammar cache).
-
#initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]") ⇒ Config
constructor
Create a new Config.
-
#separator_at(index) ⇒ String
Get the separator for a given boundary index.
Constructor Details
#initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]") ⇒ Config
Create a new Config.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/token/resolver/config.rb', line 63 def initialize(pre: "{", post: "}", separators: ["|"], min_segments: 2, max_segments: nil, segment_pattern: "[A-Za-z0-9_]") 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 @segment_pattern = segment_pattern.dup.freeze freeze end |
Instance Attribute Details
#max_segments ⇒ Integer? (readonly)
Returns Maximum number of segments (nil = unlimited).
45 46 47 |
# File 'lib/token/resolver/config.rb', line 45 def max_segments @max_segments end |
#min_segments ⇒ Integer (readonly)
Returns 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 |
#post ⇒ String (readonly)
Returns Closing delimiter for tokens.
36 37 38 |
# File 'lib/token/resolver/config.rb', line 36 def post @post end |
#pre ⇒ String (readonly)
Returns Opening delimiter for tokens.
33 34 35 |
# File 'lib/token/resolver/config.rb', line 33 def pre @pre end |
#segment_pattern ⇒ String (readonly)
Returns Parslet-compatible character class for segment content. Only characters matching this pattern are valid inside a token segment. Default: ‘“[A-Za-z0-9_]”` (word characters — no spaces, no operators).
50 51 52 |
# File 'lib/token/resolver/config.rb', line 50 def segment_pattern @segment_pattern end |
#separators ⇒ Array<String> (readonly)
Returns 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
.default ⇒ Config
Default config suitable for kettle-jem style tokens like KJ|GEM_NAME.
80 81 82 |
# File 'lib/token/resolver/config.rb', line 80 def default @default ||= new # rubocop:disable ThreadSafety/ClassInstanceVariable end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
Equality based on all attributes.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/token/resolver/config.rb', line 89 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 && segment_pattern == other.segment_pattern end |
#hash ⇒ Integer
Hash based on all attributes (for use as Hash key / grammar cache).
105 106 107 |
# File 'lib/token/resolver/config.rb', line 105 def hash [pre, post, separators, min_segments, max_segments, segment_pattern].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.
115 116 117 118 119 120 121 |
# File 'lib/token/resolver/config.rb', line 115 def separator_at(index) if index < separators.length separators[index] else separators.last end end |