UtopiaSourceUtopiaControllerActionsClassMethods

module ClassMethods

Exposed to the controller class.

Definitions

def self.extended(klass)

Initialize class-level state when this module is extended.

Signature

parameter klass Class

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 first Path | String | Symbol | Array

The first path pattern or named suffix.

parameter path Array(String)

Additional path components.

parameter options Hash

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 controller Utopia::Controller::Base

The controller instance.

parameter request Rack::Request

The request.

parameter path Utopia::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