mustermann 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +472 -321
- data/bench/capturing.rb +4 -4
- data/bench/regexp.rb +21 -0
- data/lib/mustermann/ast/boundaries.rb +44 -0
- data/lib/mustermann/ast/compiler.rb +13 -7
- data/lib/mustermann/ast/expander.rb +45 -14
- data/lib/mustermann/ast/node.rb +69 -6
- data/lib/mustermann/ast/param_scanner.rb +20 -0
- data/lib/mustermann/ast/parser.rb +140 -22
- data/lib/mustermann/ast/pattern.rb +68 -21
- data/lib/mustermann/ast/template_generator.rb +28 -0
- data/lib/mustermann/ast/transformer.rb +32 -24
- data/lib/mustermann/ast/translator.rb +24 -10
- data/lib/mustermann/ast/validation.rb +10 -6
- data/lib/mustermann/caster.rb +116 -0
- data/lib/mustermann/composite.rb +101 -0
- data/lib/mustermann/expander.rb +204 -0
- data/lib/mustermann/identity.rb +56 -0
- data/lib/mustermann/mapper.rb +94 -0
- data/lib/mustermann/pattern.rb +193 -13
- data/lib/mustermann/pattern_cache.rb +49 -0
- data/lib/mustermann/regexp.rb +1 -0
- data/lib/mustermann/regexp_based.rb +19 -2
- data/lib/mustermann/regular.rb +29 -0
- data/lib/mustermann/router/rack.rb +47 -0
- data/lib/mustermann/router/simple.rb +142 -0
- data/lib/mustermann/router.rb +9 -0
- data/lib/mustermann/simple_match.rb +5 -0
- data/lib/mustermann/sinatra.rb +22 -5
- data/lib/mustermann/to_pattern.rb +50 -0
- data/lib/mustermann/version.rb +1 -1
- data/lib/mustermann.rb +102 -19
- data/mustermann.gemspec +2 -12
- data/spec/ast_spec.rb +14 -0
- data/spec/composite_spec.rb +147 -0
- data/spec/expander_spec.rb +120 -0
- data/spec/identity_spec.rb +44 -0
- data/spec/mapper_spec.rb +83 -0
- data/spec/mustermann_spec.rb +50 -11
- data/spec/pattern_spec.rb +34 -7
- data/spec/regexp_based_spec.rb +1 -1
- data/spec/regular_spec.rb +61 -0
- data/spec/router/rack_spec.rb +39 -0
- data/spec/router/simple_spec.rb +32 -0
- data/spec/simple_match_spec.rb +1 -1
- data/spec/sinatra_spec.rb +197 -9
- data/spec/to_pattern_spec.rb +69 -0
- metadata +44 -146
- data/.gitignore +0 -18
- data/.rspec +0 -2
- data/.travis.yml +0 -7
- data/.yardopts +0 -1
- data/Gemfile +0 -2
- data/LICENSE +0 -22
- data/Rakefile +0 -6
- data/internals.md +0 -64
- data/lib/mustermann/ast/tree_renderer.rb +0 -29
- data/lib/mustermann/rails.rb +0 -17
- data/lib/mustermann/shell.rb +0 -22
- data/lib/mustermann/simple.rb +0 -35
- data/lib/mustermann/template.rb +0 -47
- data/spec/rails_spec.rb +0 -521
- data/spec/shell_spec.rb +0 -108
- data/spec/simple_spec.rb +0 -236
- data/spec/support/coverage.rb +0 -18
- data/spec/support/env.rb +0 -6
- data/spec/support/expand_matcher.rb +0 -27
- data/spec/support/match_matcher.rb +0 -39
- data/spec/support/pattern.rb +0 -28
- data/spec/support.rb +0 -5
- data/spec/template_spec.rb +0 -814
data/README.md
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
# The Amazing Mustermann
|
|
2
2
|
|
|
3
|
-
[](https://travis-ci.org/rkh/mustermann) [](https://coveralls.io/r/rkh/mustermann) [](https://codeclimate.com/github/rkh/mustermann) [](https://gemnasium.com/rkh/mustermann) [](http://badge.fury.io/rb/mustermann)
|
|
4
|
-
|
|
5
3
|
*Make sure you view the correct docs: [latest release](http://rubydoc.info/gems/mustermann/frames), [master](http://rubydoc.info/github/rkh/mustermann/master/frames).*
|
|
6
4
|
|
|
7
|
-
Welcome to [Mustermann](http://en.wikipedia.org/wiki/List_of_placeholder_names_by_language#German). Mustermann is your personal string matching expert. As an expert in the field of strings and patterns, Mustermann
|
|
5
|
+
Welcome to [Mustermann](http://en.wikipedia.org/wiki/List_of_placeholder_names_by_language#German). Mustermann is your personal string matching expert. As an expert in the field of strings and patterns, Mustermann keeps its runtime dependencies to a minimum and is fully covered with specs and documentation.
|
|
8
6
|
|
|
9
7
|
Given a string pattern, Mustermann will turn it into an object that behaves like a regular expression and has comparable performance characteristics.
|
|
10
8
|
|
|
@@ -18,124 +16,358 @@ when Mustermann.new('foo/*') then puts "prefixed with foo"
|
|
|
18
16
|
when Mustermann.new('*.pdf') then puts "it's a PDF"
|
|
19
17
|
when Mustermann.new('*.png') then puts "it's an image"
|
|
20
18
|
end
|
|
19
|
+
|
|
20
|
+
pattern = Mustermann.new('/:prefix/*.*')
|
|
21
|
+
pattern.params('/a/b.c') # => { "prefix" => "a", splat => ["b", "c"] }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Overview
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* **[Pattern Types](#-pattern-types):** Mustermann supports a wide variety of different pattern types, making it compatible with a large variety of existing software.
|
|
29
|
+
* **[Fine Grained Control](#-available-options):** You can easily adjust matching behavior and add constraints to the placeholders and capture groups.
|
|
30
|
+
* **[Binary Operators](#-binary-operators):** Patterns can be combined into composite patterns using binary operators.
|
|
31
|
+
* **[Regexp Look Alike](#-regexp-look-alike):** Mustermann patterns can be used as a replacement for regular expressions.
|
|
32
|
+
* **[Parameter Parsing](#-parameter-parsing):** Mustermann can parse matched parameters into a Sinatra-style "params" hash, including type casting.
|
|
33
|
+
* **[Peeking](#-peeking):** Lets you check if the beginning of a string matches a pattern.
|
|
34
|
+
* **[Expanding](#-expanding):** Besides parsing a parameters from an input string, a pattern object can also be used to generate a string from a set of parameters.
|
|
35
|
+
* **[Generating Templates](#-generating-templates):** This comes in handy when wanting to hand on patterns rather than fully expanded strings as part of an external API.
|
|
36
|
+
* **[Proc Look Alike](#-proc-look-alike):** Pass on a pattern instead of a block.
|
|
37
|
+
* **[Duck Typing](#-duck-typing):** You can create your own pattern-like objects by implementing `to_pattern`.
|
|
38
|
+
* **[Performance](#-performance):** Patterns are implemented with both performance and a low memory footprint in mind.
|
|
39
|
+
|
|
40
|
+
### Additional Tooling
|
|
41
|
+
|
|
42
|
+
These features are included in the library, but not loaded by default
|
|
43
|
+
|
|
44
|
+
* **[Mapper](#-mapper):** A simple tool for mapping one string to another based on patterns.
|
|
45
|
+
* **[Routers](#-routers):** Model execution flow based on pattern matching. Comes with a simple Rack router.
|
|
46
|
+
* **[Sinatra Integration](#-sinatra-integration):** Mustermann can be used as a [Sinatra](http://www.sinatrarb.com/) extension. Sinatra 2.0 and beyond will use Mustermann by default.
|
|
47
|
+
|
|
48
|
+
<a name="-pattern-types"></a>
|
|
49
|
+
## Pattern Types
|
|
50
|
+
|
|
51
|
+
Mustermann support multiple pattern types. A pattern type defines the syntax, matching semantics and whether certain features, like [expanding](#-expanding) and [generating templates](#-generating-templates), are available.
|
|
52
|
+
|
|
53
|
+
You can create a pattern of a certain type by passing `type` option to `Mustermann.new`:
|
|
54
|
+
|
|
55
|
+
``` ruby
|
|
56
|
+
require 'mustermann'
|
|
57
|
+
pattern = Mustermann.new('/*/**', type: :shell)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Note that this will use the type as suggestion: When passing in a string argument, it will create a pattern of the given type, but it might choose a different type for other objects (a regular expression argument will always result in a [regexp](#-pattern-details-regexp) pattern, a symbol always in a [sinatra](#-pattern-details-sinatra) pattern, etc).
|
|
61
|
+
|
|
62
|
+
Alternatively, you can also load and instantiate the pattern type directly:
|
|
63
|
+
|
|
64
|
+
``` ruby
|
|
65
|
+
require 'mustermann/shell'
|
|
66
|
+
pattern = Mustermann::Shell.new('/*/**')
|
|
21
67
|
```
|
|
22
68
|
|
|
69
|
+
Mustermann itself includes the [sinatra](#-sinatra-pattern), [identity](#-identity-pattern) and [regexp](#-regexp-pattern) pattern types. Other pattern types are available as separate gems.
|
|
70
|
+
|
|
71
|
+
<a name="-binary-operators"></a>
|
|
72
|
+
## Binary Operators
|
|
73
|
+
|
|
74
|
+
Patterns can be combined via binary operators. These are:
|
|
75
|
+
|
|
76
|
+
* `|` (or): Resulting pattern matches if at least one of the input pattern matches.
|
|
77
|
+
* `&` (and): Resulting pattern matches if all input patterns match.
|
|
78
|
+
* `^` (xor): Resulting pattern matches if exactly one of the input pattern matches.
|
|
79
|
+
|
|
80
|
+
``` ruby
|
|
81
|
+
require 'mustermann'
|
|
82
|
+
|
|
83
|
+
first = Mustermann.new('/foo/:input')
|
|
84
|
+
second = Mustermann.new('/:input/bar')
|
|
85
|
+
|
|
86
|
+
first | second === "/foo/foo" # => true
|
|
87
|
+
first | second === "/foo/bar" # => true
|
|
88
|
+
|
|
89
|
+
first & second === "/foo/foo" # => false
|
|
90
|
+
first & second === "/foo/bar" # => true
|
|
91
|
+
|
|
92
|
+
first ^ second === "/foo/foo" # => true
|
|
93
|
+
first ^ second === "/foo/bar" # => false
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
These resulting objects are fully functional pattern objects, allowing you to call methods like `params` or `to_proc` on them. Moreover, *or* patterns created solely from expandable patterns will also be expandable. The same logic also applies to generating templates from *or* patterns.
|
|
97
|
+
|
|
98
|
+
<a name="-regexp-look-alike"></a>
|
|
99
|
+
## Regexp Look Alike
|
|
100
|
+
|
|
101
|
+
Pattern objects mimic Ruby's `Regexp` class by implementing `match`, `=~`, `===`, `names` and `named_captures`.
|
|
102
|
+
|
|
103
|
+
``` ruby
|
|
104
|
+
require 'mustermann'
|
|
105
|
+
|
|
106
|
+
pattern = Mustermann.new('/:page')
|
|
107
|
+
pattern.match('/') # => nil
|
|
108
|
+
pattern.match('/home') # => #<MatchData "/home" page:"home">
|
|
109
|
+
pattern =~ '/home' # => 0
|
|
110
|
+
pattern === '/home' # => true (this allows using it in case statements)
|
|
111
|
+
pattern.names # => ['page']
|
|
112
|
+
pattern.names # => {"page"=>[1]}
|
|
113
|
+
|
|
114
|
+
pattern = Mustermann.new('/home', type: :identity)
|
|
115
|
+
pattern.match('/') # => nil
|
|
116
|
+
pattern.match('/home') # => #<Mustermann::SimpleMatch "/home">
|
|
117
|
+
pattern =~ '/home' # => 0
|
|
118
|
+
pattern === '/home' # => true (this allows using it in case statements)
|
|
119
|
+
pattern.names # => []
|
|
120
|
+
pattern.names # => {}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Moreover, patterns based on regular expressions (all but `identity` and `shell`) automatically convert to regular expressions when needed:
|
|
124
|
+
|
|
125
|
+
``` ruby
|
|
126
|
+
require 'mustermann'
|
|
127
|
+
|
|
128
|
+
pattern = Mustermann.new('/:page')
|
|
129
|
+
union = Regexp.union(pattern, /^$/)
|
|
130
|
+
|
|
131
|
+
union =~ "/foo" # => 0
|
|
132
|
+
union =~ "" # => 0
|
|
133
|
+
|
|
134
|
+
Regexp.try_convert(pattern) # => /.../
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
This way, unless some code explicitly checks the class for a regular expression, you should be able to pass in a pattern object instead even if the code in question was not written with Mustermann in mind.
|
|
138
|
+
|
|
139
|
+
<a name="-parameter-parsing"></a>
|
|
140
|
+
## Parameter Parsing
|
|
141
|
+
|
|
23
142
|
Besides being a `Regexp` look-alike, Mustermann also adds a `params` method, that will give you a Sinatra-style hash:
|
|
24
143
|
|
|
25
144
|
``` ruby
|
|
145
|
+
require 'mustermann'
|
|
146
|
+
|
|
26
147
|
pattern = Mustermann.new('/:prefix/*.*')
|
|
27
148
|
pattern.params('/a/b.c') # => { "prefix" => "a", splat => ["b", "c"] }
|
|
28
149
|
```
|
|
29
150
|
|
|
30
|
-
|
|
151
|
+
For patterns with typed captures, it will also automatically convert them:
|
|
152
|
+
|
|
153
|
+
``` ruby
|
|
154
|
+
require 'mustermann'
|
|
155
|
+
|
|
156
|
+
pattern = Mustermann.new('/<prefix>/<int:id>', type: :flask)
|
|
157
|
+
pattern.params('/page/10') # => { "prefix" => "page", "id" => 10 }
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
<a name="-peeking"></a>
|
|
161
|
+
## Peeking
|
|
162
|
+
|
|
163
|
+
Peeking gives the option to match a pattern against the beginning of a string rather the full string. Patterns come with four methods for peeking:
|
|
164
|
+
|
|
165
|
+
* `peek` returns the matching substring.
|
|
166
|
+
* `peek_size` returns the number of characters matching.
|
|
167
|
+
* `peek_match` will return a `MatchData` or `Mustermann::SimpleMatch` (just like `match` does for the full string)
|
|
168
|
+
* `peek_params` will return the `params` hash parsed from the substring and the number of characters.
|
|
169
|
+
|
|
170
|
+
All of the above will turn `nil` if there was no match.
|
|
171
|
+
|
|
172
|
+
``` ruby
|
|
173
|
+
require 'mustermann'
|
|
174
|
+
|
|
175
|
+
pattern = Mustermann.new('/:prefix')
|
|
176
|
+
pattern.peek('/foo/bar') # => '/foo'
|
|
177
|
+
pattern.peek_size('/foo/bar') # => 4
|
|
178
|
+
|
|
179
|
+
path_info = '/foo/bar'
|
|
180
|
+
params, size = patter.peek_params(path_info) # params == { "prefix" => "foo" }
|
|
181
|
+
rest = path_info[size..-1] # => "/bar"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
<a name="-expanding"></a>
|
|
185
|
+
## Expanding
|
|
186
|
+
|
|
187
|
+
Similarly to parsing, it is also possible to generate a string from a pattern by expanding it with a hash.
|
|
188
|
+
For simple expansions, you can use `Pattern#expand`.
|
|
31
189
|
|
|
32
190
|
``` ruby
|
|
33
191
|
pattern = Mustermann.new('/:file(.:ext)?')
|
|
34
192
|
pattern.expand(file: 'pony') # => "/pony"
|
|
35
193
|
pattern.expand(file: 'pony', ext: 'jpg') # => "/pony.jpg"
|
|
194
|
+
pattern.expand(ext: 'jpg') # raises Mustermann::ExpandError
|
|
36
195
|
```
|
|
37
196
|
|
|
38
|
-
|
|
197
|
+
Expanding can be useful for instance when implementing link helpers.
|
|
198
|
+
|
|
199
|
+
### Expander Objects
|
|
39
200
|
|
|
40
|
-
|
|
201
|
+
To get fine-grained control over expansion, you can use `Mustermann::Expander` directly.
|
|
41
202
|
|
|
42
|
-
You can
|
|
203
|
+
You can create an expander object directly from a string:
|
|
43
204
|
|
|
44
205
|
``` ruby
|
|
45
|
-
|
|
206
|
+
require 'mustermann/expander'
|
|
207
|
+
expander = Mustermann::Expander("/:file.jpg")
|
|
208
|
+
expander.expand(file: 'pony') # => "/pony.jpg"
|
|
209
|
+
|
|
210
|
+
expander = Mustermann::Expander(":file(.:ext)", type: :rails)
|
|
211
|
+
expander.expand(file: 'pony', ext: 'jpg') # => "/pony.jpg"
|
|
46
212
|
```
|
|
47
213
|
|
|
48
|
-
|
|
214
|
+
Or you can pass it a pattern instance:
|
|
49
215
|
|
|
50
216
|
``` ruby
|
|
51
|
-
|
|
217
|
+
require 'mustermann'
|
|
218
|
+
pattern = Mustermann.new("/:file")
|
|
219
|
+
|
|
220
|
+
require 'mustermann/expander'
|
|
221
|
+
expander = Mustermann::Expander.new(pattern)
|
|
52
222
|
```
|
|
53
223
|
|
|
54
|
-
|
|
224
|
+
### Expanding Multiple Patterns
|
|
55
225
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
<a href="#uri_decode"><tt>uri_decode</tt></a>
|
|
106
|
-
</td>
|
|
107
|
-
</tr>
|
|
108
|
-
<tr>
|
|
109
|
-
<th><a href="#sinatra"><tt>sinatra</tt></a></th>
|
|
110
|
-
<td>Sinatra 2.0 style patterns (default)</td>
|
|
111
|
-
<td><tt>/:slug(.:ext)?</tt></td>
|
|
112
|
-
<td>
|
|
113
|
-
<a href="#capture"><tt>capture</tt></a>,
|
|
114
|
-
<a href="#except"><tt>except</tt></a>,
|
|
115
|
-
<a href="#greedy"><tt>greedy</tt></a>,
|
|
116
|
-
<a href="#ignore_unknown_options"><tt>ignore_unknown_options</tt></a>,
|
|
117
|
-
<a href="#space_matches_plus"><tt>space_matches_plus</tt></a>,
|
|
118
|
-
<a href="#uri_decode"><tt>uri_decode</tt></a>
|
|
119
|
-
</td>
|
|
120
|
-
</tr>
|
|
121
|
-
<tr>
|
|
122
|
-
<th><a href="#template"><tt>template</tt></a></th>
|
|
123
|
-
<td><a href="http://tools.ietf.org/html/rfc6570">URI templates</a></td>
|
|
124
|
-
<td><tt>/dictionary/{term}</tt></td>
|
|
125
|
-
<td>
|
|
126
|
-
<a href="#capture"><tt>capture</tt></a>,
|
|
127
|
-
<a href="#except"><tt>except</tt></a>,
|
|
128
|
-
<a href="#greedy"><tt>greedy</tt></a>,
|
|
129
|
-
<a href="#ignore_unknown_options"><tt>ignore_unknown_options</tt></a>,
|
|
130
|
-
<a href="#space_matches_plus"><tt>space_matches_plus</tt></a>,
|
|
131
|
-
<a href="#uri_decode"><tt>uri_decode</tt></a>
|
|
132
|
-
</td>
|
|
133
|
-
</tr>
|
|
134
|
-
</tbody>
|
|
135
|
-
</table>
|
|
226
|
+
You can add patterns to an expander object via `<<`:
|
|
227
|
+
|
|
228
|
+
``` ruby
|
|
229
|
+
require 'mustermann'
|
|
230
|
+
|
|
231
|
+
expander = Mustermann::Expander.new
|
|
232
|
+
expander << "/users/:user_id"
|
|
233
|
+
expander << "/pages/:page_id"
|
|
234
|
+
|
|
235
|
+
expander.expand(user_id: 15) # => "/users/15"
|
|
236
|
+
expander.expand(page_id: 58) # => "/pages/58"
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
You can set pattern options when creating the expander:
|
|
240
|
+
|
|
241
|
+
``` ruby
|
|
242
|
+
require 'mustermann'
|
|
243
|
+
|
|
244
|
+
expander = Mustermann::Expander.new(type: :template)
|
|
245
|
+
expander << "/users/{user_id}"
|
|
246
|
+
expander << "/pages/{page_id}"
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Additionally, it is possible to combine patterns of different types:
|
|
250
|
+
|
|
251
|
+
``` ruby
|
|
252
|
+
require 'mustermann'
|
|
253
|
+
|
|
254
|
+
expander = Mustermann::Expander.new
|
|
255
|
+
expander << Mustermann.new("/users/{user_id}", type: :template)
|
|
256
|
+
expander << Mustermann.new("/pages/:page_id", type: :rails)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Handling Additional Values
|
|
260
|
+
|
|
261
|
+
The handling of additional values passed in to `expand` can be changed by setting the `additional_values` option:
|
|
262
|
+
|
|
263
|
+
``` ruby
|
|
264
|
+
require 'mustermann'
|
|
265
|
+
|
|
266
|
+
expander = Mustermann::Expander.new("/:slug", additional_values: :raise)
|
|
267
|
+
expander.expand(slug: "foo", value: "bar") # raises Mustermann::ExpandError
|
|
268
|
+
|
|
269
|
+
expander = Mustermann::Expander.new("/:slug", additional_values: :ignore)
|
|
270
|
+
expander.expand(slug: "foo", value: "bar") # => "/foo"
|
|
271
|
+
|
|
272
|
+
expander = Mustermann::Expander.new("/:slug", additional_values: :append)
|
|
273
|
+
expander.expand(slug: "foo", value: "bar") # => "/foo?value=bar"
|
|
274
|
+
```
|
|
136
275
|
|
|
137
|
-
|
|
276
|
+
It is also possible to pass this directly to the `expand` call:
|
|
277
|
+
|
|
278
|
+
``` ruby
|
|
279
|
+
require 'mustermann'
|
|
280
|
+
|
|
281
|
+
pattern = Mustermann.new('/:slug')
|
|
282
|
+
pattern.expand(:append, slug: "foo", value: "bar") # => "/foo?value=bar"
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
<a name="-generating-templates"></a>
|
|
286
|
+
## Generating Templates
|
|
287
|
+
|
|
288
|
+
... TODO ...
|
|
289
|
+
|
|
290
|
+
<a name="-proc-look-alike"></a>
|
|
291
|
+
## Proc Look Alike
|
|
292
|
+
|
|
293
|
+
Patterns implement `to_proc`:
|
|
294
|
+
|
|
295
|
+
``` ruby
|
|
296
|
+
require 'mustermann'
|
|
297
|
+
pattern = Mustermann.new('/foo')
|
|
298
|
+
callback = pattern.to_proc # => #<Proc>
|
|
299
|
+
|
|
300
|
+
callback.call('/foo') # => true
|
|
301
|
+
callback.call('/bar') # => false
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
They can therefore be easily passed to methods expecting a block:
|
|
305
|
+
|
|
306
|
+
``` ruby
|
|
307
|
+
require 'mustermann'
|
|
308
|
+
|
|
309
|
+
list = ["foo", "example@email.com", "bar"]
|
|
310
|
+
pattern = Mustermann.new(":name@:domain.:tld")
|
|
311
|
+
email = list.detect(&pattern) # => "example@email.com"
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
<a name="-mapper"></a>
|
|
315
|
+
## Mapper
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
You can use a mapper to transform strings according to two or more mappings:
|
|
319
|
+
|
|
320
|
+
``` ruby
|
|
321
|
+
require 'mustermann/mapper'
|
|
322
|
+
|
|
323
|
+
mapper = Mustermann::Mapper.new("/:page(.:format)?" => ["/:page/view.:format", "/:page/view.html"])
|
|
324
|
+
mapper['/foo'] # => "/foo/view.html"
|
|
325
|
+
mapper['/foo.xml'] # => "/foo/view.xml"
|
|
326
|
+
mapper['/foo/bar'] # => "/foo/bar"
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
<a name="-routers"></a>
|
|
330
|
+
## Routers
|
|
331
|
+
|
|
332
|
+
Mustermann comes with basic router implementations that will call certain callbacks depending on the input.
|
|
333
|
+
|
|
334
|
+
### Simple Router
|
|
335
|
+
|
|
336
|
+
The simple router chooses callbacks based on an input string.
|
|
337
|
+
|
|
338
|
+
``` ruby
|
|
339
|
+
require 'mustermann/router/simple'
|
|
340
|
+
|
|
341
|
+
router = Mustermann::Router::Simple.new(default: 42)
|
|
342
|
+
router.on(':name', capture: :digit) { |string| string.to_i }
|
|
343
|
+
router.call("23") # => 23
|
|
344
|
+
router.call("example") # => 42
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Rack Router
|
|
348
|
+
|
|
349
|
+
This is not a full replacement for Rails, Sinatra, Cuba, etc, as it only cares about path based routing.
|
|
350
|
+
|
|
351
|
+
``` ruby
|
|
352
|
+
require 'mustermann/router/rack'
|
|
353
|
+
|
|
354
|
+
router = Mustermann::Router::Rack.new do
|
|
355
|
+
on '/' do |env|
|
|
356
|
+
[200, {'Content-Type' => 'text/plain'}, ['Hello World!']]
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
on '/:name' do |env|
|
|
360
|
+
name = env['mustermann.params']['name']
|
|
361
|
+
[200, {'Content-Type' => 'text/plain'}, ["Hello #{name}!"]]
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
on '/something/*', call: SomeApp
|
|
365
|
+
end
|
|
138
366
|
|
|
367
|
+
# in a config.ru
|
|
368
|
+
run router
|
|
369
|
+
```
|
|
370
|
+
<a name="-sinatra-integration"></a>
|
|
139
371
|
## Sinatra Integration
|
|
140
372
|
|
|
141
373
|
All patterns implement `match`, which means they can be dropped into Sinatra and other Rack routers:
|
|
@@ -257,29 +489,91 @@ end
|
|
|
257
489
|
* It would introduce breaking changes, even though these would be minor
|
|
258
490
|
* Like Sinatra 2.0, Mustermann requires Ruby 2.0 or newer
|
|
259
491
|
|
|
492
|
+
<a name="-duck-typing"></a>
|
|
493
|
+
## Duck Typing
|
|
494
|
+
|
|
495
|
+
<a name="-duck-typing-to-pattern"></a>
|
|
496
|
+
### `to_pattern`
|
|
497
|
+
|
|
498
|
+
All methods converting string input to pattern objects will also accept any arbitrary object that implements `to_pattern`:
|
|
499
|
+
|
|
500
|
+
``` ruby
|
|
501
|
+
require 'mustermann'
|
|
502
|
+
|
|
503
|
+
class MyObject
|
|
504
|
+
def to_pattern(**options)
|
|
505
|
+
Mustermann.new("/foo", **options)
|
|
506
|
+
end
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
object = MyObject.new
|
|
510
|
+
Mustermann.new(object, type: :rails) # => #<Mustermann::Rails:"/foo">
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
It might also be that you want to call `to_pattern` yourself instead of `Mustermann.new`. You can load `mustermann/to_pattern` to implement this method for strings, regular expressions and pattern objects:
|
|
514
|
+
|
|
515
|
+
``` ruby
|
|
516
|
+
require 'mustermann/to_pattern'
|
|
517
|
+
|
|
518
|
+
"/foo".to_pattern # => #<Mustermann::Sinatra:"/foo">
|
|
519
|
+
"/foo".to_pattern(type: :rails) # => #<Mustermann::Rails:"/foo">
|
|
520
|
+
%r{/foo}.to_pattern # => #<Mustermann::Regular:"\\/foo">
|
|
521
|
+
"/foo".to_pattern.to_pattern # => #<Mustermann::Sinatra:"/foo">
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
You can also use the `Mustermann::ToPattern` mixin to easily add `to_pattern` to your own objects:
|
|
525
|
+
|
|
526
|
+
``` ruby
|
|
527
|
+
require 'mustermann/to_pattern'
|
|
528
|
+
|
|
529
|
+
class MyObject
|
|
530
|
+
include Mustermann::ToPattern
|
|
531
|
+
|
|
532
|
+
def to_s
|
|
533
|
+
"/foo"
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
MyObject.new.to_pattern # => #<Mustermann::Sinatra:"/foo">
|
|
538
|
+
```
|
|
260
539
|
|
|
261
|
-
|
|
540
|
+
<a name="-duck-typing-respond-to"></a>
|
|
541
|
+
### `respond_to?`
|
|
262
542
|
|
|
263
|
-
|
|
543
|
+
You can and should use `respond_to?` to check if a pattern supports certain features.
|
|
264
544
|
|
|
265
|
-
|
|
545
|
+
``` ruby
|
|
546
|
+
require 'mustermann'
|
|
547
|
+
pattern = Mustermann.new("/")
|
|
266
548
|
|
|
267
|
-
|
|
549
|
+
puts "supports expanding" if pattern.respond_to? :expand
|
|
550
|
+
puts "supports generating templates" if pattern.respond_to? :to_templates
|
|
551
|
+
```
|
|
268
552
|
|
|
269
|
-
|
|
553
|
+
Alternatively, you can handle a `NotImplementedError` raised from such a method.
|
|
270
554
|
|
|
271
555
|
``` ruby
|
|
272
|
-
require 'mustermann
|
|
273
|
-
Mustermann
|
|
556
|
+
require 'mustermann'
|
|
557
|
+
pattern = Mustermann.new("/")
|
|
558
|
+
|
|
559
|
+
begin
|
|
560
|
+
p pattern.to_templates
|
|
561
|
+
rescue NotImplementedError
|
|
562
|
+
puts "does not support generating templates"
|
|
563
|
+
end
|
|
274
564
|
```
|
|
275
565
|
|
|
276
|
-
|
|
566
|
+
This behavior corresponds to what Ruby does, for instance for [`fork`](http://ruby-doc.org/core-2.1.1/NotImplementedError.html).
|
|
567
|
+
|
|
568
|
+
<a name="-available-options"></a>
|
|
569
|
+
## Available Options
|
|
277
570
|
|
|
571
|
+
<a name="-available-options--capture"></a>
|
|
278
572
|
### `capture`
|
|
279
573
|
|
|
280
|
-
Supported by: `
|
|
574
|
+
Supported by: All types except `identity`, `shell` and `simple` patterns.
|
|
281
575
|
|
|
282
|
-
|
|
576
|
+
Most pattern types support changing the strings named captures will match via the `capture` options.
|
|
283
577
|
|
|
284
578
|
Possible values for a capture:
|
|
285
579
|
|
|
@@ -302,9 +596,10 @@ Mustermann.new('/:id.:ext', capture: { id: /\d+/, ext: ['png', 'jpg'] })
|
|
|
302
596
|
|
|
303
597
|
Available POSIX character classes are: `:alnum`, `:alpha`, `:blank`, `:cntrl`, `:digit`, `:graph`, `:lower`, `:print`, `:punct`, `:space`, `:upper`, `:xdigit`, `:word` and `:ascii`.
|
|
304
598
|
|
|
599
|
+
<a name="-available-options--except"></a>
|
|
305
600
|
### `except`
|
|
306
601
|
|
|
307
|
-
Supported by: `
|
|
602
|
+
Supported by: All types except `identity`, `shell` and `simple` patterns.
|
|
308
603
|
|
|
309
604
|
Given you supply a second pattern via the except option. Any string that would match the primary pattern but also matches the except pattern will not result in a successful match. Feel free to read that again. Or just take a look at this example:
|
|
310
605
|
|
|
@@ -322,13 +617,15 @@ pattern === '/foo.jpg' # => true
|
|
|
322
617
|
pattern === '/foo.png' # => false
|
|
323
618
|
```
|
|
324
619
|
|
|
620
|
+
<a name="-available-options--greedy"></a>
|
|
325
621
|
### `greedy`
|
|
326
622
|
|
|
327
|
-
Supported by:
|
|
623
|
+
Supported by: All types except `identity` and `shell` patterns.
|
|
624
|
+
Default value: `true`
|
|
328
625
|
|
|
329
626
|
**Simple** patterns are greedy, meaning that for the pattern `:foo:bar?`, everything will be captured as `foo`, `bar` will always be `nil`. By setting `greedy` to `false`, `foo` will capture as little as possible (which in this case would only be the first letter), leaving the rest to `bar`.
|
|
330
627
|
|
|
331
|
-
**
|
|
628
|
+
**All other** supported patterns are semi-greedy. This means `:foo(.:bar)?` (`:foo(.:bar)` for Rails patterns) will capture everything before the *last* dot as `foo`. For these two pattern types, you can switch into non-greedy mode by setting the `greedy` option to false. In that case `foo` will only capture the part before the *first* dot.
|
|
332
629
|
|
|
333
630
|
Semi-greedy behavior is not specific to dots, it works with all characters or strings. For instance, `:a(foo:b)` will capture everything before the *last* `foo` as `a`, and `:foo(bar)?` will not capture a `bar` at the end.
|
|
334
631
|
|
|
@@ -340,11 +637,13 @@ pattern = Mustermann.new(':a.:b', greedy: false)
|
|
|
340
637
|
pattern.match('a.b.c.d') # => #<MatchData a:"a" b:"b.c.d">
|
|
341
638
|
```
|
|
342
639
|
|
|
640
|
+
<a name="-available-options--space_matches_plus"></a>
|
|
343
641
|
### `space_matches_plus`
|
|
344
642
|
|
|
345
|
-
Supported by:
|
|
643
|
+
Supported by: All types except `identity`, `regexp` and `shell` patterns.
|
|
644
|
+
Default value: `true`
|
|
346
645
|
|
|
347
|
-
|
|
646
|
+
Most pattern types will by default also match a plus sign for a space in the pattern:
|
|
348
647
|
|
|
349
648
|
``` ruby
|
|
350
649
|
Mustermann.new('a b') === 'a+b' # => true
|
|
@@ -364,9 +663,11 @@ pattern.match('a b')[:x] # => 'a b'
|
|
|
364
663
|
pattern.match('a+b')[:x] # => 'a+b'
|
|
365
664
|
````
|
|
366
665
|
|
|
666
|
+
<a name="-available-options--uri_decode"></a>
|
|
367
667
|
### `uri_decode`
|
|
368
668
|
|
|
369
|
-
Supported by all
|
|
669
|
+
Supported by all pattern types.
|
|
670
|
+
Default value: `true`
|
|
370
671
|
|
|
371
672
|
Usually, characters in the pattern will also match the URI encoded version of these characters:
|
|
372
673
|
|
|
@@ -382,79 +683,49 @@ Mustermann.new('a b', uri_decode: false) === 'a b' # => true
|
|
|
382
683
|
Mustermann.new('a b', uri_decode: false) === 'a%20b' # => false
|
|
383
684
|
```
|
|
384
685
|
|
|
686
|
+
<a name="-available-options--ignore_unknown_options"></a>
|
|
385
687
|
### `ignore_unknown_options`
|
|
386
688
|
|
|
387
|
-
Supported by all patterns.
|
|
689
|
+
Supported by all patterns.
|
|
690
|
+
Default value: `false`
|
|
388
691
|
|
|
389
692
|
If you pass an option in that is not supported by the specific pattern type, Mustermann will raise an `ArgumentError`.
|
|
390
693
|
By setting `ignore_unknown_options` to `true`, it will happily ignore the option.
|
|
391
694
|
|
|
392
|
-
|
|
695
|
+
<a name="-performance"></a>
|
|
696
|
+
## Performance
|
|
393
697
|
|
|
394
|
-
|
|
698
|
+
It's generally a good idea to reuse pattern objects, since as much computation as possible is happening during object creation, so that the actual matching or expanding is quite fast.
|
|
395
699
|
|
|
396
|
-
|
|
700
|
+
Pattern objects should be treated as immutable. Their internals have been designed for both performance and low memory usage. To reduce pattern compilation, `Mustermann.new` and `Mustermann::Pattern.new` might return the same instance when given the same arguments, if that instance has not yet been garbage collected. However, this is not guaranteed, so do not rely on object identity.
|
|
397
701
|
|
|
398
|
-
|
|
399
|
-
<thead>
|
|
400
|
-
<tr>
|
|
401
|
-
<th>Syntax Element</th>
|
|
402
|
-
<th>Description</th>
|
|
403
|
-
</tr>
|
|
404
|
-
</thead>
|
|
405
|
-
<tbody>
|
|
406
|
-
<tr>
|
|
407
|
-
<td><i>any character</i></td>
|
|
408
|
-
<td>Matches exactly that character or a URI escaped version of it.</td>
|
|
409
|
-
</tr>
|
|
410
|
-
</tbody>
|
|
411
|
-
</table>
|
|
702
|
+
### String Matching
|
|
412
703
|
|
|
413
|
-
|
|
704
|
+
When using a pattern instead of a regular expression for string matching, performance will usually be comparable.
|
|
414
705
|
|
|
415
|
-
|
|
706
|
+
In certain cases, Mustermann might outperform naive, equivalent regular expressions. It achieves this by using look-ahead and atomic groups in ways that work well with a backtracking, NFA-based regular expression engine (such as the Oniguruma/Onigmo engine used by Ruby). It can be difficult and error prone to construct complex regular expressions using these techniques by hand. This only applies to patterns generating an AST internally (all but [identity](#-pattern-details-identity), [shell](#-pattern-details-shell), [simple](#-pattern-details-simple) and [regexp](#-pattern-details-regexp) patterns).
|
|
416
707
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
<th>Syntax Element</th>
|
|
421
|
-
<th>Description</th>
|
|
422
|
-
</tr>
|
|
423
|
-
</thead>
|
|
424
|
-
<tbody>
|
|
425
|
-
<tr>
|
|
426
|
-
<td><b>:</b><i>name</i></td>
|
|
427
|
-
<td>
|
|
428
|
-
Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
|
|
429
|
-
Capture behavior can be modified with <a href="#capture"><tt>capture</tt></a> and <a href="#greedy"><tt>greedy</tt></a> option.
|
|
430
|
-
</td>
|
|
431
|
-
</tr>
|
|
432
|
-
<tr>
|
|
433
|
-
<td><b>*</b><i>name</i></td>
|
|
434
|
-
<td>
|
|
435
|
-
Captures anything in a non-greedy fashion. Capture is named <i>name</i>.
|
|
436
|
-
</td>
|
|
437
|
-
</tr>
|
|
438
|
-
<tr>
|
|
439
|
-
<td><b>(</b><i>expression</i><b>)</b></td>
|
|
440
|
-
<td>Enclosed <i>expression</i> is optional.</td>
|
|
441
|
-
</tr>
|
|
442
|
-
<tr>
|
|
443
|
-
<td><b>/</b></td>
|
|
444
|
-
<td>
|
|
445
|
-
Matches forward slash. Does not match URI encoded version of forward slash.
|
|
446
|
-
</td>
|
|
447
|
-
</tr>
|
|
448
|
-
<tr>
|
|
449
|
-
<td><i>any other character</i></td>
|
|
450
|
-
<td>Matches exactly that character or a URI encoded version of it.</td>
|
|
451
|
-
</tr>
|
|
452
|
-
</tbody>
|
|
453
|
-
</table>
|
|
708
|
+
When using a Mustermann pattern as a direct Regexp replacement (ie, via methods like `=~`, `match` or `===`), the overhead will be a single method dispatch, which some Ruby implementations might even eliminate with method inlining. This only applies to patterns using a regular expression internally (all but [identity](#-pattern-details-identity) and [shell](#-pattern-details-shell) patterns).
|
|
709
|
+
|
|
710
|
+
### Expanding
|
|
454
711
|
|
|
455
|
-
|
|
712
|
+
Pattern expansion significantly outperforms other, widely used Ruby tools for generating URLs from URL patterns in most use cases.
|
|
456
713
|
|
|
457
|
-
|
|
714
|
+
This comes with a few trade-offs:
|
|
715
|
+
|
|
716
|
+
* As with pattern compilation, as much computation as possible has been shifted to compiling expansion rules. This will add compilation overhead, which is why patterns only generate these rules on the first invocation to `Mustermann::Pattern#expand`. Create a `Mustermann::Expander` instance yourself to get better control over the point in time this computation should happen.
|
|
717
|
+
* Memory is sacrificed in favor of performance: The size of the expander object will grow linear with the number of possible combination for expansion keys ("/:foo/:bar" has one such combination, but "/(:foo/)?:bar?" has four)
|
|
718
|
+
* Parsing a params hash from a string generated from another params hash might not result in two identical hashes, and vice versa. Specifically, expanding ignores capture constraints, type casting and greediness.
|
|
719
|
+
* Partial expansion is (currently) not supported.
|
|
720
|
+
|
|
721
|
+
## Details on Pattern Types
|
|
722
|
+
|
|
723
|
+
<a name="-identity-pattern"></a>
|
|
724
|
+
### `identity`
|
|
725
|
+
|
|
726
|
+
**Supported options:**
|
|
727
|
+
[`uri_decode`](#-available-options--uri_decode),
|
|
728
|
+
[`ignore_unknown_options`](#-available-options--ignore_unknown_options).
|
|
458
729
|
|
|
459
730
|
<table>
|
|
460
731
|
<thead>
|
|
@@ -465,35 +736,18 @@ Shell patterns, as used in Bash or with `Dir.glob`.
|
|
|
465
736
|
</thead>
|
|
466
737
|
<tbody>
|
|
467
738
|
<tr>
|
|
468
|
-
<td><
|
|
469
|
-
<td>Matches
|
|
470
|
-
</tr>
|
|
471
|
-
<tr>
|
|
472
|
-
<td><b>**</b></td>
|
|
473
|
-
<td>Matches anything.</td>
|
|
474
|
-
</tr>
|
|
475
|
-
<tr>
|
|
476
|
-
<td><b>[</b><i>set</i><b>]</b></td>
|
|
477
|
-
<td>Matches one character in <i>set</i>.</td>
|
|
478
|
-
</tr>
|
|
479
|
-
<tr>
|
|
480
|
-
<td><b>{</b><i>a</i>,<i>b</i><b>}</b></td>
|
|
481
|
-
<td>Matches <i>a</i> or <i>b</i>.</td>
|
|
482
|
-
</tr>
|
|
483
|
-
<tr>
|
|
484
|
-
<td><b>\</b><i>x</i></td>
|
|
485
|
-
<td>Matches <i>x</i> or URI encoded version of <i>x</i>. For instance <tt>\*</tt> matches <tt>*</tt>.</td>
|
|
486
|
-
</tr>
|
|
487
|
-
<tr>
|
|
488
|
-
<td><i>any other character</i></td>
|
|
489
|
-
<td>Matches exactly that character or a URI encoded version of it.</td>
|
|
739
|
+
<td><i>any character</i></td>
|
|
740
|
+
<td>Matches exactly that character or a URI escaped version of it.</td>
|
|
490
741
|
</tr>
|
|
491
742
|
</tbody>
|
|
492
743
|
</table>
|
|
493
744
|
|
|
494
|
-
|
|
745
|
+
<a name="-regexp-pattern"></a>
|
|
746
|
+
### `regexp`
|
|
495
747
|
|
|
496
|
-
|
|
748
|
+
**Supported options:**
|
|
749
|
+
[`uri_decode`](#-available-options--uri_decode),
|
|
750
|
+
[`ignore_unknown_options`](#-available-options--ignore_unknown_options).
|
|
497
751
|
|
|
498
752
|
<table>
|
|
499
753
|
<thead>
|
|
@@ -504,42 +758,22 @@ Patterns as used by Sinatra 1.3. Useful for porting an application that relies o
|
|
|
504
758
|
</thead>
|
|
505
759
|
<tbody>
|
|
506
760
|
<tr>
|
|
507
|
-
<td><
|
|
508
|
-
<td>
|
|
509
|
-
Captures anything but a forward slash in a greedy fashion. Capture is named <i>name</i>.
|
|
510
|
-
</td>
|
|
511
|
-
</tr>
|
|
512
|
-
<tr>
|
|
513
|
-
<td><b>*</b></td>
|
|
514
|
-
<td>
|
|
515
|
-
Captures anything in a non-greedy fashion. Capture is named splat.
|
|
516
|
-
It is always an array of captures, as you can use <tt>*</tt> more than once in a pattern.
|
|
517
|
-
</td>
|
|
518
|
-
</tr>
|
|
519
|
-
<tr>
|
|
520
|
-
<td><i>x</i><b>?</b></td>
|
|
521
|
-
<td>Makes <i>x</i> optional. For instance <tt>foo?</tt> matches <tt>foo</tt> or <tt>fo</tt>.</td>
|
|
522
|
-
</tr>
|
|
523
|
-
<tr>
|
|
524
|
-
<td><b>/</b></td>
|
|
525
|
-
<td>
|
|
526
|
-
Matches forward slash. Does not match URI encoded version of forward slash.
|
|
527
|
-
</td>
|
|
528
|
-
</tr>
|
|
529
|
-
<tr>
|
|
530
|
-
<td><i>any special character</i></td>
|
|
531
|
-
<td>Matches exactly that character or a URI encoded version of it.</td>
|
|
532
|
-
</tr>
|
|
533
|
-
<tr>
|
|
534
|
-
<td><i>any other character</i></td>
|
|
535
|
-
<td>Matches exactly that character.</td>
|
|
761
|
+
<td><i>any string</i></td>
|
|
762
|
+
<td>Interpreted as regular expression.</td>
|
|
536
763
|
</tr>
|
|
537
764
|
</tbody>
|
|
538
765
|
</table>
|
|
539
766
|
|
|
767
|
+
<a name="-sinatra-pattern"></a>
|
|
540
768
|
### `sinatra`
|
|
541
769
|
|
|
542
|
-
|
|
770
|
+
**Supported options:**
|
|
771
|
+
[`capture`](#-available-options--capture),
|
|
772
|
+
[`except`](#-available-options--except),
|
|
773
|
+
[`greedy`](#-available-options--greedy),
|
|
774
|
+
[`space_matches_plus`](#-available-options--space_matches_plus),
|
|
775
|
+
[`uri_decode`](#-available-options--uri_decode),
|
|
776
|
+
[`ignore_unknown_options`](#-available-options--ignore_unknown_options).
|
|
543
777
|
|
|
544
778
|
<table>
|
|
545
779
|
<thead>
|
|
@@ -550,17 +784,23 @@ Sinatra 2.0 style patterns. The default used by Mustermann.
|
|
|
550
784
|
</thead>
|
|
551
785
|
<tbody>
|
|
552
786
|
<tr>
|
|
553
|
-
<td><b>:</b><i>name</i></td>
|
|
787
|
+
<td><b>:</b><i>name</i> <i><b>or</b></i> <b>{</b><i>name</i><b>}</b></td>
|
|
554
788
|
<td>
|
|
555
789
|
Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
|
|
556
|
-
Capture behavior can be modified with <
|
|
790
|
+
Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
|
|
557
791
|
</td>
|
|
558
792
|
</tr>
|
|
559
793
|
<tr>
|
|
560
|
-
<td><b>*</b></td>
|
|
794
|
+
<td><b>*</b><i>name</i> <i><b>or</b></i> <b>{+</b><i>name</i><b>}</b></td>
|
|
795
|
+
<td>
|
|
796
|
+
Captures anything in a non-greedy fashion. Capture is named <i>name</i>.
|
|
797
|
+
</td>
|
|
798
|
+
</tr>
|
|
799
|
+
<tr>
|
|
800
|
+
<td><b>*</b> <i><b>or</b></i> <b>{+splat}</b></td>
|
|
561
801
|
<td>
|
|
562
802
|
Captures anything in a non-greedy fashion. Capture is named splat.
|
|
563
|
-
It is always an array of captures, as you can use
|
|
803
|
+
It is always an array of captures, as you can use it more than once in a pattern.
|
|
564
804
|
</td>
|
|
565
805
|
</tr>
|
|
566
806
|
<tr>
|
|
@@ -570,9 +810,15 @@ Sinatra 2.0 style patterns. The default used by Mustermann.
|
|
|
570
810
|
or to separate two elements that would otherwise be parsed as one.
|
|
571
811
|
</td>
|
|
572
812
|
</tr>
|
|
813
|
+
<tr>
|
|
814
|
+
<td><b>(</b><i>expression</i><b>|</b><i>expression</i><b>|</b><i>...</i><b>)</b></td>
|
|
815
|
+
<td>
|
|
816
|
+
Will match anything matching the nested expressions. May contain any other syntax element, including captures.
|
|
817
|
+
</td>
|
|
818
|
+
</tr>
|
|
573
819
|
<tr>
|
|
574
820
|
<td><i>x</i><b>?</b></td>
|
|
575
|
-
<td>Makes <i>x</i> optional. For instance <tt>(foo)?</tt> matches <tt>foo</tt> or an empty string.</td>
|
|
821
|
+
<td>Makes <i>x</i> optional. For instance, <tt>(foo)?</tt> matches <tt>foo</tt> or an empty string.</td>
|
|
576
822
|
</tr>
|
|
577
823
|
<tr>
|
|
578
824
|
<td><b>/</b></td>
|
|
@@ -590,98 +836,3 @@ Sinatra 2.0 style patterns. The default used by Mustermann.
|
|
|
590
836
|
</tr>
|
|
591
837
|
</tbody>
|
|
592
838
|
</table>
|
|
593
|
-
|
|
594
|
-
### `template`
|
|
595
|
-
|
|
596
|
-
Parses fully expanded URI templates as specified by [RFC 6570](http://tools.ietf.org/html/rfc6570).
|
|
597
|
-
|
|
598
|
-
Note that it differs from URI templates in that it takes the unescaped version of special character instead of the escaped version.
|
|
599
|
-
|
|
600
|
-
<table>
|
|
601
|
-
<thead>
|
|
602
|
-
<tr>
|
|
603
|
-
<th>Syntax Element</th>
|
|
604
|
-
<th>Description</th>
|
|
605
|
-
</tr>
|
|
606
|
-
</thead>
|
|
607
|
-
<tbody>
|
|
608
|
-
<tr>
|
|
609
|
-
<td><b>{</b><i>o</i> <i>var</i> <i>m</i><b>,</b> <i>var</i> <i>m</i><b>,</b> ...<b>}</b></td>
|
|
610
|
-
<td>
|
|
611
|
-
Captures expansion.
|
|
612
|
-
Operator <i>o</i>: <code>+ # . / ; ? &</tt> or none.
|
|
613
|
-
Modifier <i>m</i>: <code>:num *</tt> or none.
|
|
614
|
-
</td>
|
|
615
|
-
</tr>
|
|
616
|
-
<tr>
|
|
617
|
-
<td><b>/</b></td>
|
|
618
|
-
<td>
|
|
619
|
-
Matches forward slash. Does not match URI encoded version of forward slash.
|
|
620
|
-
</td>
|
|
621
|
-
</tr>
|
|
622
|
-
<tr>
|
|
623
|
-
<td><i>any other character</i></td>
|
|
624
|
-
<td>Matches exactly that character or a URI encoded version of it.</td>
|
|
625
|
-
</tr>
|
|
626
|
-
</tbody>
|
|
627
|
-
</table>
|
|
628
|
-
|
|
629
|
-
The operators `+` and `#` will always match non-greedy, whereas all other operators match semi-greedy by default.
|
|
630
|
-
All modifiers and operators are supported. However, it does not parse lists as single values without the *explode* modifier (aka *star*).
|
|
631
|
-
Parametric operators (`;`, `?` and `&`) currently only match parameters in given order.
|
|
632
|
-
|
|
633
|
-
``` ruby
|
|
634
|
-
pattern = Mustermann.new("{/segments*}/{page}{.ext,cmpr:2}", type: :template)
|
|
635
|
-
pattern.params("/a/b/c.tar.gz") # => {"segments"=>["a","b"], "page"=>"c", "ext"=>"tar", "cmpr"=>"gz"}
|
|
636
|
-
```
|
|
637
|
-
|
|
638
|
-
Please keep the following in mind:
|
|
639
|
-
|
|
640
|
-
> "Some URI Templates can be used in reverse for the purpose of variable matching: comparing the template to a fully formed URI in order to extract the variable parts from that URI and assign them to the named variables. Variable matching only works well if the template expressions are delimited by the beginning or end of the URI or by characters that cannot be part of the expansion, such as reserved characters surrounding a simple string expression. In general, regular expression languages are better suited for variable matching."
|
|
641
|
-
> — *RFC 6570, Sec 1.5: "Limitations"*
|
|
642
|
-
|
|
643
|
-
If you reuse the exact same templates and expose them via an external API meant for expansion,
|
|
644
|
-
you should set `uri_decode` to `false` in order to conform with the specification.
|
|
645
|
-
|
|
646
|
-
If you are looking for an alternative implementation that also supports expanding, check out [addressable](http://addressable.rubyforge.org/).
|
|
647
|
-
|
|
648
|
-
## Requirements
|
|
649
|
-
|
|
650
|
-
Mustermann has no dependencies besides a Ruby 2.0 compatible Ruby implementation.
|
|
651
|
-
|
|
652
|
-
It is known to work on **MRI 2.0** and **MRI trunk**.
|
|
653
|
-
**JRuby** is not yet supported, but is likely to follow soon (see [issue #2](https://github.com/rkh/mustermann/issues/2) for up to date information on JRuby).
|
|
654
|
-
**Rubinius** is not yet able to parse the Mustermann source code.
|
|
655
|
-
|
|
656
|
-
## Release History
|
|
657
|
-
|
|
658
|
-
Mustermann follows [Semantic Versioning 2.0](http://semver.org/). Anything documented in the README or via YARD and not declared private is part of the public API.
|
|
659
|
-
|
|
660
|
-
### Stable Releases
|
|
661
|
-
|
|
662
|
-
There have been no stable releases yet. The code base is considered solid but I don't know of anyone using it in production yet.
|
|
663
|
-
As there has been no stable release yet, the API might still change, though I consider this unlikely.
|
|
664
|
-
|
|
665
|
-
### Development Releases
|
|
666
|
-
|
|
667
|
-
* **Mustermann 0.0.1** (2013-04-27)
|
|
668
|
-
* More Infos:
|
|
669
|
-
[RubyGems.org](http://rubygems.org/gems/mustermann/versions/0.0.1),
|
|
670
|
-
[RubyDoc.info](rubydoc.info/gems/mustermann/0.0.1/frames),
|
|
671
|
-
[GitHub.com](https://github.com/rkh/mustermann/tree/v0.0.1)
|
|
672
|
-
* Initial Release.
|
|
673
|
-
* **Mustermann 0.1.0** (2013-05-12)
|
|
674
|
-
* Add `Pattern#expand` for generating strings from patterns.
|
|
675
|
-
* Add better internal API for working with the AST.
|
|
676
|
-
* Improved documentation.
|
|
677
|
-
* Avoids parsing the path twice when used as Sinatra extension.
|
|
678
|
-
* Better exceptions for unknown pattern types.
|
|
679
|
-
* Better handling of edge cases around extend.
|
|
680
|
-
* More specs to ensure API stability.
|
|
681
|
-
* Largely rework internals of Sinatra, Rails and Template patterns.
|
|
682
|
-
|
|
683
|
-
### Upcoming Releases
|
|
684
|
-
|
|
685
|
-
* **Mustermann 0.2.0** (next release with new features)
|
|
686
|
-
* **Mustermann 1.0.0** (before Sinatra 2.0)
|
|
687
|
-
* First stable release.
|