module ClassMethods
Exposed to the controller class.
Definitions
def self.extended(klass)
Initialize class-level state when this module is extended.
Signature
-
parameter
klassClass The class to configure.
-
returns
Class The configured controller class.
Implementation
def self.extended(klass)
klass.instance_eval do
@actions = nil
@otherwise = nil
end
end
def actions
Return the root of the action lookup tree.
Signature
-
returns
Action The root action.
Implementation
def actions
@actions ||= Action.new
end
def on(first, *path, **options, &block)
Define an action for the given request path.
Signature
-
parameter
firstPath | String | Symbol | Array The first path pattern or named suffix.
-
parameter
pathArray(String) Additional path components.
-
parameter
optionsHash Metadata associated with the action.
-
returns
Action The defined action.
Implementation
def on(first, *path, **options, &block)
if first.is_a? Symbol
first = ["**", first.to_s]
end
actions.define(Path.split(first) + path, **options, &block)
end
def otherwise(&block)
Define the fallback action.
Signature
-
returns
Proc The fallback action body.
Implementation
def otherwise(&block)
@otherwise = block
end
def dispatch(controller, request, path)
Dispatch the request to the first matching action.
Signature
-
parameter
controllerUtopia::Controller::Base The controller instance.
-
parameter
requestRack::Request The request.
-
parameter
pathUtopia::Path | String The path.
-
returns
Object | Nil The result of the final matching action or fallback action.
Implementation
def dispatch(controller, request, path)
if @actions
matched = @actions.apply(path.components) do |action|
controller.instance_exec(request, path, &action.callback)
end
end
if @otherwise and !matched
controller.instance_exec(request, path, &@otherwise)
end
end