class Rollerskates::Routing::Router

Public Instance Methods

actions(options = {}) click to toggle source
# 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_actions() click to toggle source
# 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) click to toggle source
# File lib/rollerskates/routing/router.rb, line 4
def draw(&block)
  instance_eval(&block)
end
endpoints() click to toggle source
# File lib/rollerskates/routing/router.rb, line 72
def endpoints
  @endpoints ||= Hash.new { |hash, key| hash[key] = [] }
end
method_path_to(route, controller_name) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rollerskates/routing/router.rb, line 68
def root(location)
  get "/", to: location
end

Private Instance Methods

controller_and_action_for(path_to) click to toggle source
# File lib/rollerskates/routing/router.rb, line 88
def controller_and_action_for(path_to)
  controller_path, action = path_to.split('#')
  controller = "#{controller_path.camelize}Controller"
  [controller, action.to_sym]
end
pattern_for(path) click to toggle source
# File lib/rollerskates/routing/router.rb, line 78
def pattern_for(path)
  placeholders = []
  pattern = path.to_s.gsub!(/(:\w+)/) do |match|
    placeholders << match[1..-1].freeze
    "(?<#{placeholders.last}>[^/?#]+)"
  end
  pattern = pattern ? pattern : path
  [/^#{pattern}$/, placeholders]
end