Class: Grimm::Router

Inherits:
Object show all
Defined in:
lib/grimm/routing.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



4
5
6
# File 'lib/grimm/routing.rb', line 4

def initialize
  @routes = Hash.new { |hash, key| hash[key] = [] }
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



3
4
5
# File 'lib/grimm/routing.rb', line 3

def routes
  @routes
end

Class Method Details

.match_verbs(*verbs) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#check_part(placeholder, part) ⇒ Object



22
23
24
25
26
27
28
29
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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) ⇒ Object



81
82
83
84
85
# 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) ⇒ Object



39
40
41
# File 'lib/grimm/routing.rb', line 39

def draw(&block)
  instance_eval(&block)
end

#find_match(route_match, url, request) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



43
44
45
# File 'lib/grimm/routing.rb', line 43

def root(address)
  get("/", to: address)
end

#save_routes(regexp_parts, verb, placeholder, options) ⇒ Object



31
32
33
34
35
# 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