Class: Utopia::Content::Node

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(controller, uri_path, request_path, file_path) ⇒ Node

Returns a new instance of Node.



34
35
36
37
38
39
40
# File 'lib/utopia/content/node.rb', line 34

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_pathObject (readonly)

Returns the value of attribute file_path.



44
45
46
# File 'lib/utopia/content/node.rb', line 44

def file_path
  @file_path
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



42
43
44
# File 'lib/utopia/content/node.rb', line 42

def request_path
  @request_path
end

#uri_pathObject (readonly)

Returns the value of attribute uri_path.



43
44
45
# File 'lib/utopia/content/node.rb', line 43

def uri_path
  @uri_path
end

Instance Method Details

#call(document, state) ⇒ Object

Invoked when the node is being rendered by Document.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/utopia/content/node.rb', line 117

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


50
51
52
# File 'lib/utopia/content/node.rb', line 50

def link
  return Link.new(:file, uri_path)
end


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/utopia/content/node.rb', line 82

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

#local_path(path = '.', base = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/utopia/content/node.rb', line 58

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



54
55
56
# File 'lib/utopia/content/node.rb', line 54

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.

Returns:

  • (Node)

    The node which will be used to render the tag.



112
113
114
# File 'lib/utopia/content/node.rb', line 112

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

#nameObject



46
47
48
# File 'lib/utopia/content/node.rb', line 46

def name
  @uri_path.basename
end

#parent_pathObject



78
79
80
# File 'lib/utopia/content/node.rb', line 78

def parent_path
  @uri_path.dirname
end

#process!(request, attributes = {}) ⇒ Object



129
130
131
# File 'lib/utopia/content/node.rb', line 129

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


94
95
96
# File 'lib/utopia/content/node.rb', line 94

def related_links
  @controller.links(@uri_path.dirname, :name => @uri_path.basename, :indices => true)
end

#relative_path(path = '.') ⇒ Object



71
72
73
74
75
76
# File 'lib/utopia/content/node.rb', line 71

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


106
107
108
# File 'lib/utopia/content/node.rb', line 106

def sibling_links(**options)
  return @controller.links(siblings_path, **options)
end

#siblings_pathObject



98
99
100
101
102
103
104
# File 'lib/utopia/content/node.rb', line 98

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