Class: Xhive::Router::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/xhive/router/route.rb

Constant Summary collapse

@@routes =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/xhive/router/route.rb', line 6

def action
  @action
end

#argsObject (readonly)

Returns the value of attribute args.



6
7
8
# File 'lib/xhive/router/route.rb', line 6

def args
  @args
end

#asObject (readonly)

Returns the value of attribute as.



6
7
8
# File 'lib/xhive/router/route.rb', line 6

def as
  @as
end

#inlineObject (readonly)

Returns the value of attribute inline.



6
7
8
# File 'lib/xhive/router/route.rb', line 6

def inline
  @inline
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/xhive/router/route.rb', line 6

def klass
  @klass
end

#routeObject (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.

Returns:

  • (Boolean)


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