Class: Utopia::Content::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/node.rb

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.



30
31
32
33
34
35
36
# File 'lib/utopia/content/node.rb', line 30

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.



40
41
42
# File 'lib/utopia/content/node.rb', line 40

def file_path
  @file_path
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



38
39
40
# File 'lib/utopia/content/node.rb', line 38

def request_path
  @request_path
end

#uri_pathObject (readonly)

Returns the value of attribute uri_path.



39
40
41
# File 'lib/utopia/content/node.rb', line 39

def uri_path
  @uri_path
end

Instance Method Details

#call(transaction, state) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/utopia/content/node.rb', line 109

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


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

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


78
79
80
81
82
83
84
85
86
87
# File 'lib/utopia/content/node.rb', line 78

def links(path = '.', **options, &block)
	path = uri_path.dirname + Path[path]
	links = Links.index(@controller.root, path, options)
	
	if block_given?
		links.each(&block)
	else
		links
	end
end

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



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/utopia/content/node.rb', line 50

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(tag) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/utopia/content/node.rb', line 63

def lookup(tag)
	from_path = parent_path
	
	# If the current node is called 'foo', we can't lookup 'foo' in the current directory or we will have infinite recursion.
	if tag.name == @uri_path.basename
		from_path = from_path.dirname
	end
	
	return @controller.lookup_tag(tag.name, from_path)
end

#lookup_node(path) ⇒ Object



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

def lookup_node(path)
	@controller.lookup_node(path)
end

#parent_pathObject



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

def parent_path
	uri_path.dirname
end

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



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

def process!(request, attributes = {})
	transaction = Transaction.new(request)
	
	output = transaction.render_node(self, attributes)
	
	return [200, transaction.headers, [output]]
end


89
90
91
92
93
# File 'lib/utopia/content/node.rb', line 89

def related_links
	name = @uri_path.last.split('.', 2).first
	
	return Links.index(@controller.root, uri_path.dirname, :name => name, :indices => true)
end


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

def sibling_links(**options)
	return Links.index(@controller.root, siblings_path, options)
end

#siblings_pathObject



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

def siblings_path
	name = @uri_path.last.split('.', 2).first
	
	if name == INDEX
		@uri_path.dirname(2)
	else
		@uri_path.dirname
	end
end