Class: Microframe::Router
- Inherits:
-
Object
- Object
- Microframe::Router
- Defined in:
- lib/microframe/routing/router.rb
Instance Attribute Summary collapse
-
#mapper ⇒ Object
readonly
Returns the value of attribute mapper.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#request ⇒ Object
Returns the value of attribute request.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
Class Method Summary collapse
Instance Method Summary collapse
- #draw(&block) ⇒ Object
- #handle_request(verb, path) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #resources(name) ⇒ Object
- #setup_controller(handler) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
9 10 11 |
# File 'lib/microframe/routing/router.rb', line 9 def initialize @route = Hash.new end |
Instance Attribute Details
#mapper ⇒ Object (readonly)
Returns the value of attribute mapper.
6 7 8 |
# File 'lib/microframe/routing/router.rb', line 6 def mapper @mapper end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
6 7 8 |
# File 'lib/microframe/routing/router.rb', line 6 def object @object end |
#request ⇒ Object
Returns the value of attribute request.
7 8 9 |
# File 'lib/microframe/routing/router.rb', line 7 def request @request end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
6 7 8 |
# File 'lib/microframe/routing/router.rb', line 6 def route @route end |
Class Method Details
.setup_verbs(*verbs) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/microframe/routing/router.rb', line 37 def self.setup_verbs(*verbs) verbs.each do |verb| verb = verb.to_s define_method(verb) { |path, handler| set_route(verb.upcase, path, handler) } end end |
Instance Method Details
#draw(&block) ⇒ Object
46 47 48 49 |
# File 'lib/microframe/routing/router.rb', line 46 def draw(&block) instance_eval(&block) @route.default = {} end |
#handle_request(verb, path) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/microframe/routing/router.rb', line 13 def handle_request(verb, path) @mapper ||= Mapper.start(route) handler = @mapper.map(verb, path) #get_handler(verb, path) return missing_path unless handler @request.params.merge!(@mapper.placeholders) response = setup_controller(handler) unless object.view_rendered response = object.render_view end response end |
#resources(name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/microframe/routing/router.rb', line 51 def resources(name) name = name.to_s get("/#{name}", to: "#{name}#index") get("/#{name}/new", to: "#{name}#new") get("/#{name}/:id", to: "#{name}#show") get("/#{name}/:id/edit", to: "#{name}#edit") post("/#{name}", to: "#{name}#create") patch("/#{name}/:id", to: "#{name}#update") delete("/name/:id", to: "#{name}#destroy") end |
#setup_controller(handler) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/microframe/routing/router.rb', line 27 def setup_controller(handler) controller = handler[:controller] action = handler[:action] get_handler_file(controller) @object = Module.const_get(controller.capitalize + "Controller").new(request, controller, action) object.send(action.to_sym) end |