Class: Router
- Inherits:
-
Object
- Object
- Router
- Includes:
- UrlHelpers
- Defined in:
- lib/scaffold/lib/router/router.rb
Instance Attribute Summary collapse
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #add_route(pattern, method, controller_class, action_name) ⇒ Object
- #collection(&prc) ⇒ Object
- #display_routes ⇒ Object
- #draw(&proc) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(req) ⇒ Object
- #member(&prc) ⇒ Object
- #resource(name, options = {}, &prc) ⇒ Object
- #resources(name, options = {}, &prc) ⇒ Object
- #root(options = {}) ⇒ Object
- #run(req, res) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
8 9 10 11 12 13 14 15 16 |
# File 'lib/scaffold/lib/router/router.rb', line 8 def initialize @routes = [] @patterns = [] @current_route = "^/" @member_route = "^/" @collection_route = "^/" @nested_route = "^/" @current_class = nil; end |
Instance Attribute Details
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
6 7 8 |
# File 'lib/scaffold/lib/router/router.rb', line 6 def patterns @patterns end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
6 7 8 |
# File 'lib/scaffold/lib/router/router.rb', line 6 def routes @routes end |
Instance Method Details
#add_route(pattern, method, controller_class, action_name) ⇒ Object
30 31 32 33 |
# File 'lib/scaffold/lib/router/router.rb', line 30 def add_route(pattern, method, controller_class, action_name) routes << Route.new(pattern, method, controller_class, action_name) patterns << pattern unless patterns.include?(pattern) end |
#collection(&prc) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/scaffold/lib/router/router.rb', line 139 def collection(&prc) self.current_route = collection_route prc.call if prc self.current_route = nested_route nil end |
#display_routes ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/scaffold/lib/router/router.rb', line 18 def display_routes routes.each do |route| url = Router.make_url(route.pattern) method = route.http_method controller = route.controller_class helper = Router.make_helper_name(route.pattern) action = route.action_name puts "#{method.to_s.upcase}: #{url}, #{controller}##{action}, #{helper}" end end |
#draw(&proc) ⇒ Object
35 36 37 |
# File 'lib/scaffold/lib/router/router.rb', line 35 def draw(&proc) self.instance_eval(&proc) end |
#match(req) ⇒ Object
67 68 69 |
# File 'lib/scaffold/lib/router/router.rb', line 67 def match(req) routes.find { |route| route.matches?(req) } end |
#member(&prc) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/scaffold/lib/router/router.rb', line 131 def member(&prc) self.current_route = member_route prc.call if prc self.current_route = nested_route nil end |
#resource(name, options = {}, &prc) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/scaffold/lib/router/router.rb', line 101 def resource(name, = {}, &prc) controller_class = "#{name.capitalize}Controller".constantize methods = [:index, :create, :new, :edit, :update, :show, :destroy] patterns = { index: [Regexp.new("#{current_route}#{name}/?$"), :get], new: [Regexp.new("#{current_route}#{name}/new/?$"), :get], show: [Regexp.new("#{current_route}#{name}/?$"), :get], create: [Regexp.new("#{current_route}#{name}$"), :post], destroy: [Regexp.new("#{current_route}#{name}/?$"), :delete], update: [Regexp.new("#{current_route}#{name}/?$"), :patch], edit: [Regexp.new("#{current_route}#{name}/edit/?$"), :get] } default = { only: methods, except: [] } default.merge!() names = default[:only] - default[:except] names.each do |name| params = patterns[name] + [controller_class] + [name] add_route(*params) end self.current_class = name set_resource_routes(name) prc.call if prc reset_routes nil end |
#resources(name, options = {}, &prc) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/scaffold/lib/router/router.rb', line 71 def resources(name, = {}, &prc) controller_class = "#{name.capitalize}Controller".constantize methods = [:index, :create, :new, :edit, :update, :show, :destroy] patterns = { index: [Regexp.new("#{current_route}#{name}/?$"), :get], new: [Regexp.new("#{current_route}#{name}/new/?$"), :get], show: [Regexp.new("#{current_route}#{name}/(?<id>\\d+)/?$"), :get], create: [Regexp.new("#{current_route}#{name}$"), :post], destroy: [Regexp.new("#{current_route}#{name}/(?<id>\\d+)/?$"), :delete], update: [Regexp.new("#{current_route}#{name}/(?<id>\\d+)/?$"), :patch], edit: [Regexp.new("#{current_route}#{name}/(?<id>\\d+)/edit/?$"), :get] } default = { only: methods, except: [] } default.merge!() names = default[:only] - default[:except] names.each do |name| params = patterns[name] + [controller_class] + [name] add_route(*params) end self.current_class = name set_resources_routes(name) prc.call if prc reset_routes nil end |
#root(options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/scaffold/lib/router/router.rb', line 147 def root( = {}) to_array = [:to].split('#') pattern = Regexp.new("^/?$") http_method = :get controller_class = "#{to_array[0].capitalize}Controller".constantize action_name = to_array[1].to_sym add_route(pattern, http_method, controller_class, action_name) end |
#run(req, res) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/scaffold/lib/router/router.rb', line 157 def run(req, res) route = match(req) if route route.run(req, res, patterns) else res.status = 404 res.write('Route not found') end end |