mustermann 0.3.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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +429 -665
  3. data/lib/mustermann/ast/boundaries.rb +44 -0
  4. data/lib/mustermann/ast/compiler.rb +13 -7
  5. data/lib/mustermann/ast/expander.rb +22 -12
  6. data/lib/mustermann/ast/node.rb +69 -5
  7. data/lib/mustermann/ast/param_scanner.rb +20 -0
  8. data/lib/mustermann/ast/parser.rb +138 -19
  9. data/lib/mustermann/ast/pattern.rb +59 -7
  10. data/lib/mustermann/ast/template_generator.rb +28 -0
  11. data/lib/mustermann/ast/transformer.rb +2 -2
  12. data/lib/mustermann/ast/translator.rb +22 -8
  13. data/lib/mustermann/ast/validation.rb +4 -3
  14. data/lib/mustermann/composite.rb +101 -0
  15. data/lib/mustermann/expander.rb +2 -2
  16. data/lib/mustermann/identity.rb +56 -0
  17. data/lib/mustermann/pattern.rb +187 -10
  18. data/lib/mustermann/pattern_cache.rb +49 -0
  19. data/lib/mustermann/regexp.rb +1 -0
  20. data/lib/mustermann/regexp_based.rb +18 -1
  21. data/lib/mustermann/regular.rb +4 -1
  22. data/lib/mustermann/simple_match.rb +5 -0
  23. data/lib/mustermann/sinatra.rb +22 -5
  24. data/lib/mustermann/to_pattern.rb +11 -6
  25. data/lib/mustermann/version.rb +1 -1
  26. data/lib/mustermann.rb +95 -20
  27. data/mustermann.gemspec +1 -14
  28. data/spec/ast_spec.rb +14 -0
  29. data/spec/composite_spec.rb +147 -0
  30. data/spec/expander_spec.rb +15 -0
  31. data/spec/identity_spec.rb +44 -0
  32. data/spec/mustermann_spec.rb +21 -2
  33. data/spec/pattern_spec.rb +7 -3
  34. data/spec/regular_spec.rb +25 -0
  35. data/spec/sinatra_spec.rb +184 -9
  36. data/spec/to_pattern_spec.rb +49 -0
  37. metadata +15 -180
  38. data/.gitignore +0 -18
  39. data/.rspec +0 -2
  40. data/.travis.yml +0 -10
  41. data/.yardopts +0 -1
  42. data/Gemfile +0 -2
  43. data/LICENSE +0 -22
  44. data/Rakefile +0 -6
  45. data/internals.md +0 -64
  46. data/lib/mustermann/ast/tree_renderer.rb +0 -29
  47. data/lib/mustermann/rails.rb +0 -17
  48. data/lib/mustermann/shell.rb +0 -29
  49. data/lib/mustermann/simple.rb +0 -35
  50. data/lib/mustermann/template.rb +0 -47
  51. data/spec/rails_spec.rb +0 -521
  52. data/spec/shell_spec.rb +0 -108
  53. data/spec/simple_spec.rb +0 -236
  54. data/spec/support/coverage.rb +0 -16
  55. data/spec/support/env.rb +0 -16
  56. data/spec/support/expand_matcher.rb +0 -27
  57. data/spec/support/match_matcher.rb +0 -39
  58. data/spec/support/pattern.rb +0 -39
  59. data/spec/support.rb +0 -5
  60. data/spec/template_spec.rb +0 -814
