Class: YesRoutes

Inherits:
Object
  • Object
show all
Defined in:
lib/yes_routes.rb

Defined Under Namespace

Classes: RouteDoesNotExistError, RouteParamsNilError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.pathsObject

Returns the value of attribute paths.



8
9
10
# File 'lib/yes_routes.rb', line 8

def paths
  @paths
end

.routesObject

Returns the value of attribute routes.



8
9
10
# File 'lib/yes_routes.rb', line 8

def routes
  @routes
end

Class Method Details

.action(path_string, class_name) ⇒ Object



38
39
40
41
# File 'lib/yes_routes.rb', line 38

def action(path_string, class_name)
  match("GET", path_string, class_name, :new)
  match("POST", path_string, class_name, :create)
end

.actions(path_string, class_name) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/yes_routes.rb', line 43

def actions(path_string, class_name)
  match("GET", path_string, class_name, :show)
  match("GET", "#{path_string}/new", class_name, :new)
  match("POST", "#{path_string}/new", class_name, :create)
  match("GET", "#{path_string}/edit", class_name, :edit)
  match("POST", "#{path_string}/edit", class_name, :update)
  match("POST", "#{path_string}/delete", class_name, :delete)
end

.get(path_string, class_name, method_name) ⇒ Object



30
31
32
# File 'lib/yes_routes.rb', line 30

def get(path_string, class_name, method_name)
  match("GET", path_string, class_name, method_name)
end

.match(verb, path_string, class_name, method_name) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/yes_routes.rb', line 10

def match(verb, path_string, class_name, method_name)
  @paths ||= {}
  @paths[[class_name, method_name]] = path_string

  @routes ||= Hash.new { |hash, key| hash[key] = [] }
  @routes[verb] << [path_string, class_name, method_name]
end

.path(class_name, method_name, params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yes_routes.rb', line 18

def path(class_name, method_name, params = {})
  path_string = paths[[class_name, method_name]]

  raise RouteParamsNilError, "Route params for :#{class_name}, :#{method_name} cannot be nil" if params.nil?

  params = params.to_param if params.is_a?(YesRecord)

  raise RouteDoesNotExistError, "Route for class #{class_name} and method #{method_name} doesn't exist" unless path_string

  sub_params_in_path(path_string, params)
end

.post(path_string, class_name, method_name) ⇒ Object



34
35
36
# File 'lib/yes_routes.rb', line 34

def post(path_string, class_name, method_name)
  match("POST", path_string, class_name, method_name)
end

.resource(path_string, class_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yes_routes.rb', line 52

def resource(path_string, class_name)
  [
    ["GET", :index, ""],
    ["GET", :new, "/new"],
    ["POST", :create, "/new"],
    ["GET", :show, "/:id"],
    ["GET", :edit, "/:id/edit"],
    ["POST", :update, "/:id/edit"],
    ["POST", :delete, "/:id/delete"]
  ].each do |method, method_name, suffix|
    match(method, full_path(path_string, suffix), class_name, method_name)
  end
end

.resources(path_string, class_name) ⇒ Object

Raises:

  • (StandardError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/yes_routes.rb', line 66

def resources(path_string, class_name)
  raise StandardError, "Needs at least two url segments" if path_string.split("/").size < 2
  raise StandardError, "The last url segment needs to be a param" unless path_string.split("/").last.include?(":")

  *prefix, _ = path_string.split("/")
  prefix = prefix.join("/")

  [
    ["GET", :index, "", :plural],
    ["GET", :new, "/new", :plural],
    ["POST", :create, "/new", :plural],
    ["GET", :show, ""],
    ["GET", :edit, "/edit"],
    ["POST", :update, "/edit"],
    ["POST", :delete, "/delete"]
  ].each do |method, method_name, suffix, plural|
    if plural
      match(method, full_path(prefix, suffix), class_name, method_name)
    else
      match(method, full_path(path_string, suffix), class_name, method_name)
    end
  end
end