Last active
December 30, 2015 07:49
-
-
Save Wardrop/7798612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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