Class: Microframe::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/microframe/routing/router.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter



9
10
11
# File 'lib/microframe/routing/router.rb', line 9

def initialize
  @routes = Hash.new
end

Instance Attribute Details

#mapperObject (readonly)

Returns the value of attribute mapper.



6
7
8
# File 'lib/microframe/routing/router.rb', line 6

def mapper
  @mapper
end

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/microframe/routing/router.rb', line 6

def object
  @object
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/microframe/routing/router.rb', line 7

def request
  @request
end

#routesObject (readonly)

Returns the value of attribute routes.



6
7
8
# File 'lib/microframe/routing/router.rb', line 6

def routes
  @routes
end

Class Method Details

.setup_verbs(*verbs) ⇒ Object



40
41
42
43
44
# File 'lib/microframe/routing/router.rb', line 40

def self.setup_verbs(*verbs)
  verbs.each do |verb|
    define_method(verb) { |path, handler| set_route(verb.to_s.upcase, path, handler) }
  end
end

Instance Method Details

#draw(&block) ⇒ Object



48
49
50
51
# File 'lib/microframe/routing/router.rb', line 48

def draw(&block)
  instance_eval(&block)
  @routes.default = {}
end

#handle_requestObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/microframe/routing/router.rb', line 13

def handle_request
  verb = request.request_method
  path = request.path_info
  @mapper ||= Mapper.start(routes)
  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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/microframe/routing/router.rb', line 53

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")
  put("/#{name}/:id", to: "#{name}#update")
  delete("/#{name}/:id", to: "#{name}#destroy")
end

#setup_controller(handler) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/microframe/routing/router.rb', line 30

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