Class: Lydia::Route
- Inherits:
-
Object
- Object
- Lydia::Route
- Defined in:
- lib/lydia/route.rb
Constant Summary collapse
- WILDCARD_REGEX =
/\/\*(.*)/.freeze
- NAMED_SEGMENTS_REGEX =
/\/([^\/]*):([^:$\/]+)/.freeze
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
Instance Method Summary collapse
-
#initialize(request_method, namespace, pattern, options = {}, &block) ⇒ Route
constructor
A new instance of Route.
- #match?(env) ⇒ Boolean
Constructor Details
#initialize(request_method, namespace, pattern, options = {}, &block) ⇒ Route
Returns a new instance of Route.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lydia/route.rb', line 8 def initialize(request_method, namespace, pattern, = {}, &block) @request_method = request_method @namespace = namespace @pattern = pattern = @block = block if pattern.is_a? String path = (namespace || '') + pattern if path.match(WILDCARD_REGEX) result = path.gsub(WILDCARD_REGEX, '(?:/(.*)|)') elsif path.match(NAMED_SEGMENTS_REGEX) result = path.gsub(NAMED_SEGMENTS_REGEX, '/\1(?<\2>[^.$/]+)') else result = path end @regexp = Regexp.new("\\A#{result}\\z") elsif pattern.is_a?(Regexp) @regexp = Regexp.new((namespace || '') + pattern.to_s) else raise ArgumentError.new('Pattern must be a string or a regex') end end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def block @block end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def params @params end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def pattern @pattern end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def regexp @regexp end |
#request_method ⇒ Object (readonly)
Returns the value of attribute request_method.
3 4 5 |
# File 'lib/lydia/route.rb', line 3 def request_method @request_method end |
Instance Method Details
#match?(env) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/lydia/route.rb', line 31 def match?(env) match = @regexp.match("#{env['PATH_INFO']}") @params = Hash[match.names.map(&:to_sym).zip(match.captures)] if match && match.names.size match end |