Class: Xhive::Router::Route
- Inherits:
-
Object
- Object
- Xhive::Router::Route
- Defined in:
- lib/xhive/router/route.rb
Constant Summary collapse
- @@routes =
[]
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#inline ⇒ Object
readonly
Returns the value of attribute inline.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
Class Method Summary collapse
-
.add(route, klass, action, inline) ⇒ Object
Public: adds a new route to the collection.
-
.find(url) ⇒ Object
Public: finds the route that matches the url.
Instance Method Summary collapse
-
#initialize(route, klass, action, opts = {}) ⇒ Route
constructor
A new instance of Route.
-
#matches?(url) ⇒ Boolean
Public: checks if the route matches the url.
-
#params_from(url) ⇒ Object
Public: returns the parameters present in the url.
Constructor Details
#initialize(route, klass, action, opts = {}) ⇒ Route
Returns a new instance of Route.
8 9 10 11 12 13 14 15 |
# File 'lib/xhive/router/route.rb', line 8 def initialize(route, klass, action, opts={}) @route = route @klass = klass @action = action @args = extract_args(route) @inline = opts[:inline] || false @as = opts[:as].to_s || klass end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
6 7 8 |
# File 'lib/xhive/router/route.rb', line 6 def action @action end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
6 7 8 |
# File 'lib/xhive/router/route.rb', line 6 def args @args end |
#as ⇒ Object (readonly)
Returns the value of attribute as.
6 7 8 |
# File 'lib/xhive/router/route.rb', line 6 def as @as end |
#inline ⇒ Object (readonly)
Returns the value of attribute inline.
6 7 8 |
# File 'lib/xhive/router/route.rb', line 6 def inline @inline end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/xhive/router/route.rb', line 6 def klass @klass end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
6 7 8 |
# File 'lib/xhive/router/route.rb', line 6 def route @route end |
Class Method Details
.add(route, klass, action, inline) ⇒ Object
Public: adds a new route to the collection.
route - The String containing the route definition. klass - The String containing the Controller/Cell class name. action - The String containing the action name. inline - The Boolean indicating if it should be rendered inline or as an AJAX widget.
24 25 26 |
# File 'lib/xhive/router/route.rb', line 24 def self.add(route, klass, action, inline) @@routes << new(route, klass, action, inline) end |
.find(url) ⇒ Object
Public: finds the route that matches the url.
url - The String containing the url.
Returns: a Route object that matches the url.
34 35 36 |
# File 'lib/xhive/router/route.rb', line 34 def self.find(url) @@routes.select {|r| r.matches?(url) }.first end |
Instance Method Details
#matches?(url) ⇒ Boolean
Public: checks if the route matches the url.
url - The String containing the url to match.
Returns: true or false.
44 45 46 |
# File 'lib/xhive/router/route.rb', line 44 def matches?(url) !!(matcher =~ url) end |
#params_from(url) ⇒ Object
Public: returns the parameters present in the url.
url - The String containing the url.
Returns: a hash of the form { :id => ‘1’, :name => ‘john’ }
54 55 56 57 |
# File 'lib/xhive/router/route.rb', line 54 def params_from(url) values = url.scan(matcher).flatten params = Hash[args.zip values] end |