Class: Mustermann::Pattern Abstract
- Inherits:
-
Object
- Object
- Mustermann::Pattern
- Includes:
- Mustermann
- Defined in:
- lib/mustermann/pattern.rb
Overview
Superclass for all pattern implementations.
Direct Known Subclasses
Constant Summary
Constants included from Mustermann
CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError, TrieError
Instance Attribute Summary collapse
-
#uri_decode ⇒ Object
readonly
Returns the value of attribute uri_decode.
Class Method Summary collapse
-
.new(string, **options) ⇒ Mustermann::Pattern
A new instance of Mustermann::Pattern.
-
.supported?(option, **options) ⇒ Boolean
Whether or not option is supported.
-
.supported_options(*list) ⇒ Object
List of supported options.
Instance Method Summary collapse
-
#+(other) ⇒ Mustermann::Pattern
Creates a concatenated pattern by combingin self with the other pattern supplied.
-
#==(other) ⇒ true, false
Two patterns are considered equal if they are of the same type, have the same pattern string and the same options.
-
#===(string) ⇒ Boolean
Whether or not the pattern matches the given string.
-
#=~(string) ⇒ Integer?
Nil if pattern does not match the string, zero if it does.
-
#eql?(other) ⇒ true, false
Two patterns are considered equal if they are of the same type, have the same pattern string and the same options.
-
#expand(behavior = nil, values = {}) ⇒ String
Expanding is supported by almost all patterns (notable exceptions are Shell, Regular and Simple).
-
#hash ⇒ Integer
Used by Ruby internally for hashing.
-
#initialize(string, **options) ⇒ Pattern
constructor
A new instance of Pattern.
-
#match(string) ⇒ Mustermann::Match?
The match object if the pattern matches.
-
#params(string = nil) ⇒ Hash{String: String, Array<String>}?
Sinatra style params if pattern matches.
-
#peek(string) ⇒ String?
Tries to match the pattern against the beginning of the string (as opposed to the full string).
-
#peek_match(string) ⇒ Mustermann::Match?
Tries to match the pattern against the beginning of the string (as opposed to the full string).
-
#peek_params(string) ⇒ Array<Hash, Integer>?
Tries to match the pattern against the beginning of the string (as opposed to the full string).
-
#peek_size(string) ⇒ Integer?
Tries to match the pattern against the beginning of the string (as opposed to the full string).
-
#to_proc ⇒ Proc
Proc wrapping #===.
-
#to_s ⇒ String
The string representation of the pattern.
-
#to_templates ⇒ Array<String>
Generates a list of URI template strings representing the pattern.
-
#|(other) ⇒ Mustermann::Pattern
(also: #&, #^)
A composite pattern.
Methods included from Mustermann
Constructor Details
#initialize(string, **options) ⇒ Pattern
Returns a new instance of Pattern.
75 76 77 78 79 |
# File 'lib/mustermann/pattern.rb', line 75 def initialize(string, uri_decode: true, **) @uri_decode = uri_decode @string = string.to_s.dup @options = yield.freeze if block_given? end |
Instance Attribute Details
#uri_decode ⇒ Object (readonly)
Returns the value of attribute uri_decode.
63 64 65 |
# File 'lib/mustermann/pattern.rb', line 63 def uri_decode @uri_decode end |
Class Method Details
.new(string, **options) ⇒ Mustermann::Pattern
Returns a new instance of Mustermann::Pattern.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mustermann/pattern.rb', line 50 def self.new(string, ignore_unknown_options: false, **) if = .select { |key, value| supported?(key, **) if key != :ignore_unknown_options } else unsupported = .keys.detect { |key| not supported?(key, **) } raise ArgumentError, "unsupported option %p for %p" % [unsupported, self] if unsupported end @map ||= EqualityMap.new @map.fetch([string, ]) { super(string, **) { } } end |
.supported?(option, **options) ⇒ Boolean
Returns Whether or not option is supported.
40 41 42 |
# File 'lib/mustermann/pattern.rb', line 40 def self.supported?(option, **) .include? option end |
.supported_options ⇒ Array<Symbol> .supported_options(*list) ⇒ Array<Symbol>
List of supported options.
24 25 26 27 28 29 |
# File 'lib/mustermann/pattern.rb', line 24 def self.(*list) @supported_options ||= [] = @supported_options.concat(list) += superclass. if self < Pattern end |
Instance Method Details
#+(other) ⇒ Mustermann::Pattern
Creates a concatenated pattern by combingin self with the other pattern supplied. Patterns of different types can be mixed. The availability of ‘to_templates` and `expand` depends on the patterns being concatenated.
String input is treated as identity pattern.
313 314 315 |
# File 'lib/mustermann/pattern.rb', line 313 def +(other) Concat.new(self, other, type: :identity) end |
#==(other) ⇒ true, false
Two patterns are considered equal if they are of the same type, have the same pattern string and the same options.
117 118 119 |
# File 'lib/mustermann/pattern.rb', line 117 def ==(other) other.class == self.class and other.to_s == @string and other. == end |
#===(string) ⇒ Boolean
Needs to be overridden by subclass.
Returns Whether or not the pattern matches the given string.
104 105 106 |
# File 'lib/mustermann/pattern.rb', line 104 def ===(string) raise NotImplementedError, 'subclass responsibility' end |
#=~(string) ⇒ Integer?
Returns nil if pattern does not match the string, zero if it does.
96 97 98 |
# File 'lib/mustermann/pattern.rb', line 96 def =~(string) 0 if self === string end |
#eql?(other) ⇒ true, false
Two patterns are considered equal if they are of the same type, have the same pattern string and the same options.
124 125 126 |
# File 'lib/mustermann/pattern.rb', line 124 def eql?(other) other.class.eql?(self.class) and other.to_s.eql?(@string) and other..eql?() end |
#expand(behavior = nil, values = {}) ⇒ String
217 218 219 |
# File 'lib/mustermann/pattern.rb', line 217 def (behavior = nil, values = {}) raise NotImplementedError, "expanding not supported by #{self.class}" end |
#hash ⇒ Integer
Used by Ruby internally for hashing.
110 111 112 |
# File 'lib/mustermann/pattern.rb', line 110 def hash self.class.hash ^ @string.hash ^ .hash end |
#match(string) ⇒ Mustermann::Match?
Returns the match object if the pattern matches.
89 90 91 |
# File 'lib/mustermann/pattern.rb', line 89 def match(string) Match.new(self, string) if self === string end |
#params(string = nil) ⇒ Hash{String: String, Array<String>}?
Returns Sinatra style params if pattern matches.
190 |
# File 'lib/mustermann/pattern.rb', line 190 def params(string = nil) = match(string)&.params |
#peek(string) ⇒ String?
Tries to match the pattern against the beginning of the string (as opposed to the full string). Will return the substring if it matches.
151 152 153 154 |
# File 'lib/mustermann/pattern.rb', line 151 def peek(string) size = peek_size(string) string[0, size] if size end |
#peek_match(string) ⇒ Mustermann::Match?
Tries to match the pattern against the beginning of the string (as opposed to the full string). Will return a MatchData or similar instance for the matched substring.
166 167 168 169 |
# File 'lib/mustermann/pattern.rb', line 166 def peek_match(string) matched = peek(string) Match.new(self, matched, {}, post_match: string[matched.size..-1]) if matched end |
#peek_params(string) ⇒ Array<Hash, Integer>?
Tries to match the pattern against the beginning of the string (as opposed to the full string). Will return a two element Array with the params parsed from the substring as first entry and the length of the substring as second.
183 184 185 186 |
# File 'lib/mustermann/pattern.rb', line 183 def peek_params(string) match = peek_match(string) match ? [match.params, match.string.size] : nil end |
#peek_size(string) ⇒ Integer?
Tries to match the pattern against the beginning of the string (as opposed to the full string). Will return the count of the matching characters if it matches.
137 138 139 140 |
# File 'lib/mustermann/pattern.rb', line 137 def peek_size(string) # this is a very naive, unperformant implementation string.size.downto(0).detect { |s| self === string[0, s] } end |
#to_proc ⇒ Proc
Returns proc wrapping #===.
323 324 325 |
# File 'lib/mustermann/pattern.rb', line 323 def to_proc @to_proc ||= method(:===).to_proc end |
#to_s ⇒ String
Returns the string representation of the pattern.
82 83 84 |
# File 'lib/mustermann/pattern.rb', line 82 def to_s @string.dup end |
#to_templates ⇒ Array<String>
This method is only implemented by certain subclasses.
Generates a list of URI template strings representing the pattern.
Note that this transformation is lossy and the strings matching these templates might not match the pattern (and vice versa).
This comes in quite handy since URI templates are not made for pattern matching. That way you can easily use a more precise template syntax and have it automatically generate hypermedia links for you.
Template generation is supported by almost all patterns (notable exceptions are Shell, Regular and Simple). Union Composite patterns (with the | operator) support template generation if all patterns they are composed of also support it.
256 257 258 |
# File 'lib/mustermann/pattern.rb', line 256 def to_templates raise NotImplementedError, "template generation not supported by #{self.class}" end |
#|(other) ⇒ Mustermann::Pattern #&(other) ⇒ Mustermann::Pattern #^(other) ⇒ Mustermann::Pattern Also known as: &, ^
Returns a composite pattern.
292 293 294 |
# File 'lib/mustermann/pattern.rb', line 292 def |(other) Mustermann::Composite.new(self, other, operator: __callee__, type: :identity) end |