Class: Orbit::Router

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/orbit/router.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Singleton

included, #instantiate

Constructor Details

#initializeRouter

Returns a new instance of Router.



6
7
8
9
10
# File 'lib/orbit/router.rb', line 6

def initialize
  instantiate

  @routes = {}
end

Instance Attribute Details

#routesObject

Returns the value of attribute routes.



4
5
6
# File 'lib/orbit/router.rb', line 4

def routes
  @routes
end

Class Method Details

.add(verb, path, controller, action) ⇒ Object



47
48
49
# File 'lib/orbit/router.rb', line 47

def self.add(verb, path, controller, action)
  instance.add(verb, path, controller, action)
end

.match(verb, path) ⇒ Object



12
13
14
# File 'lib/orbit/router.rb', line 12

def self.match(verb, path)
  instance.match(verb, path)
end

.routesObject



43
44
45
# File 'lib/orbit/router.rb', line 43

def self.routes
  instance.routes
end

Instance Method Details

#add(verb, path, controller, action) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/orbit/router.rb', line 51

def add(verb, path, controller, action)
  verb = verb.downcase.to_sym
  path = path.gsub('//', '/')

  routes[verb] ||= {}
  routes[verb][path] = { class: controller, action: action, route: Orbit::Config.route_class.new(path) }

  re_sort_routes(verb)
end

#match(verb, path) ⇒ Object



16
17
18
19
20
# File 'lib/orbit/router.rb', line 16

def match(verb, path)
  verb = verb.downcase.to_sym

  matching_route(verb, path)
end

#matching_route(verb, path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/orbit/router.rb', line 28

def matching_route(verb, path)
  routes_for_verb = routes_for_verb(verb)
  route = routes_for_verb.fetch(path, nil)

  unless route
    route = routes_for_verb.find do |pattern, options|
      options[:route].path.regex.match(path)
    end

    route = route.last if route
  end

  route
end

#routes_for_verb(verb) ⇒ Object



22
23
24
25
26
# File 'lib/orbit/router.rb', line 22

def routes_for_verb(verb)
  @routes_for_verb ||= {}

  @routes_for_verb[verb] ||= routes.fetch(verb, {})
end