0
@@ -15,32 +15,48 @@ module Waves
0
functor( :match, nil, String ) { |pattern, path| {} }
0
# an empty pattern array matches root, i.e. "/"
0
functor( :match, [], '/' ) { | pattern, path | {} }
0
+ # otherwise break the path down into an array and match arrays
0
functor( :match, Array, String ) { | pattern, path | match( pattern, path.split('/')[1..-1] ) }
0
+ # this variation should never come up ... right?
0
functor( :match, Array, nil ) { | pattern, path | nil }
0
+ # alright, now we are into the general case, matching two arrays ...
0
functor( :match, Array, Array ) do | wants, gots |
0
- r = {}; matches = wants.all? do | want |
0
- if r[true] || want == true
0
- r[true] ||= []; r[true] << gots.shift
0
- match( r, want, gots.shift )
0
+ r = {}; if wants.length > gots.length
0
+ # pad gots with nils so they are the same length
0
+ gots = ( gots + ( [nil] * ( wants.length - gots.length ) ) )
0
+ elsif wants.length < gots.length
0
+ # true is a wildcard matcher ...
0
+ return false unless wants.last == true
0
+ # special case where true is the entire pattern
0
+ return { true => gots } if wants.size == 1
0
+ # collapse last n elements down to an array to match true
0
+ r[ true ] = gots[ wants.size-2..-1 ]
0
+ gots = gots[ 0..wants.size-2 ]
0
- r if
matches and gots.empty?0
+ r if
wants.zip( gots ).all? { |want, got| match( r, want, got ) }0
functor( :match, Hash, String, String ) { | r, want, got | got if want == got }
0
functor( :match, Hash, Regexp, String ) { | r, want, got | got if want === got }
0
- #
placeholder Symbols use a default regex for matching0
+ #
a symbol matches pretty much anything and stores it as a param ...0
functor( :match, Hash, Symbol, String ) do | r, want, got |
0
- r[ want.to_s ] =
got if match( r, /^([\w\_\-\#]+)$/, got )
0
+ r[ want.to_s ] =
match( r, /^(\S+)$/, got )
0
- functor( :match, Hash, Hash, String ) do | r, want, got |
0
+ # a hash is either a param with a custom regexp or a default value ...
0
+ functor( :match, Hash, Hash, Object ) do | r, want, got |
0
+ key, want = want.first ; match( r, key, want, got )
0
+ functor( :match, Hash, Symbol, String, String ) do | r, key, want, got |
0
- # hashes represent optional values with a default
0
- functor( :match, Hash, Hash, nil ) { | r, want, got | r[ want.keys.first.to_s ] = want.values.first }
0
- # everything else is mandatory ...
0
- functor( :match, Hash, Object, nil ) { | r, want, got | false }
0
+ functor( :match, Hash, Symbol, String, nil ) do | r, key, want, got |
0
+ functor( :match, Hash, Symbol, Regexp, String ) do | r, key, want, got |
0
+ r[ key.to_s ] = match( r, want, got )
0
+ functor( :match, Hash, Symbol, Regexp, nil ) do | r, key, want, got |
Comments
No one has commented yet.