UtopiaSourceUtopiaContentNode

class Node

Represents an immutable node within the content hierarchy.

Nested

Definitions

def initialize(controller, uri_path, request_path, file_path)

Initialize a node within the filesystem-backed content hierarchy.

Signature

parameter controller Utopia::Controller::Base

The controller instance.

parameter uri_path Utopia::Path | String

The uri path.

parameter request_path Utopia::Path | String

The request path.

parameter file_path String

The filesystem path.

Implementation

def initialize(controller, uri_path, request_path, file_path)
	@controller = controller
	
	@uri_path = uri_path
	@request_path = request_path
	@file_path = file_path
end

def name

Return the node's URI basename.

Signature

returns String

The node name.

Implementation

def name
	@uri_path.basename
end

def lookup_node(path)

Resolve another node relative to this node's parent.

Signature

parameter path Utopia::Path | String

The path.

returns Node | Nil

The resolved node.

Implementation

def lookup_node(path)
	@controller.lookup_node(parent_path + Path[path])
end

def local_path(path = ".", base = nil)

Resolve a content path beneath the controller root.

Signature

parameter path Utopia::Path | String

The path.

returns Pathname

The local filesystem path.

Implementation

def local_path(path = ".", base = nil)
	path = Path[path]
	
	root = Pathname.new(@controller.root)
	
	if path.absolute?
		return root.join(*path.components)
	else
		base ||= uri_path.dirname
		return root.join(*(base + path).components)
	end
end

def relative_path(path = ".")

Resolve a path relative to this node's containing URI path.

Signature

parameter path Utopia::Path | String

The path.

returns Path

The resolved content path.

Implementation

def relative_path(path = ".")
	path = Path[path]
	base = uri_path.dirname
	
	return base + path
end

def parent_path

Return this node's containing URI path.

Signature

returns Path

The parent URI path.

Implementation

def parent_path
	@uri_path.dirname
end

def siblings_path

Return the directory whose links are siblings of this node.

Signature

returns Path

The sibling directory path.

Implementation

def siblings_path
	if @uri_path.basename == INDEX
		@uri_path.dirname(2)
	else
		@uri_path.dirname
	end
end

def lookup_tag(tag)

Lookup the given tag which is being rendered within the given node. Invoked by class Utopia::Content::Document.

Implementation

def lookup_tag(tag)
	return @controller.lookup_tag(tag.name, self)
end

def call(document, state)

Invoked when the node is being rendered by class Utopia::Content::Document.

Implementation

def call(document, state)
	# Load the template:
	template = @controller.fetch_template(@file_path)
	
	# Evaluate the template/code:
	context = Context.new(document, state)
	markup = template.to_buffer(context)
	
	# Render the resulting markup into the document:
	document.parse_markup(markup)
end

def process!(request, attributes = {})

Process the request and return the resulting response.

Signature

parameter request Rack::Request

The request.

parameter attributes Hash

The attributes.

returns Array

The response.

Implementation

def process!(request, attributes = {})
	Document.render(self, request, attributes).to_a
end