Class: Rollerskates::Routing::Router

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

Instance Method Summary collapse

Instance Method Details

#actions(options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/rollerskates/routing/router.rb', line 33

def actions(options = {})
  return resources_only(options[:only]) if options[:only]
  return resources_except(options[:except]) if options[:except]
  default_actions
end

#default_actionsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rollerskates/routing/router.rb', line 20

def default_actions
  [
    { action: :index,   method: :get },
    { action: :create,  method: :post },
    { action: :new,     method: :get, suffix: :new },
    { action: :show,    method: :get, placeholder: :id },
    { action: :edit,    method: :get, placeholder: :id, suffix: :edit },
    { action: :destroy, method: :delete, placeholder: :id },
    { action: :update,  method: :put,    placeholder: :id },
    { action: :update,  method: :patch,  placeholder: :id }
  ]
end

#draw(&block) ⇒ Object



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

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

#endpointsObject



72
73
74
# File 'lib/rollerskates/routing/router.rb', line 72

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

#method_path_to(route, controller_name) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/rollerskates/routing/router.rb', line 51

def method_path_to(route, controller_name)
  suffix = route[:suffix] ? "/#{route[:suffix]}" : ""
  placeholder = route[:placeholder] ? "/:#{route[:placeholder]}" : ""
  [
    route[:method],
    "/#{controller_name}#{placeholder}#{suffix}",
    "#{controller_name}##{route[:action]}"
  ]
end

#resources(controller_name, options = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/rollerskates/routing/router.rb', line 61

def resources(controller_name, options = {})
  actions(options).each do |route|
    method, path, action = method_path_to(route, controller_name)
    send(method, path, to: action)
  end
end

#resources_except(*options) ⇒ Object



45
46
47
48
49
# File 'lib/rollerskates/routing/router.rb', line 45

def resources_except(*options)
  default_actions.reject do |action|
    options.include? action[:action]
  end
end

#resources_only(*options) ⇒ Object



39
40
41
42
43
# File 'lib/rollerskates/routing/router.rb', line 39

def resources_only(*options)
  default_actions.select do |action|
    options.include? action[:action]
  end
end

#root(location) ⇒ Object



68
69
70
# File 'lib/rollerskates/routing/router.rb', line 68

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