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
controllerUtopia::Controller::Base The controller instance.
-
parameter
uri_pathUtopia::Path | String The uri path.
-
parameter
request_pathUtopia::Path | String The request path.
-
parameter
file_pathString 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
pathUtopia::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
pathUtopia::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
pathUtopia::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 links(path = ".", **options, &block)
Enumerate or return links relative to this node.
Signature
-
parameter
pathUtopia::Path | String The path.
-
yields
{|link| ...} Each matching link when a block is given.
-
returns
Array(Link) The matching links.
Implementation
def links(path = ".", **options, &block)
path = uri_path.dirname + Path[path]
links = @controller.links(path, **options)
if block_given?
links.each(&block)
else
links
end
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 sibling_links(**options)
Return links that are siblings of this node.
Signature
-
parameter
optionsHash The options.
-
returns
Array(Link) The sibling links.
Implementation
def sibling_links(**options)
return @controller.links(siblings_path, **options)
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
requestRack::Request The request.
-
parameter
attributesHash The attributes.
-
returns
Array The response.
Implementation
def process!(request, attributes = {})
Document.render(self, request, attributes).to_a
end