mustermann 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +9 -6
- data/README.md +247 -10
- data/lib/mustermann.rb +13 -5
- data/lib/mustermann/ast/expander.rb +15 -0
- data/lib/mustermann/ast/pattern.rb +2 -3
- data/lib/mustermann/expander.rb +39 -4
- data/lib/mustermann/mapper.rb +94 -0
- data/lib/mustermann/pattern.rb +3 -3
- data/lib/mustermann/regular.rb +26 -0
- data/lib/mustermann/router/rack.rb +2 -2
- data/lib/mustermann/router/simple.rb +1 -1
- data/lib/mustermann/sinatra.rb +1 -1
- data/lib/mustermann/to_pattern.rb +45 -0
- data/lib/mustermann/version.rb +1 -1
- data/mustermann.gemspec +3 -1
- data/spec/expander_spec.rb +28 -0
- data/spec/mapper_spec.rb +83 -0
- data/spec/mustermann_spec.rb +30 -10
- data/spec/pattern_spec.rb +27 -4
- data/spec/regexp_based_spec.rb +1 -1
- data/spec/regular_spec.rb +36 -0
- data/spec/router/rack_spec.rb +1 -1
- data/spec/router/simple_spec.rb +9 -7
- data/spec/shell_spec.rb +1 -1
- data/spec/simple_match_spec.rb +1 -1
- data/spec/sinatra_spec.rb +13 -0
- data/spec/support/env.rb +11 -1
- data/spec/support/expand_matcher.rb +2 -2
- data/spec/support/match_matcher.rb +2 -2
- data/spec/support/pattern.rb +6 -2
- data/spec/to_pattern_spec.rb +20 -0
- metadata +64 -28
- data/lib/mustermann/equality_map.rb +0 -46
@@ -1,46 +0,0 @@
|
|
1
|
-
module Mustermann
|
2
|
-
# A simple wrapper around ObjectSpace::WeakMap that allows matching keys by equality rather than identity.
|
3
|
-
# Used for caching.
|
4
|
-
#
|
5
|
-
# @see #fetch
|
6
|
-
# @!visibility private
|
7
|
-
class EqualityMap
|
8
|
-
# @!visibility private
|
9
|
-
def initialize
|
10
|
-
@keys = {}
|
11
|
-
@map = ObjectSpace::WeakMap.new
|
12
|
-
end
|
13
|
-
|
14
|
-
# @param [Array<#hash>] key for caching
|
15
|
-
# @yield block that will be called to populate entry if missing
|
16
|
-
# @return value stored in map or result of block
|
17
|
-
# @!visibility private
|
18
|
-
def fetch(*key)
|
19
|
-
identity = @keys[key.hash]
|
20
|
-
key = identity == key ? identity : key
|
21
|
-
|
22
|
-
# it is ok that this is not thread-safe, worst case it has double cost in
|
23
|
-
# generating, object equality is not guaranteed anyways
|
24
|
-
@map[key] ||= track(key, yield)
|
25
|
-
end
|
26
|
-
|
27
|
-
# @param [#hash] key for identifying the object
|
28
|
-
# @param [Object] object to be stored
|
29
|
-
# @return [Object] same as the second parameter
|
30
|
-
def track(key, object)
|
31
|
-
ObjectSpace.define_finalizer(object, finalizer(hash))
|
32
|
-
@keys[key.hash] = key
|
33
|
-
object
|
34
|
-
end
|
35
|
-
|
36
|
-
# Finalizer proc needs to be generated in different scope so it doesn't keep a reference to the object.
|
37
|
-
#
|
38
|
-
# @param [Fixnum] hash for key
|
39
|
-
# @return [Proc] finalizer callback
|
40
|
-
def finalizer(hash)
|
41
|
-
proc { @keys.delete(hash) }
|
42
|
-
end
|
43
|
-
|
44
|
-
private :track, :finalizer
|
45
|
-
end
|
46
|
-
end
|