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
A new instance of Node.
- #links(path = '.', **options, &block) ⇒ Object
- #local_path(path = '.', base = nil) ⇒ Object
- #lookup_node(path) ⇒ Object
-
#lookup_tag(tag) ⇒ Node
Lookup the given tag which is being rendered within the given node.
- #name ⇒ Object
- #parent_path ⇒ Object
- #process!(request, attributes = {}) ⇒ Object
- #related_links ⇒ Object
- #relative_path(path = '.') ⇒ Object
- #sibling_links(**options) ⇒ Object
- #siblings_path ⇒ Object
Constructor Details
#initialize(controller, uri_path, request_path, file_path) ⇒ Node
Returns a new instance of Node.
18 19 20 21 22 23 24 |
# File 'lib/utopia/content/node.rb', line 18 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.
28 29 30 |
# File 'lib/utopia/content/node.rb', line 28 def file_path @file_path end |
#request_path ⇒ Object (readonly)
Returns the value of attribute request_path.
26 27 28 |
# File 'lib/utopia/content/node.rb', line 26 def request_path @request_path end |
#uri_path ⇒ Object (readonly)
Returns the value of attribute uri_path.
27 28 29 |
# File 'lib/utopia/content/node.rb', line 27 def uri_path @uri_path end |
Instance Method Details
#call(document, state) ⇒ Object
Invoked when the node is being rendered by Document.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/utopia/content/node.rb', line 97 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
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/utopia/content/node.rb', line 62 def links(path = '.', **, &block) path = uri_path.dirname + Path[path] links = @controller.links(path, **) if block_given? links.each(&block) else links end end |
#local_path(path = '.', base = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/utopia/content/node.rb', line 38 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 |
#lookup_node(path) ⇒ Object
34 35 36 |
# File 'lib/utopia/content/node.rb', line 34 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.
92 93 94 |
# File 'lib/utopia/content/node.rb', line 92 def lookup_tag(tag) return @controller.lookup_tag(tag.name, self) end |
#name ⇒ Object
30 31 32 |
# File 'lib/utopia/content/node.rb', line 30 def name @uri_path.basename end |
#parent_path ⇒ Object
58 59 60 |
# File 'lib/utopia/content/node.rb', line 58 def parent_path @uri_path.dirname end |
#process!(request, attributes = {}) ⇒ Object
109 110 111 |
# File 'lib/utopia/content/node.rb', line 109 def process!(request, attributes = {}) Document.render(self, request, attributes).to_a end |
#related_links ⇒ Object
74 75 76 |
# File 'lib/utopia/content/node.rb', line 74 def @controller.links(@uri_path.dirname, name: @uri_path.basename, indices: true) end |
#relative_path(path = '.') ⇒ Object
51 52 53 54 55 56 |
# File 'lib/utopia/content/node.rb', line 51 def relative_path(path = '.') path = Path[path] base = uri_path.dirname return base + path end |
#sibling_links(**options) ⇒ Object
86 87 88 |
# File 'lib/utopia/content/node.rb', line 86 def sibling_links(**) return @controller.links(siblings_path, **) end |
#siblings_path ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/utopia/content/node.rb', line 78 def siblings_path if @uri_path.basename == INDEX @uri_path.dirname(2) else @uri_path.dirname end end |