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.
-
#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 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) ⇒ 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) ⇒ Config
Create a new Config.
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_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 |
#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 | GEM_NAME. |
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.
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 |
#hash ⇒ Integer
Hash based on all attributes (for use as Hash key / grammar cache).
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.
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 |