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
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'set'
|
|
2
|
+
require 'thread'
|
|
3
|
+
require 'mustermann'
|
|
4
|
+
|
|
5
|
+
module Mustermann
|
|
6
|
+
# A simple, persistent cache for creating repositories.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
# require 'mustermann/pattern_cache'
|
|
10
|
+
# cache = Mustermann::PatternCache.new
|
|
11
|
+
#
|
|
12
|
+
# # use this instead of Mustermann.new
|
|
13
|
+
# pattern = cache.create_pattern("/:name", type: :rails)
|
|
14
|
+
#
|
|
15
|
+
# @note
|
|
16
|
+
# {Mustermann::Pattern.new} (which is used by {Mustermann.new}) will reuse instances that have
|
|
17
|
+
# not yet been garbage collected. You only need an extra cache if you do not keep a reference to
|
|
18
|
+
# the patterns around.
|
|
19
|
+
#
|
|
20
|
+
# @api private
|
|
21
|
+
class PatternCache
|
|
22
|
+
# @param [Hash] pattern_options default options used for {#create_pattern}
|
|
23
|
+
def initialize(**pattern_options)
|
|
24
|
+
@cached = Set.new
|
|
25
|
+
@mutex = Mutex.new
|
|
26
|
+
@pattern_options = pattern_options
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @param (see Mustermann.new)
|
|
30
|
+
# @return (see Mustermann.new)
|
|
31
|
+
# @raise (see Mustermann.new)
|
|
32
|
+
# @see Mustermann.new
|
|
33
|
+
def create_pattern(string, **pattern_options)
|
|
34
|
+
pattern = Mustermann.new(string, **pattern_options, **@pattern_options)
|
|
35
|
+
@mutex.synchronize { @cached.add(pattern) } unless @cached.include? pattern
|
|
36
|
+
pattern
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Removes all pattern instances from the cache.
|
|
40
|
+
def clear
|
|
41
|
+
@mutex.synchronize { @cached.clear }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @return [Integer] number of currently cached patterns
|
|
45
|
+
def size
|
|
46
|
+
@mutex.synchronize { @cached.size }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'mustermann/regular'
|
|
@@ -14,14 +14,31 @@ module Mustermann
|
|
|
14
14
|
# @return (see Mustermann::Pattern#initialize)
|
|
15
15
|
# @see (see Mustermann::Pattern#initialize)
|
|
16
16
|
def initialize(string, **options)
|
|
17
|
-
@regexp = compile(string, **options)
|
|
18
17
|
super
|
|
18
|
+
regexp = compile(**options)
|
|
19
|
+
@peek_regexp = /\A(#{regexp})/
|
|
20
|
+
@regexp = /\A#{regexp}\Z/
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @param (see Mustermann::Pattern#peek_size)
|
|
24
|
+
# @return (see Mustermann::Pattern#peek_size)
|
|
25
|
+
# @see (see Mustermann::Pattern#peek_size)
|
|
26
|
+
def peek_size(string)
|
|
27
|
+
return unless match = peek_match(string)
|
|
28
|
+
match.to_s.size
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @param (see Mustermann::Pattern#peek_match)
|
|
32
|
+
# @return (see Mustermann::Pattern#peek_match)
|
|
33
|
+
# @see (see Mustermann::Pattern#peek_match)
|
|
34
|
+
def peek_match(string)
|
|
35
|
+
@peek_regexp.match(string)
|
|
19
36
|
end
|
|
20
37
|
|
|
21
38
|
extend Forwardable
|
|
22
39
|
def_delegators :regexp, :===, :=~, :match, :names, :named_captures
|
|
23
40
|
|
|
24
|
-
def compile(
|
|
41
|
+
def compile(**options)
|
|
25
42
|
raise NotImplementedError, 'subclass responsibility'
|
|
26
43
|
end
|
|
27
44
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'mustermann'
|
|
2
|
+
require 'mustermann/regexp_based'
|
|
3
|
+
|
|
4
|
+
module Mustermann
|
|
5
|
+
# Regexp pattern implementation.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# Mustermann.new('/.*', type: :regexp) === '/bar' # => true
|
|
9
|
+
#
|
|
10
|
+
# @see Mustermann::Pattern
|
|
11
|
+
# @see file:README.md#simple Syntax description in the README
|
|
12
|
+
class Regular < RegexpBased
|
|
13
|
+
register :regexp, :regular
|
|
14
|
+
|
|
15
|
+
# @param (see Mustermann::Pattern#initialize)
|
|
16
|
+
# @return (see Mustermann::Pattern#initialize)
|
|
17
|
+
# @see (see Mustermann::Pattern#initialize)
|
|
18
|
+
def initialize(string, **options)
|
|
19
|
+
string = $1 if string.to_s =~ /\A\(\?\-mix\:(.*)\)\Z/ && string.inspect == "/#$1/"
|
|
20
|
+
super(string, **options)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def compile(**options)
|
|
24
|
+
/#{@string}/
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private :compile
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'mustermann/router/simple'
|
|
2
|
+
|
|
3
|
+
module Mustermann
|
|
4
|
+
module Router
|
|
5
|
+
# Simple pattern based router that allows matching paths to a given Rack application.
|
|
6
|
+
#
|
|
7
|
+
# @example config.ru
|
|
8
|
+
# router = Mustermann::Rack.new do
|
|
9
|
+
# on '/' do |env|
|
|
10
|
+
# [200, {'Content-Type' => 'text/plain'}, ['Hello World!']]
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# on '/:name' do |env|
|
|
14
|
+
# name = env['mustermann.params']['name']
|
|
15
|
+
# [200, {'Content-Type' => 'text/plain'}, ["Hello #{name}!"]]
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# on '/something/*', call: SomeApp
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# # in a config.ru
|
|
22
|
+
# run router
|
|
23
|
+
class Rack < Simple
|
|
24
|
+
def initialize(env_prefix: "mustermann", params_key: "#{env_prefix}.params", pattern_key: "#{env_prefix}.pattern", **options, &block)
|
|
25
|
+
@params_key, @pattern_key = params_key, pattern_key
|
|
26
|
+
options[:default] = [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found"]] unless options.include? :default
|
|
27
|
+
super(**options, &block)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def invoke(callback, env, params, pattern)
|
|
31
|
+
params_was, pattern_was = env[@params_key], env[@pattern_key]
|
|
32
|
+
env[@params_key], env[@pattern_key] = params, pattern
|
|
33
|
+
response = callback.call(env)
|
|
34
|
+
response[1].each { |k,v| throw :pass if k.downcase == 'x-cascade' and v == 'pass' }
|
|
35
|
+
response
|
|
36
|
+
ensure
|
|
37
|
+
env[@params_key], env[@pattern_key] = params_was, pattern_was
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def string_for(env)
|
|
41
|
+
env['PATH_INFO']
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private :invoke, :string_for
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
require 'mustermann'
|
|
2
|
+
|
|
3
|
+
module Mustermann
|
|
4
|
+
module Router
|
|
5
|
+
# Simple pattern based router that allows matching a string to a given callback.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# require 'mustermann/router/simple'
|
|
9
|
+
#
|
|
10
|
+
# router = Mustermann::Router::Simple.new do
|
|
11
|
+
# on ':name/:sub' do |string, params|
|
|
12
|
+
# params['sub']
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# on 'foo' do
|
|
16
|
+
# "bar"
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# router.call("foo") # => "bar"
|
|
21
|
+
# router.call("a/b") # => "b"
|
|
22
|
+
# router.call("bar") # => nil
|
|
23
|
+
class Simple
|
|
24
|
+
# Default value for when no pattern matches
|
|
25
|
+
attr_accessor :default
|
|
26
|
+
|
|
27
|
+
# @example with a default value
|
|
28
|
+
# require 'mustermann/router/simple'
|
|
29
|
+
#
|
|
30
|
+
# router = Mustermann::Router::Simple.new(default: 42)
|
|
31
|
+
# router.on(':name', capture: :digit) { |string| string.to_i }
|
|
32
|
+
# router.call("23") # => 23
|
|
33
|
+
# router.call("example") # => 42
|
|
34
|
+
#
|
|
35
|
+
# @example block with implicit receiver
|
|
36
|
+
# require 'mustermann/router/simple'
|
|
37
|
+
#
|
|
38
|
+
# router = Mustermann::Router::Simple.new do
|
|
39
|
+
# on('/foo') { 'foo' }
|
|
40
|
+
# on('/bar') { 'bar' }
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# @example block with explicit receiver
|
|
44
|
+
# require 'mustermann/router/simple'
|
|
45
|
+
#
|
|
46
|
+
# router = Mustermann::Router::Simple.new(type: :rails) do |r|
|
|
47
|
+
# r.on('/foo') { 'foo' }
|
|
48
|
+
# r.on('/bar') { 'bar' }
|
|
49
|
+
# end
|
|
50
|
+
#
|
|
51
|
+
# @param default value to be returned if nothing matches
|
|
52
|
+
# @param options [Hash] pattern options
|
|
53
|
+
# @return [Mustermann::Router::Simple] new router instance
|
|
54
|
+
def initialize(default: nil, **options, &block)
|
|
55
|
+
@options = options
|
|
56
|
+
@map = []
|
|
57
|
+
@default = default
|
|
58
|
+
|
|
59
|
+
block.arity == 0 ? instance_eval(&block) : yield(self) if block
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# @example
|
|
63
|
+
# require 'mustermann/router/simple'
|
|
64
|
+
#
|
|
65
|
+
# router = Mustermann::Router::Simple.new
|
|
66
|
+
# router.on(':a/:b') { 42 }
|
|
67
|
+
# router['foo/bar'] # => <#Proc:...>
|
|
68
|
+
# router['foo_bar'] # => nil
|
|
69
|
+
#
|
|
70
|
+
# @return [#call, nil] callback for given string, if a pattern matches
|
|
71
|
+
def [](string)
|
|
72
|
+
string = string_for(string) unless string.is_a? String
|
|
73
|
+
@map.detect { |p,v| p === string }[1]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @example
|
|
77
|
+
# require 'mustermann/router/simple'
|
|
78
|
+
#
|
|
79
|
+
# router = Mustermann::Router::Simple.new
|
|
80
|
+
# router['/:name'] = proc { |string, params| params['name'] }
|
|
81
|
+
# router.call('/foo') # => "foo"
|
|
82
|
+
#
|
|
83
|
+
# @param pattern [String, Mustermann::Pattern] matcher
|
|
84
|
+
# @param callback [#call] callback to call on match
|
|
85
|
+
# @see #on
|
|
86
|
+
def []=(pattern, callback)
|
|
87
|
+
on(pattern, call: callback)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @example with block
|
|
91
|
+
# require 'mustermann/router/simple'
|
|
92
|
+
#
|
|
93
|
+
# router = Mustermann::Router::Simple.new
|
|
94
|
+
#
|
|
95
|
+
# router.on(':a/:b') { 42 }
|
|
96
|
+
# router.call('foo/bar') # => 42
|
|
97
|
+
# router.call('foo_bar') # => nil
|
|
98
|
+
#
|
|
99
|
+
# @example with callback option
|
|
100
|
+
# require 'mustermann/router/simple'
|
|
101
|
+
#
|
|
102
|
+
# callback = proc { 42 }
|
|
103
|
+
# router = Mustermann::Router::Simple.new
|
|
104
|
+
#
|
|
105
|
+
# router.on(':a/:b', call: callback)
|
|
106
|
+
# router.call('foo/bar') # => 42
|
|
107
|
+
# router.call('foo_bar') # => nil
|
|
108
|
+
#
|
|
109
|
+
# @param patterns [Array<String, Pattern>]
|
|
110
|
+
# @param call [#call] callback object, need to hand in block if missing
|
|
111
|
+
# @param options [Hash] pattern options
|
|
112
|
+
def on(*patterns, call: Proc.new, **options)
|
|
113
|
+
patterns.each do |pattern|
|
|
114
|
+
pattern = Mustermann.new(pattern.to_str, **options, **@options) if pattern.respond_to? :to_str
|
|
115
|
+
@map << [pattern, call]
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Finds the matching callback and calls `call` on it with the given input and the params.
|
|
120
|
+
# @return the callback's return value
|
|
121
|
+
def call(input)
|
|
122
|
+
@map.each do |pattern, callback|
|
|
123
|
+
catch(:pass) do
|
|
124
|
+
next unless params = pattern.params(string_for(input))
|
|
125
|
+
return invoke(callback, input, params, pattern)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
@default
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def invoke(callback, input, params, pattern)
|
|
132
|
+
callback.call(input, params)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def string_for(input)
|
|
136
|
+
input.to_str
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
private :invoke, :string_for
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
data/lib/mustermann/sinatra.rb
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require 'mustermann'
|
|
1
2
|
require 'mustermann/ast/pattern'
|
|
2
3
|
|
|
3
4
|
module Mustermann
|
|
@@ -9,11 +10,27 @@ module Mustermann
|
|
|
9
10
|
# @see Mustermann::Pattern
|
|
10
11
|
# @see file:README.md#sinatra Syntax description in the README
|
|
11
12
|
class Sinatra < AST::Pattern
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
on(?
|
|
15
|
-
|
|
16
|
-
on(
|
|
13
|
+
register :sinatra
|
|
14
|
+
|
|
15
|
+
on(nil, ??, ?), ?|) { |c| unexpected(c) }
|
|
16
|
+
|
|
17
|
+
on(?*) { |c| scan(/\w+/) ? node(:named_splat, buffer.matched) : node(:splat) }
|
|
18
|
+
on(?:) { |c| node(:capture) { scan(/\w+/) } }
|
|
19
|
+
on(?\\) { |c| node(:char, expect(/./)) }
|
|
20
|
+
|
|
21
|
+
on ?( do |char|
|
|
22
|
+
groups = []
|
|
23
|
+
groups << node(:group) { read unless check(?)) or scan(?|) } until scan(?))
|
|
24
|
+
groups.size == 1 ? groups.first : node(:union, groups)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
on ?{ do |char|
|
|
28
|
+
type = scan(?+) ? :named_splat : :capture
|
|
29
|
+
name = expect(/[\w\.]+/)
|
|
30
|
+
type = :splat if type == :named_splat and name == 'splat'
|
|
31
|
+
expect(?})
|
|
32
|
+
node(type, name)
|
|
33
|
+
end
|
|
17
34
|
|
|
18
35
|
suffix ?? do |char, element|
|
|
19
36
|
node(:optional, element)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'mustermann'
|
|
2
|
+
|
|
3
|
+
module Mustermann
|
|
4
|
+
# Mixin for adding {#to_pattern} ducktyping to objects.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# require 'mustermann/to_pattern'
|
|
8
|
+
#
|
|
9
|
+
# class Foo
|
|
10
|
+
# include Mustermann::ToPattern
|
|
11
|
+
#
|
|
12
|
+
# def to_s
|
|
13
|
+
# ":foo/:bar"
|
|
14
|
+
# end
|
|
15
|
+
# end
|
|
16
|
+
#
|
|
17
|
+
# Foo.new.to_pattern # => #<Mustermann::Sinatra:":foo/:bar">
|
|
18
|
+
#
|
|
19
|
+
# By default included into String, Symbol, Regexp, Array and {Mustermann::Pattern}.
|
|
20
|
+
module ToPattern
|
|
21
|
+
PRIMITIVES = [String, Symbol, Array, Regexp, Mustermann::Pattern]
|
|
22
|
+
private_constant :PRIMITIVES
|
|
23
|
+
|
|
24
|
+
# Converts the object into a {Mustermann::Pattern}.
|
|
25
|
+
#
|
|
26
|
+
# @example converting a string
|
|
27
|
+
# ":name.png".to_pattern # => #<Mustermann::Sinatra:":name.png">
|
|
28
|
+
#
|
|
29
|
+
# @example converting a string with options
|
|
30
|
+
# "/*path".to_pattern(type: :rails) # => #<Mustermann::Rails:"/*path">
|
|
31
|
+
#
|
|
32
|
+
# @example converting a regexp
|
|
33
|
+
# /.*/.to_pattern # => #<Mustermann::Regular:".*">
|
|
34
|
+
#
|
|
35
|
+
# @example converting a pattern
|
|
36
|
+
# Mustermann.new("foo").to_pattern # => #<Mustermann::Sinatra:"foo">
|
|
37
|
+
#
|
|
38
|
+
# @param [Hash] options The options hash.
|
|
39
|
+
# @return [Mustermann::Pattern] pattern corresponding to object.
|
|
40
|
+
def to_pattern(**options)
|
|
41
|
+
input = self if PRIMITIVES.any? { |p| self.is_a? p }
|
|
42
|
+
input ||= __getobj__ if respond_to?(:__getobj__)
|
|
43
|
+
Mustermann.new(input || to_s, **options)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
PRIMITIVES.each do |klass|
|
|
47
|
+
append_features(klass)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/lib/mustermann/version.rb
CHANGED
data/lib/mustermann.rb
CHANGED
|
@@ -1,36 +1,117 @@
|
|
|
1
|
+
require 'mustermann/pattern'
|
|
2
|
+
require 'mustermann/composite'
|
|
3
|
+
require 'thread'
|
|
4
|
+
|
|
1
5
|
# Namespace and main entry point for the Mustermann library.
|
|
2
6
|
#
|
|
3
7
|
# Under normal circumstances the only external API entry point you should be using is {Mustermann.new}.
|
|
4
8
|
module Mustermann
|
|
5
|
-
#
|
|
9
|
+
# Type to use if no type is given.
|
|
10
|
+
# @api private
|
|
11
|
+
DEFAULT_TYPE = :sinatra
|
|
12
|
+
|
|
13
|
+
# Creates a new pattern based on input.
|
|
14
|
+
#
|
|
15
|
+
# * From {Mustermann::Pattern}: returns given pattern.
|
|
16
|
+
# * From String: creates a pattern from the string, depending on type option (defaults to {Mustermann::Sinatra})
|
|
17
|
+
# * From Regexp: creates a {Mustermann::Regular} pattern.
|
|
18
|
+
# * From Symbol: creates a {Mustermann::Sinatra} pattern with a single named capture named after the input.
|
|
19
|
+
# * From an Array or multiple inputs: creates a new pattern from each element, combines them to a {Mustermann::Composite}.
|
|
20
|
+
# * From anything else: Will try to call to_pattern on it or raise a TypeError.
|
|
21
|
+
#
|
|
22
|
+
# Note that if the input is a {Mustermann::Pattern}, Regexp or Symbol, the type option is ignored and if to_pattern is
|
|
23
|
+
# called on the object, the type will be handed on but might be ignored by the input object.
|
|
24
|
+
#
|
|
25
|
+
# If you want to enforce the pattern type, you should create them via their expected class.
|
|
26
|
+
#
|
|
27
|
+
# @example creating patterns
|
|
28
|
+
# require 'mustermann'
|
|
29
|
+
#
|
|
30
|
+
# Mustermann.new("/:name") # => #<Mustermann::Sinatra:"/example">
|
|
31
|
+
# Mustermann.new("/{name}", type: :template) # => #<Mustermann::Template:"/{name}">
|
|
32
|
+
# Mustermann.new(/.*/) # => #<Mustermann::Regular:".*">
|
|
33
|
+
# Mustermann.new(:name, capture: :word) # => #<Mustermann::Sinatra:":name">
|
|
34
|
+
# Mustermann.new("/", "/*.jpg", type: :shell) # => #<Mustermann::Composite:(shell:"/" | shell:"/*.jpg")>
|
|
35
|
+
#
|
|
36
|
+
# @example using custom #to_pattern
|
|
37
|
+
# require 'mustermann'
|
|
38
|
+
#
|
|
39
|
+
# class MyObject
|
|
40
|
+
# def to_pattern(**options)
|
|
41
|
+
# Mustermann.new("/:name", **options)
|
|
42
|
+
# end
|
|
43
|
+
# end
|
|
44
|
+
#
|
|
45
|
+
# Mustermann.new(MyObject.new, type: :rails) # => #<Mustermann::Rails:"/:name">
|
|
46
|
+
#
|
|
47
|
+
# @example enforcing type
|
|
48
|
+
# require 'mustermann/sinatra'
|
|
49
|
+
#
|
|
50
|
+
# Mustermann::Sinatra.new("/:name")
|
|
51
|
+
#
|
|
52
|
+
# @param [String, Pattern, Regexp, Symbol, #to_pattern, Array<String, Pattern, Regexp, Symbol, #to_pattern>]
|
|
53
|
+
# input The representation of the pattern
|
|
6
54
|
# @param [Hash] options The options hash
|
|
7
55
|
# @return [Mustermann::Pattern] pattern corresponding to string.
|
|
8
56
|
# @raise (see [])
|
|
9
57
|
# @raise (see Mustermann::Pattern.new)
|
|
58
|
+
# @raise [TypeError] if the passed object cannot be converted to a pattern
|
|
10
59
|
# @see file:README.md#Types_and_Options "Types and Options" in the README
|
|
11
|
-
def self.new(
|
|
12
|
-
|
|
60
|
+
def self.new(*input, type: DEFAULT_TYPE, **options)
|
|
61
|
+
type ||= DEFAULT_TYPE
|
|
62
|
+
input = input.first if input.size < 2
|
|
63
|
+
case input
|
|
64
|
+
when Pattern then input
|
|
65
|
+
when Regexp then self[:regexp].new(input, **options)
|
|
66
|
+
when String then self[type].new(input, **options)
|
|
67
|
+
when Symbol then self[:sinatra].new(input.inspect, **options)
|
|
68
|
+
when Array then Composite.new(input, type: type, **options)
|
|
69
|
+
else
|
|
70
|
+
pattern = input.to_pattern(type: type, **options) if input.respond_to? :to_pattern
|
|
71
|
+
raise TypeError, "#{input.class} can't be coerced into Mustermann::Pattern" if pattern.nil?
|
|
72
|
+
pattern
|
|
73
|
+
end
|
|
13
74
|
end
|
|
14
75
|
|
|
76
|
+
@mutex ||= Mutex.new
|
|
77
|
+
@types ||= {}
|
|
78
|
+
|
|
15
79
|
# Maps a type to its factory.
|
|
16
80
|
#
|
|
17
81
|
# @example
|
|
18
82
|
# Mustermann[:sinatra] # => Mustermann::Sinatra
|
|
19
83
|
#
|
|
20
|
-
# @param [Symbol]
|
|
84
|
+
# @param [Symbol] name a pattern type identifier
|
|
21
85
|
# @raise [ArgumentError] if the type is not supported
|
|
22
86
|
# @return [Class, #new] pattern factory
|
|
23
|
-
def self.[](
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
87
|
+
def self.[](name)
|
|
88
|
+
return name if name.respond_to? :new
|
|
89
|
+
@types.fetch(normalized = normalized_type(name)) do
|
|
90
|
+
@mutex.synchronize do
|
|
91
|
+
error = try_require "mustermann/#{normalized}"
|
|
92
|
+
@types.fetch(normalized) { raise ArgumentError, "unsupported type %p#{" (#{error.message})" if error}" % name }
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# @return [LoadError, nil]
|
|
98
|
+
# @!visibility private
|
|
99
|
+
def self.try_require(path)
|
|
100
|
+
require(path)
|
|
101
|
+
nil
|
|
102
|
+
rescue LoadError => error
|
|
103
|
+
raise(error) unless error.path == path
|
|
104
|
+
error
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @!visibility private
|
|
108
|
+
def self.register(name, type)
|
|
109
|
+
@types[normalized_type(name)] = type
|
|
27
110
|
end
|
|
28
111
|
|
|
29
112
|
# @!visibility private
|
|
30
|
-
def self.
|
|
31
|
-
|
|
32
|
-
@register[identifier] = [constant, load] if identifier
|
|
33
|
-
@register
|
|
113
|
+
def self.normalized_type(type)
|
|
114
|
+
type.to_s.gsub('-', '_').downcase
|
|
34
115
|
end
|
|
35
116
|
|
|
36
117
|
# @!visibility private
|
|
@@ -39,11 +120,13 @@ module Mustermann
|
|
|
39
120
|
require 'mustermann/extension'
|
|
40
121
|
object.register Extension
|
|
41
122
|
end
|
|
123
|
+
end
|
|
42
124
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
125
|
+
# :nocov:
|
|
126
|
+
begin
|
|
127
|
+
require 'mustermann/visualizer' if defined?(Pry) or defined?(IRB)
|
|
128
|
+
rescue LoadError => error
|
|
129
|
+
raise error unless error.path == 'mustermann/visualizer'
|
|
130
|
+
$stderr.puts(error.message) if caller_locations[1].absolute_path =~ %r{/lib/pry/|/irb/|^\((?:irb|pry)\)$}
|
|
131
|
+
end
|
|
132
|
+
# :nocov:
|
data/mustermann.gemspec
CHANGED
|
@@ -10,19 +10,9 @@ Gem::Specification.new do |s|
|
|
|
10
10
|
s.summary = %q{use patterns like regular expressions}
|
|
11
11
|
s.description = %q{library implementing patterns that behave like regular expressions}
|
|
12
12
|
s.license = 'MIT'
|
|
13
|
+
s.required_ruby_version = '>= 2.1.0'
|
|
13
14
|
s.files = `git ls-files`.split("\n")
|
|
14
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
15
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
16
|
-
s.
|
|
17
|
-
s.require_path = 'lib'
|
|
18
|
-
s.required_ruby_version = '>= 2.0.0'
|
|
19
|
-
|
|
20
|
-
s.add_development_dependency 'rspec'
|
|
21
|
-
s.add_development_dependency 'sinatra', '~> 1.4'
|
|
22
|
-
s.add_development_dependency 'rack-test'
|
|
23
|
-
s.add_development_dependency 'rake'
|
|
24
|
-
s.add_development_dependency 'yard'
|
|
25
|
-
s.add_development_dependency 'redcarpet'
|
|
26
|
-
s.add_development_dependency 'simplecov'
|
|
27
|
-
s.add_development_dependency 'coveralls'
|
|
17
|
+
s.add_dependency 'tool', '~> 0.2'
|
|
28
18
|
end
|
data/spec/ast_spec.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'support'
|
|
2
|
+
require 'mustermann/ast/node'
|
|
3
|
+
|
|
4
|
+
describe Mustermann::AST do
|
|
5
|
+
describe :type do
|
|
6
|
+
example { Mustermann::AST::Node[:char].type .should be == :char }
|
|
7
|
+
example { Mustermann::AST::Node[:char].new.type .should be == :char }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
describe :min_size do
|
|
11
|
+
example { Mustermann::AST::Node[:char].new.min_size.should be == 1 }
|
|
12
|
+
example { Mustermann::AST::Node[:node].new.min_size.should be == 0 }
|
|
13
|
+
end
|
|
14
|
+
end
|