Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'scorched'
class Base < Scorched::Controller
class << self
def inherited(klass)
klass.route('/*?') do
action = request.breadcrumb[-2].mapping[:conditions][:action]
if action && respond_to?(action)
send(action)
else
response.status = 500
"Misconfigured route. Endpoint action does not exist."
end
end
end
end
def captures
request.breadcrumb[-2].captures
end
end
class Admin < Base
end
class Customers < Base
def view_all
"Viewing all records"
end
def view
"Viewing single record #{captures[:id]}"
end
end
class Root < Scorched::Controller
conditions[:action] = proc { true }
class << self
def endpoint(pattern, method, controller, action)
pattern << '$' if String === pattern
map pattern: pattern, conditions: {method: method, action: action}, target: controller
end
end
endpoint '/admin/customers', 'GET', Customers, :view_all
endpoint '/admin/customers/:id', 'GET', Customers, :view
end
run Root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment