Class: Mustermann::Match
- Inherits:
-
Object
- Object
- Mustermann::Match
- Defined in:
- lib/mustermann/match.rb
Overview
The return value of Pattern#match, Pattern#peek_match, Set#match, and similar methods. Mimics large parts of the MatchData API, but also provides access to the pattern and params hash.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#captures ⇒ Array
readonly
The captures array.
-
#named_captures ⇒ Hash
readonly
The named captures hash, usually identical to #params.
-
#params ⇒ Hash
(also: #to_h)
readonly
The params hash.
-
#pattern ⇒ Mustermann::Pattern
readonly
The pattern that produced the match.
-
#post_match ⇒ String
readonly
The post match string.
-
#pre_match ⇒ String
readonly
The pre match string.
-
#regexp ⇒ Regexp?
readonly
The regular expression that produced the match, if available.
-
#string ⇒ String
readonly
The string that was matched.
Instance Method Summary collapse
- #[](key, length = nil) ⇒ Object
-
#deconstruct_keys(keys) ⇒ Hash
Deconstructs the match into a hash of the given keys.
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(pattern_or_match, string_or_match = nil, matched: nil, params: nil, post_match: nil, pre_match: nil, captures: nil, named_captures: nil, regexp: nil) ⇒ Match
constructor
A new instance of Match.
-
#names ⇒ Array<String>
The names of the named captures.
-
#to_s ⇒ String
The matched substring (like MatchData#to_s).
-
#values_at(*keys) ⇒ Array
Returns the values of the given keys as an array.
Constructor Details
#initialize(pattern, string, **options) ⇒ Match #initialize(match, **options) ⇒ Match #initialize(pattern, match, **options) ⇒ Match
Returns a new instance of Match.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mustermann/match.rb', line 48 def initialize(pattern_or_match, string_or_match = nil, matched: nil, params: nil, post_match: nil, pre_match: nil, captures: nil, named_captures: nil, regexp: nil) case pattern_or_match when Mustermann::Match, MatchData then match = pattern_or_match when Mustermann::Pattern then pattern = pattern_or_match else raise ArgumentError, "first argument must be a Mustermann::Pattern or a MatchData, not #{pattern_or_match.class}" end case string_or_match when Mustermann::Match, MatchData then match ||= string_or_match when String then string = string_or_match when nil # ignore else raise ArgumentError, "second argument must be a String or a MatchData, not #{string_or_match.class}" end @pattern = pattern || match&.pattern @string = string || match&.string || '' @params = params || match&.params || {} @post_match = post_match || match&.post_match || '' @pre_match = pre_match || match&.pre_match || '' @captures = captures || match&.captures || @params.values @named_captures = named_captures || match&.named_captures || @params @matched = matched || match&.to_s || @string unless @regexp = regexp @regexp = match.regexp if match.respond_to?(:regexp) @regexp ||= pattern.respond_to?(:regexp) ? pattern.regexp : nil end end |
Instance Attribute Details
#captures ⇒ Array (readonly)
Returns the captures array.
17 18 19 |
# File 'lib/mustermann/match.rb', line 17 def captures @captures end |
#named_captures ⇒ Hash (readonly)
Returns the named captures hash, usually identical to #params.
20 21 22 |
# File 'lib/mustermann/match.rb', line 20 def named_captures @named_captures end |
#params ⇒ Hash (readonly) Also known as: to_h
Returns the params hash.
14 15 16 |
# File 'lib/mustermann/match.rb', line 14 def params @params end |
#pattern ⇒ Mustermann::Pattern (readonly)
Returns the pattern that produced the match.
8 9 10 |
# File 'lib/mustermann/match.rb', line 8 def pattern @pattern end |
#post_match ⇒ String (readonly)
Returns the post match string.
23 24 25 |
# File 'lib/mustermann/match.rb', line 23 def post_match @post_match end |
#pre_match ⇒ String (readonly)
Returns the pre match string.
26 27 28 |
# File 'lib/mustermann/match.rb', line 26 def pre_match @pre_match end |
#regexp ⇒ Regexp? (readonly)
Returns the regular expression that produced the match, if available.
29 30 31 |
# File 'lib/mustermann/match.rb', line 29 def regexp @regexp end |
#string ⇒ String (readonly)
Returns the string that was matched.
11 12 13 |
# File 'lib/mustermann/match.rb', line 11 def string @string end |
Instance Method Details
#[](key) ⇒ Object #[](index) ⇒ Object #[](start, length) ⇒ Array #[](range) ⇒ Array
100 101 102 103 104 105 106 107 108 |
# File 'lib/mustermann/match.rb', line 100 def [](key, length = nil) case key when String then named_captures[key] when Symbol then named_captures[key.to_s] when Integer then length ? captures[key, length] : captures[key] when Range then captures[key] else raise ArgumentError, "key must be a String, Symbol, Integer, or Range, not #{key.class}" end end |
#deconstruct_keys(keys) ⇒ Hash
Deconstructs the match into a hash of the given keys. Useful for pattern matching.
114 |
# File 'lib/mustermann/match.rb', line 114 def deconstruct_keys(keys) = keys.to_h { |key| [key, self[key]] } |
#eql?(other) ⇒ Boolean Also known as: ==
120 121 122 123 |
# File 'lib/mustermann/match.rb', line 120 def eql?(other) return false unless other.is_a? self.class pattern == other.pattern && string == other.string && params == other.params end |
#hash ⇒ Object
117 |
# File 'lib/mustermann/match.rb', line 117 def hash = pattern.hash ^ string.hash ^ params.hash |
#names ⇒ Array<String>
Returns the names of the named captures.
78 |
# File 'lib/mustermann/match.rb', line 78 def names = named_captures.keys |
#to_s ⇒ String
Returns the matched substring (like MatchData#to_s).
131 |
# File 'lib/mustermann/match.rb', line 131 def to_s = @matched |
#values_at(*keys) ⇒ Array
Returns the values of the given keys as an array.
128 |
# File 'lib/mustermann/match.rb', line 128 def values_at(*keys) = keys.map { |key| self[key] } |