Class: Rory::Route
- Inherits:
-
Object
- Object
- Rory::Route
- Defined in:
- lib/rory/route.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#mask ⇒ Object
readonly
Returns the value of attribute mask.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(mask, options = {}) ⇒ Route
constructor
A new instance of Route.
- #matches_request?(path, method) ⇒ Boolean
- #methods ⇒ Object
- #module ⇒ Object
- #name ⇒ Object
- #path_params(path) ⇒ Object
- #regex ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(mask, options = {}) ⇒ Route
Returns a new instance of Route.
5 6 7 8 9 |
# File 'lib/rory/route.rb', line 5 def initialize(mask, = {}) @mask = mask.gsub(/^\//, '') @options = @controller, @action = [:to].split('#') end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/rory/route.rb', line 3 def action @action end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
3 4 5 |
# File 'lib/rory/route.rb', line 3 def controller @controller end |
#mask ⇒ Object (readonly)
Returns the value of attribute mask.
3 4 5 |
# File 'lib/rory/route.rb', line 3 def mask @mask end |
Instance Method Details
#==(other) ⇒ Object
15 16 17 |
# File 'lib/rory/route.rb', line 15 def ==(other) to_h == other.to_h end |
#matches_request?(path, method) ⇒ Boolean
31 32 33 34 35 36 |
# File 'lib/rory/route.rb', line 31 def matches_request?(path, method) @match = regex.match(path) @match && (methods.empty? || methods.include?(method.to_sym)) end |
#methods ⇒ Object
27 28 29 |
# File 'lib/rory/route.rb', line 27 def methods @options[:methods] || [] end |
#module ⇒ Object
23 24 25 |
# File 'lib/rory/route.rb', line 23 def module @options[:module] end |
#name ⇒ Object
11 12 13 |
# File 'lib/rory/route.rb', line 11 def name "#{controller}_#{action}" end |
#path_params(path) ⇒ Object
38 39 40 41 42 |
# File 'lib/rory/route.rb', line 38 def path_params(path) @match ||= regex.match(path) symbolized_param_names = @match.names.map { |name| name.to_sym } Hash[symbolized_param_names.zip(@match.captures)] end |
#regex ⇒ Object
19 20 21 |
# File 'lib/rory/route.rb', line 19 def regex /^#{@mask.gsub(/:([\w_]+)/, "(?<\\1>\[\^\\\/\]+)")}$/ end |
#to_h ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/rory/route.rb', line 44 def to_h { :mask => @mask, :controller => @controller, :action => @action, :module => @module, :methods => @methods } end |