Class: Utopia::Content::Node
- Inherits:
-
Object
- Object
- Utopia::Content::Node
- Defined in:
- lib/utopia/content/node.rb
Overview
Represents an immutable node within the content hierarchy.
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#request_path ⇒ Object
readonly
Returns the value of attribute request_path.
-
#uri_path ⇒ Object
readonly
Returns the value of attribute uri_path.
Instance Method Summary collapse
-
#call(document, state) ⇒ Object
Invoked when the node is being rendered by Document.
-
#initialize(controller, uri_path, request_path, file_path) ⇒ Node
constructor
Initialize a node within the filesystem-backed content hierarchy.
-
#links(path = ".", **options, &block) ⇒ Object
Enumerate or return links relative to this node.
-
#local_path(path = ".", base = nil) ⇒ Object
Resolve a content path beneath the controller root.
-
#lookup_node(path) ⇒ Object
Resolve another node relative to this node's parent.
-
#lookup_tag(tag) ⇒ Node
Lookup the given tag which is being rendered within the given node.
-
#name ⇒ Object
Return the node's URI basename.
-
#parent_path ⇒ Object
Return this node's containing URI path.
-
#process!(request, attributes = {}, localization: request&.localization) ⇒ Object
Process the request and return the resulting response.
-
#related_links ⇒ Object
Return localized and indexed variants related to this node.
-
#relative_path(path = ".") ⇒ Object
Resolve a path relative to this node's containing URI path.
-
#sibling_links(**options) ⇒ Object
Return links that are siblings of this node.
-
#siblings_path ⇒ Object
Return the directory whose links are siblings of this node.
Constructor Details
#initialize(controller, uri_path, request_path, file_path) ⇒ Node
Initialize a node within the filesystem-backed content hierarchy.
23 24 25 26 27 28 29 |
# File 'lib/utopia/content/node.rb', line 23 def initialize(controller, uri_path, request_path, file_path) @controller = controller @uri_path = uri_path @request_path = request_path @file_path = file_path end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
33 34 35 |
# File 'lib/utopia/content/node.rb', line 33 def file_path @file_path end |
#request_path ⇒ Object (readonly)
Returns the value of attribute request_path.
31 32 33 |
# File 'lib/utopia/content/node.rb', line 31 def request_path @request_path end |
#uri_path ⇒ Object (readonly)
Returns the value of attribute uri_path.
32 33 34 |
# File 'lib/utopia/content/node.rb', line 32 def uri_path @uri_path end |
Instance Method Details
#call(document, state) ⇒ Object
Invoked when the node is being rendered by Document.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/utopia/content/node.rb', line 124 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 |
#links(path = ".", **options, &block) ⇒ Object
Enumerate or return links relative to this node.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/utopia/content/node.rb', line 82 def links(path = ".", **, &block) path = uri_path.dirname + Path[path] links = @controller.links.index(path, **) if block_given? links.each(&block) else links end end |
#local_path(path = ".", base = nil) ⇒ Object
Resolve a content path beneath the controller root.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/utopia/content/node.rb', line 51 def local_path(path = ".", base = nil) path = Path[path] if path.relative? base ||= uri_path.dirname path = base + path end return Pathname.new(path.to_url_path.local_path(@controller.root)) end |
#lookup_node(path) ⇒ Object
Resolve another node relative to this node's parent.
44 45 46 |
# File 'lib/utopia/content/node.rb', line 44 def lookup_node(path) @controller.lookup_node(parent_path + Path[path]) end |
#lookup_tag(tag) ⇒ Node
Lookup the given tag which is being rendered within the given node. Invoked by Document.
119 120 121 |
# File 'lib/utopia/content/node.rb', line 119 def lookup_tag(tag) return @controller.lookup_tag(tag.name, self) end |
#name ⇒ Object
Return the node's URI basename.
37 38 39 |
# File 'lib/utopia/content/node.rb', line 37 def name @uri_path.basename end |
#parent_path ⇒ Object
Return this node's containing URI path.
74 75 76 |
# File 'lib/utopia/content/node.rb', line 74 def parent_path @uri_path.dirname end |
#process!(request, attributes = {}, localization: request&.localization) ⇒ Object
Process the request and return the resulting response.
141 142 143 |
# File 'lib/utopia/content/node.rb', line 141 def process!(request, attributes = {}, localization: request&.localization) Document.render(self, request, attributes, localization: localization).to_response end |
#related_links ⇒ Object
Return localized and indexed variants related to this node.
96 97 98 |
# File 'lib/utopia/content/node.rb', line 96 def @controller.links.index(@uri_path.dirname, name: @uri_path.basename, indices: true) end |
#relative_path(path = ".") ⇒ Object
Resolve a path relative to this node's containing URI path.
65 66 67 68 69 70 |
# File 'lib/utopia/content/node.rb', line 65 def relative_path(path = ".") path = Path[path] base = uri_path.dirname return base + path end |
#sibling_links(**options) ⇒ Object
Return links that are siblings of this node.
113 114 115 |
# File 'lib/utopia/content/node.rb', line 113 def sibling_links(**) return @controller.links.index(siblings_path, **) end |
#siblings_path ⇒ Object
Return the directory whose links are siblings of this node.
102 103 104 105 106 107 108 |
# File 'lib/utopia/content/node.rb', line 102 def siblings_path if @uri_path.basename == INDEX @uri_path.dirname(2) else @uri_path.dirname end end |