data/.gitignore DELETED
@@ -1,18 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- .test_queue_stats
7
- .coverage
8
- Gemfile.lock
9
- InstalledFiles
10
- _yardoc
11
- doc/
12
- lib/bundler/man
13
- pkg
14
- rdoc
15
- spec/reports
16
- test/tmp
17
- test/version_tmp
18
- tmp
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --tty
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- rvm:
2
- - 2.0.0
3
- - 2.1.0-preview1
4
- - ruby-head
5
- - jruby-head
6
- - rbx-2.0.0
7
- matrix:
8
- allow_failures:
9
- - rvm: jruby-head
10
- - rvm: rbx-2.0.0
data/.yardopts DELETED
@@ -1 +0,0 @@
1
- - README.md internals.md
data/Gemfile DELETED
@@ -1,2 +0,0 @@
1
- source 'https://rubygems.org'
2
- gemspec
data/LICENSE DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2013 Konstantin Haase
2
-
3
- Permission is hereby granted, free of charge, to any person
4
- obtaining a copy of this software and associated documentation
5
- files (the "Software"), to deal in the Software without
6
- restriction, including without limitation the rights to use,
7
- copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the
9
- Software is furnished to do so, subject to the following
10
- conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
- OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- ENV['JRUBY_OPTS'] = '--2.0 -X-C'
2
- ENV['RBXOPT'] = '-X20'
3
-
4
- task(:spec) { ruby '-w -S rspec' }
5
- task(:doc_stats) { ruby '-S yard stats' }
6
- task(default: [:spec, :doc_stats])
data/internals.md DELETED
@@ -1,64 +0,0 @@
1
- # Internal API
2
-
3
- This document describes how to use [Mustermann](README.md)'s internal API.
4
-
5
- It is a secondary goal to keep the internal API as stable as possible, in a state where it would well be possible to interface with it.
6
- However, the internal API is not covered by Semantic Versioning. As a rule of thumb, no backwards incompatible changes should be introduced to the API in minor releases (starting from 1.0.0).
7
-
8
- Should the internal API gain widespread/production use, we might consider moving parts of it over into the public API.
9
-
10
- Here is a quick example of what you can do with this:
11
-
12
- ``` ruby
13
- require 'mustermann/ast/pattern'
14
-
15
- class MyPattern < Mustermann::AST::Pattern
16
- on("~") { |c| node(:capture, buffer[1]) if expect(/\{(\w+)\}/) }
17
- on("+") { |c| node(:named_splat, buffer[1]) if expect(/\{(\w+)\}/) }
18
- on("?") { |c| node(:optional, node(:capture, buffer[1])) if expect(/\{(\w+)\}/) }
19
- end
20
-
21
- pattern = MyPattern.new("/+{prefix}/~{page}/?{optional}")
22
- pattern.params("/a/") # => nil
23
- pattern.params("/a/b/") # => { "prefix" => "a", "page" => "b", "optional" => nil }
24
- pattern.params("/a/b/c") # => { "prefix" => "a", "page" => "b", "optional" => "c" }
25
- pattern.params("/a/b/c/") # => { "prefix" => "a/b", "page" => "c", "optional" => nil }
26
-
27
- pattern.expand(prefix: "a", page: "foo") # => "/a/foo/"
28
- pattern.expand(prefix: "a/b", page: "c/d") # => "/a/b/c%2Fd/"
29
-
30
- require 'mustermann'
31
- Mustermann.register(:my_pattern, MyPattern, load: false)
32
- Mustermann.new('/+{prefix}/~{page}/?{optional}', type: :my_pattern) # => #<MyPattern:"/+{prefix}/~{page}/?{optional}">
33
-
34
- require 'sinatra/base'
35
- class MyApp < Sinatra::Base
36
- register Mustermann
37
- set :pattern, type: :my_pattern
38
-
39
- get '/hello/~{name}' do
40
- "Hello #{params[:name].capitalize}!"
41
- end
42
- end
43
-
44
- require 'mustermann/ast/tree_renderer'
45
- ast = MyPattern::Parser.parse(pattern.to_s)
46
- puts Mustermann::AST::TreeRenderer.render(ast)
47
-
48
- ```
49
-
50
- ## Pattern Registration
51
-
52
- ...
53
-
54
- ## Build Your Own Pattern
55
-
56
- ...
57
-
58
- ## Patterns Based on Regular Expressions
59
-
60
- ...
61
-
62
- ## AST-Based Patterns
63
-
64
- ...
@@ -1,29 +0,0 @@
1
- require 'mustermann/ast/translator'
2
-
3
- module Mustermann
4
- module AST
5
- # Turns an AST into a human readable string.
6
- # @!visibility private
7
- class TreeRenderer < Translator
8
- # @example
9
- # Mustermann::AST::TreeRenderer.render Mustermann::Sinatra::Parser.parse('/foo')
10
- #
11
- # @!visibility private
12
- def self.render(ast)
13
- new.translate(ast)
14
- end
15
-
16
- translate(Object) { inspect }
17
- translate(Array) { map { |e| "\n" << t(e) }.join.gsub("\n", "\n ") }
18
- translate(:node) { "#{t.type(node)} #{t(payload)}" }
19
- translate(:with_look_ahead) { "#{t.type(node)} #{t(head)} #{t(payload)}" }
20
-
21
- # Turns a class name into a node identifier.
22
- #
23
- # @!visibility private
24
- def type(node)
25
- node.class.name[/[^:]+$/].split(/(?<=.)(?=[A-Z])/).map(&:downcase).join(?_)
26
- end
27
- end
28
- end
29
- end
@@ -1,17 +0,0 @@
1
- require 'mustermann/ast/pattern'
2
-
3
- module Mustermann
4
- # Rails style pattern implementation.
5
- #
6
- # @example
7
- # Mustermann.new('/:foo', type: :rails) === '/bar' # => true
8
- #
9
- # @see Mustermann::Pattern
10
- # @see file:README.md#rails Syntax description in the README
11
- class Rails < AST::Pattern
12
- on(nil, ?)) { |c| unexpected(c) }
13
- on(?*) { |c| node(:named_splat) { scan(/\w+/) } }
14
- on(?() { |c| node(:optional, node(:group) { read unless scan(?)) }) }
15
- on(?:) { |c| node(:capture) { scan(/\w+/) } }
16
- end
17
- end
@@ -1,29 +0,0 @@
1
- require 'mustermann/pattern'
2
- require 'mustermann/simple_match'
3
-
4
- module Mustermann
5
- # Matches strings that are identical to the pattern.
6
- #
7
- # @example
8
- # Mustermann.new('/*.*', type: :shell) === '/bar' # => false
9
- #
10
- # @see Mustermann::Pattern
11
- # @see file:README.md#shell Syntax description in the README
12
- class Shell < Pattern
13
- # @param (see Mustermann::Pattern#initialize)
14
- # @return (see Mustermann::Pattern#initialize)
15
- # @see (see Mustermann::Pattern#initialize)
16
- def initialize(string, **options)
17
- @flags = File::FNM_PATHNAME | File::FNM_DOTMATCH
18
- @flags |= File::FNM_EXTGLOB if defined? File::FNM_EXTGLOB
19
- super(string, **options)
20
- end
21
-
22
- # @param (see Mustermann::Pattern#===)
23
- # @return (see Mustermann::Pattern#===)
24
- # @see (see Mustermann::Pattern#===)
25
- def ===(string)
26
- File.fnmatch? @string, unescape(string), @flags
27
- end
28
- end
29
- end
@@ -1,35 +0,0 @@
1
- require 'mustermann/regexp_based'
2
-
3
- module Mustermann
4
- # Sinatra 1.3 style pattern implementation.
5
- #
6
- # @example
7
- # Mustermann.new('/:foo', type: :simple) === '/bar' # => true
8
- #
9
- # @see Mustermann::Pattern
10
- # @see file:README.md#simple Syntax description in the README
11
- class Simple < RegexpBased
12
- supported_options :greedy, :space_matches_plus
13
-
14
- def compile(greedy: true, uri_decode: true, space_matches_plus: true, **options)
15
- pattern = @string.gsub(/[^\?\%\\\/\:\*\w]/) { |c| encoded(c, uri_decode, space_matches_plus) }
16
- pattern.gsub!(/((:\w+)|\*)/) do |match|
17
- match == "*" ? "(?<splat>.*?)" : "(?<#{$2[1..-1]}>[^/?#]+#{?? unless greedy})"
18
- end
19
- /\A#{Regexp.new(pattern)}\Z/
20
- rescue SyntaxError, RegexpError => error
21
- type = error.message["invalid group name"] ? CompileError : ParseError
22
- raise type, error.message, error.backtrace
23
- end
24
-
25
- def encoded(char, uri_decode, space_matches_plus)
26
- return Regexp.escape(char) unless uri_decode
27
- parser = URI::Parser.new
28
- encoded = Regexp.union(parser.escape(char), parser.escape(char, /./).downcase, parser.escape(char, /./).upcase)
29
- encoded = Regexp.union(encoded, encoded('+', true, true)) if space_matches_plus and char == " "
30
- encoded
31
- end
32
-
33
- private :compile, :encoded
34
- end
35
- end
@@ -1,47 +0,0 @@
1
- require 'mustermann/ast/pattern'
2
-
3
- module Mustermann
4
- # URI template pattern implementation.
5
- #
6
- # @example
7
- # Mustermann.new('/{foo}') === '/bar' # => true
8
- #
9
- # @see Mustermann::Pattern
10
- # @see file:README.md#template Syntax description in the README
11
- # @see http://tools.ietf.org/html/rfc6570 RFC 6570
12
- class Template < AST::Pattern
13
- on ?{ do |char|
14
- variable = proc do
15
- match = expect(/(?<name>\w+)(?:\:(?<prefix>\d{1,4})|(?<explode>\*))?/)
16
- node(:variable, match[:name], prefix: match[:prefix], explode: match[:explode])
17
- end
18
-
19
- operator = buffer.scan(/[\+\#\.\/;\?\&\=\,\!\@\|]/)
20
- expression = node(:expression, [variable[]], operator: operator) { variable[] if scan(?,) }
21
- expression if expect(?})
22
- end
23
-
24
- on(?}) { |c| unexpected(c) }
25
-
26
- # @!visibility private
27
- def compile(*args, **options)
28
- @split_params = {}
29
- super(*args, split_params: @split_params, **options)
30
- end
31
-
32
- # @!visibility private
33
- def map_param(key, value)
34
- return super unless variable = @split_params[key]
35
- value = value.split variable[:separator]
36
- value.map! { |e| e.sub(/\A#{key}=/, '') } if variable[:parametric]
37
- value.map! { |e| super(key, e) }
38
- end
39
-
40
- # @!visibility private
41
- def always_array?(key)
42
- @split_params.include? key
43
- end
44
-
45
- private :compile, :map_param, :always_array?
46
- end
47
- end