class Microframe::Router

Attributes

mapper[R]
object[R]
request[RW]
routes[R]

Public Class Methods

new() click to toggle source
# File lib/microframe/routing/router.rb, line 9
def initialize
  @routes = Hash.new
end
setup_verbs(*verbs) click to toggle source
# 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

Public Instance Methods

draw(&block) click to toggle source
# File lib/microframe/routing/router.rb, line 48
def draw(&block)
  instance_eval(&block)
  @routes.default = {}
end
handle_request() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

get_handler(verb, path) click to toggle source
# File lib/microframe/routing/router.rb, line 67
def get_handler(verb, path)
  routes[verb][path]
end
get_handler_file(controller) click to toggle source
# File lib/microframe/routing/router.rb, line 81
def get_handler_file(controller)
  require File.join(APP_PATH, "app", "controllers",  controller + "_controller")
end
missing_path() click to toggle source
# File lib/microframe/routing/router.rb, line 85
def missing_path
  [404, {"Content-Type" => "application/html"}, ["<p>We are here but unfortunately, this page: #{request.host}#{request.path_info} isn't. Return home while we keep looking for it.</p>"]]
end
set_route(verb, path, handler = {}) click to toggle source
# File lib/microframe/routing/router.rb, line 71
def set_route(verb, path, handler = {})
  @routes[verb] ||= {}
  @routes[verb][path] = setup_handler(handler)
end
setup_handler(handler) click to toggle source
# File lib/microframe/routing/router.rb, line 76
def setup_handler(handler)
  controller, action = handler[:to].split("#")
  {controller: controller, action: action}
end