class Grimm::Router

Attributes

routes[R]

Public Class Methods

match_verbs(*verbs) click to toggle source
# File lib/grimm/routing.rb, line 8
def self.match_verbs(*verbs)
  verbs.each do |verb|
    define_method(verb) do |url, options = {}|
      url_parts = url.split("/")
      url_parts.select! { |part| !part.empty? }
      placeholder = []
      regexp_parts = url_parts.map do |part|
        check_part(placeholder, part)
      end
      save_routes(regexp_parts, verb, placeholder, options)
    end
  end
end
new() click to toggle source
# File lib/grimm/routing.rb, line 4
def initialize
  @routes = Hash.new { |hash, key| hash[key] = [] }
end

Public Instance Methods

check_part(placeholder, part) click to toggle source
# File lib/grimm/routing.rb, line 22
def check_part(placeholder, part)
  if part[0] == ":"
    placeholder << part[1..-1]
    "([A-Za-z0-9_]+)"
  else
    part
  end
end
check_url(request) click to toggle source
# File lib/grimm/routing.rb, line 58
def check_url(request)
  url = request.path_info
  verb = request.request_method.downcase.to_sym
  route_match = routes[verb].detect do |route|
    route.first.match(url)
  end
  find_match(route_match, url, request)
end
convert_target(request, route) click to toggle source
# File lib/grimm/routing.rb, line 81
def convert_target(request, route)
  controller_name = route[:controller].camelcase
  controller = Object.const_get("#{controller_name}Controller")
  controller.action(request, route[:target])
end
draw(&block) click to toggle source
# File lib/grimm/routing.rb, line 39
def draw(&block)
  instance_eval(&block)
end
find_match(route_match, url, request) click to toggle source
# File lib/grimm/routing.rb, line 67
def find_match(route_match, url, request)
  if route_match
    placeholder = {}
    match = route_match.first.match(url)
    held_value = route_match[2]
    held_value.each_with_index do |value, index|
      placeholder[value] = match.captures[index]
    end
    request.params.merge!(placeholder)
    route_match << placeholder
    return convert_target(request, route_match[1])
  end
end
resources(args) click to toggle source
# File lib/grimm/routing.rb, line 47
def resources(args)
  args = args.to_s
  get("/#{args}", to: "#{args}#index")
  get("/#{args}/new", to: "#{args}#new")
  get("/#{args}/:id", to: "#{args}#show")
  get("/#{args}/edit/:id", to: "#{args}#edit")
  delete("/#{args}/:id", to: "#{args}#destroy")
  post("/#{args}/", to: "#{args}#create")
  put("/#{args}/:id", to: "#{args}#update")
end
root(address) click to toggle source
# File lib/grimm/routing.rb, line 43
def root(address)
  get("/", to: address)
end
save_routes(regexp_parts, verb, placeholder, options) click to toggle source
# File lib/grimm/routing.rb, line 31
def save_routes(regexp_parts, verb, placeholder, options)
  regexp = regexp_parts.join("/")
  routes[verb] << [Regexp.new("^/#{regexp}$"),
                   parse_to(options[:to]), placeholder]
end

Private Instance Methods

parse_to(option) click to toggle source
# File lib/grimm/routing.rb, line 89
def parse_to(option)
  controller, target = option.split("#")
  { controller: controller.camelcase, target: target }
end