Module: N::Rendering

Defined in:
lib/nitro/render.rb

Overview

Rendering utility methods

Constant Summary collapse

@@shader =
RubyShader.new

Class Method Summary collapse

Class Method Details

.compile_action(klass, action, base) ⇒ Object

Compile a controller action.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/nitro/render.rb', line 86

def self.compile_action(klass, action, base)
	dummy, api, action = action.to_s.split('__')

	# This is not a controller action.

	return false unless action

	Logger.debug "Compiling action '#{base}/#{action}'" if $DBG

	valid = false
	
	code = %{
		def __#{api}__#{action}
	}
	
	# call 'before' filter chain.
	
	if klass.respond_to?(:before_filters)
		code << %{
			#{klass.gen_filters_call_code(klass.before_filters)}
		}
	end

	# call the action
	
	if klass.instance_methods.include?(action)
		valid = true
		code << %{
			#{action}();
		}
	end

	# call the programmatically generated template if exists.

	if klass.instance_methods.include?("#{action}__#{api}")
		valid = true
		code << %{
			return unless #{action}__#{api}();
		}
	end
	
	# call the template if exists.
	
	if template = template_for_action(base, action)
		valid = true
		code << %{
			return unless __#{api}__#{action}__template();
		}
	end

	# raise "Invalid action '#{action}' for '#{klass}'!" unless valid
	return false unless valid

	# call 'after' filter chain.

	if klass.respond_to?(:after_filters)
		code << %{
			#{klass.gen_filters_call_code(klass.after_filters)}
		}
	end
	
	code << %{
			redirect_referer if @out.empty?
		end
	}
	
	if template
		code << %{
			def __#{api}__#{action}__template
				#{transform_template(template, Rendering.shader)}
			end
		}
	end
	
	klass.class_eval(code)

	return true
end

.template_for_action(base, action, ext = :xhtml) ⇒ Object

Given the action try find the matching template. Can search for xhtml or xml templates. Returns nil if no template file is found.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nitro/render.rb', line 50

def self.template_for_action(base, action, ext = :xhtml)
	
	# attempt to find a template of the form
	# base/action.xhtml

	path = "#{base}/#{action}.#{ext}".squeeze('/')

	unless File.exist?(path)

		# attempt to find a template of the form
		# base/action/index.xhtml

		path = "#{base}/#{action}/#{Rendering.default_template}.#{ext}".squeeze('/')
		
		unless File.exist?(path)
			# No template found!
			path = nil
		end
	end
	
	return path
end

.transform_template(path, shader) ⇒ Object

Transform a template to ruby rendering code.



75
76
77
78
79
80
81
82
# File 'lib/nitro/render.rb', line 75

def self.transform_template(path, shader)
	Logger.debug "Transforming '#{path}'" if $DBG

	text = File.read(path)
	hash, text = shader.process(path, text)
	
	return text
end