Class: NYNY::RouteSignature
- Inherits:
-
Object
- Object
- NYNY::RouteSignature
- Defined in:
- lib/nyny/route_signature.rb
Constant Summary collapse
- NAME_PATTERN =
/:(\S+)/
Instance Attribute Summary collapse
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
- #build_regex(signature) ⇒ Object
-
#initialize(signature) ⇒ RouteSignature
constructor
A new instance of RouteSignature.
- #match(path) ⇒ Object
- #pattern_for(signature) ⇒ Object
Constructor Details
#initialize(signature) ⇒ RouteSignature
Returns a new instance of RouteSignature.
6 7 8 |
# File 'lib/nyny/route_signature.rb', line 6 def initialize signature @pattern = pattern_for signature end |
Instance Attribute Details
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
5 6 7 |
# File 'lib/nyny/route_signature.rb', line 5 def pattern @pattern end |
Instance Method Details
#build_regex(signature) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/nyny/route_signature.rb', line 15 def build_regex signature return %r(^#{signature}$) unless signature.include?(':') groups = signature.split('/').map do |part| next part if part.empty? next part unless part.start_with? ':' name = NAME_PATTERN.match(part)[1] %Q{(?<#{name}>\\S+)} end.select {|s| !s.empty? }.join('\/') %r(^\/#{groups}$) end |
#match(path) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/nyny/route_signature.rb', line 28 def match path data = pattern.match path if data Hash[data.names.map {|n| [n.to_sym, URI.unescape(data[n])]}] end end |
#pattern_for(signature) ⇒ Object
10 11 12 13 |
# File 'lib/nyny/route_signature.rb', line 10 def pattern_for signature return signature if signature.is_a? Regexp build_regex(signature.start_with?('/') ? signature : "/#{signature}") end |