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
Class Method Summary collapse
-
.new(string, **options) ⇒ Mustermann::Pattern
A new instance of Mustermann::Pattern.
-
.supported?(option) ⇒ Boolean
Whether or not option is supported.
-
.supported_options(*list) ⇒ Object
List of supported options.
Instance Method Summary collapse
-
#===(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.
-
#expand(**values) ⇒ String
Expanded string.
-
#initialize(string, **options) ⇒ Pattern
constructor
A new instance of Pattern.
-
#match(string) ⇒ MatchData, ...
MatchData or similar object if the pattern matches.
-
#named_captures ⇒ Array<String>
Capture names.
-
#names ⇒ Hash{String: Array<Integer>}
Capture names mapped to capture index.
-
#params(string = nil, captures: nil, offset: 0) ⇒ Hash{String: String, Array<String>}?
Sinatra style params if pattern matches.
-
#to_s ⇒ String
The string representation of the pattern.
Methods included from Mustermann
Constructor Details
#initialize(string, **options) ⇒ Pattern
Returns a new instance of Pattern.
59 60 61 62 |
# File 'lib/mustermann/pattern.rb', line 59 def initialize(string, uri_decode: true, **) @uri_decode = uri_decode @string = string.to_s.dup end |
Class Method Details
.new(string, **options) ⇒ Mustermann::Pattern
Returns a new instance of Mustermann::Pattern.
41 42 43 44 45 46 47 48 49 |
# File 'lib/mustermann/pattern.rb', line 41 def self.new(string, ignore_unknown_options: false, **) unless unsupported = .keys.detect { |key| not supported?(key) } raise ArgumentError, "unsupported option %p for %p" % [unsupported, self] if unsupported end @map ||= Tool::EqualityMap.new @map.fetch(string, ) { super(string, ) } end |
.supported?(option) ⇒ Boolean
Returns Whether or not option is supported.
31 32 33 |
# File 'lib/mustermann/pattern.rb', line 31 def self.supported?(option) .include? option end |
.supported_options ⇒ Array<Symbol> .supported_options(*list) ⇒ Array<Symbol>
List of supported options.
22 23 24 25 26 27 |
# File 'lib/mustermann/pattern.rb', line 22 def self.(*list) @supported_options ||= [] = @supported_options.concat(list) += superclass. if self < Pattern end |
Instance Method Details
#===(string) ⇒ Boolean
Needs to be overridden by subclass.
Returns Whether or not the pattern matches the given string.
89 90 91 |
# File 'lib/mustermann/pattern.rb', line 89 def ===(string) raise NotImplementedError, 'subclass responsibility' end |
#=~(string) ⇒ Integer?
Returns nil if pattern does not match the string, zero if it does.
81 82 83 |
# File 'lib/mustermann/pattern.rb', line 81 def =~(string) 0 if self === string end |
#expand(**values) ⇒ String
This method is only implemented by certain subclasses.
Returns expanded string.
137 138 139 |
# File 'lib/mustermann/pattern.rb', line 137 def (**values) raise NotImplementedError, "expanding not supported by #{self.class}" end |
#match(string) ⇒ MatchData, ...
Returns MatchData or similar object if the pattern matches.
74 75 76 |
# File 'lib/mustermann/pattern.rb', line 74 def match(string) SimpleMatch.new(string) if self === string end |
#named_captures ⇒ Array<String>
Returns capture names.
95 96 97 |
# File 'lib/mustermann/pattern.rb', line 95 def named_captures {} end |
#names ⇒ Hash{String: Array<Integer>}
Returns capture names mapped to capture index.
101 102 103 |
# File 'lib/mustermann/pattern.rb', line 101 def names [] end |
#params(string = nil, captures: nil, offset: 0) ⇒ Hash{String: String, Array<String>}?
Returns Sinatra style params if pattern matches.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mustermann/pattern.rb', line 107 def params(string = nil, captures: nil, offset: 0) return unless captures ||= match(string) params = named_captures.map do |name, positions| values = positions.map { |pos| map_param(name, captures[pos + offset]) }.flatten values = values.first if values.size < 2 and not always_array? name [name, values] end Hash[params] end |
#to_s ⇒ String
Returns the string representation of the pattern.
65 66 67 |
# File 'lib/mustermann/pattern.rb', line 65 def to_s @string.dup end |