Class: Nitro::Compiler
- Inherits:
-
Object
- Object
- Nitro::Compiler
- Defined in:
- lib/nitro/compiler.rb
Overview
The Compiler transforms published methods (actions) and assorted template files (views) into specialized code that responds to a URI. The generated action methods are injects in the Controller that handles the URI.
Constant Summary collapse
- PROTO_TEMPLATE_ROOT =
"#{Nitro.proto_path}/public"
Instance Attribute Summary collapse
-
#controller ⇒ Object
The controller for this compiler.
-
#shared ⇒ Object
The compiler stages (compilers) can create multiple variables or accumulation bufffers to communicate with each other.
Class Method Summary collapse
-
.precompile(filename) ⇒ Object
Typically used to precompile css templates.
Instance Method Summary collapse
-
#action?(sym) ⇒ Boolean
Helper.
-
#compile(action) ⇒ Object
Compiles an action method in the given (controller) class.
-
#compile_action(action) ⇒ Object
Compiles an action.
-
#compile_template(action, path) ⇒ Object
Compile the template into a render method.
-
#initialize(controller = nil) ⇒ Compiler
constructor
A new instance of Compiler.
-
#template_for_action(action, ext = Glue::Template.extension) ⇒ Object
(also: #template?)
Traverse the template_root stack to find a template for this action.
-
#transform_template(action, template) ⇒ Object
This method transforms the template.
Constructor Details
#initialize(controller = nil) ⇒ Compiler
Returns a new instance of Compiler.
64 65 66 67 |
# File 'lib/nitro/compiler.rb', line 64 def initialize(controller = nil) @controller = controller @shared = {} end |
Instance Attribute Details
#controller ⇒ Object
The controller for this compiler.
27 28 29 |
# File 'lib/nitro/compiler.rb', line 27 def controller @controller end |
#shared ⇒ Object
The compiler stages (compilers) can create multiple variables or accumulation bufffers to communicate with each other. Typically javascript and css acc-buffers are used. This is the shared memory used by the compilers.
34 35 36 |
# File 'lib/nitro/compiler.rb', line 34 def shared @shared end |
Class Method Details
.precompile(filename) ⇒ Object
Typically used to precompile css templates.
356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/nitro/compiler.rb', line 356 def precompile(filename) src = File.join(Glue::Template.root, "#{filename}t") dst = File.join(Server.public_root, filename) if (!File.exist?(dst)) or (File.mtime(src) > File.mtime(dst)) Logger.info "Compiling template '#{src}' to '#{dst}'." template = Glue::FileTemplate.new(src) File.open(dst, 'w') do |f| f << template.process end end end |
Instance Method Details
#action?(sym) ⇒ Boolean
Helper.
98 99 100 |
# File 'lib/nitro/compiler.rb', line 98 def action?(sym) return @controller.action_methods.include?(sym.to_s) end |
#compile(action) ⇒ Object
Compiles an action method in the given (controller) class. A sync is used to make compilation thread safe.
347 348 349 |
# File 'lib/nitro/compiler.rb', line 347 def compile(action) compile_action(action) end |
#compile_action(action) ⇒ Object
Compiles an action. The generated action combines the action supplied by the programmer and an optional template in an optimized method to handle the input URI.
Passes the action name and the parent action name in the
Example
def my_method template_root/my_method.xhtml
are combined in:
def my_method_action
This generated method is called by the dispatcher.
Glue::Template root overloading
Nitro provides a nice method of template_root overloading that allows you to use OOP principles with templates. Lets say you have the following controller inheritance.
SpecificController < BaseController < Nitro::Controller
When the compiler tries to find a template for the SpecificController, it first looks into SC’s template root. If no suitable template is found, it looks into BaseController’s template_root etc. The final template_root is always the Nitro proto dir (the prototype application). This way you can reuse Controllers and templates and only overriding the templates as required by placing a template with the same name in the more specific template root. – TODO: cleanup this method. ++
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/nitro/compiler.rb', line 197 def compile_action(action) action = @action = action.to_s.gsub(/_action$/, '') return false unless action Logger.debug "Compiling action '#@controller##{action}'" if $DBG valid = false #-- # FIXME: parent_action_name does not work as expected, # if you include actions from other controllers!! #++ code = %{ def #{action}_action @parent_action_name = @action_name @action_name = '#{action}' } # Inject the pre advices. code << ::Aspects.gen_advice_code(action, @controller.advices, :pre) # Call the action if @controller.action_methods.include?(action) valid = true # Annotated parameters. if params = @controller.ann(action.to_sym).params and (!params.nil?) params = params.collect { |p| "@#{p} = @context['#{p}']" } code << "#{params.join(';')}" end # Try to resolve action parameters. Returns negative # numbers for arbitrary parameters. #-- # gmosx: This needs a better implementation. Better means # cleaner and more optimized code. #++ param_count = @controller.instance_method(action.to_sym).arity if param_count != 0 code << %{ params = [] unless context.query_string.blank? all_params = context.query_string.split(/[&;]/) is_hash = all_params.first.index('=') all_params.each_with_index do |qs, i| } # Don't pass more parameters than the action's arity. if param_count > 0 code << "break unless i < #{param_count};" end code << %{ break if qs.index('=') and not is_hash params << CGI.unescape(qs.split(/=/).last || "") end end # Concatenate some extracted parameters. # params.concat(context.params.values) # Fill the array with nils for the missing params. (#{param_count} - params.size).times { params << nil } action_return_value = #{action}(*params) } else code << %{ action_return_value = #{action} } end code << %{ unless :stop == action_return_value } end # Try to call the template method if it exists. It is a # nice practice to put output related code in this method # instead of the main action so that this method can be # overloaded separately. # # If no template method exists, try to convert an external # template file into a template method. It is an even # better practice to place the output related code in an # external template file. # Take :view annotation into account. view = @controller.ann(action.to_sym).view # FIXME view = action if view.nil? # Search the [controller] class and it's ancestors for the template template_path = template_for_action(view.to_s) if template_path or @controller.instance_methods.include?("#{action}_template") valid = true code << %{ #{action}_template } end return false unless valid if @controller.action_methods.include?(action) code << %{ if @out.empty? and action_return_value.is_a?(String) print(action_return_value) end end } end if Render.redirect_on_empty code << %{ redirect_referer if @out.empty? } end # Inject the post advices. code << ::Aspects.gen_advice_code(action, @controller.advices, :post) code << %{ @action_name = @parent_action_name end } # First compile the action method. @controller.class_eval(code) unless @controller.respond_to?("#{action}_template") # If there is not method {action}_template in the controller # search for a file template. if template_path compile_template(action, template_path) end end return true end |
#compile_template(action, path) ⇒ Object
Compile the template into a render method. Don’t compile the template if the controller responds_to? #action_template.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/nitro/compiler.rb', line 145 def compile_template(action, path) Logger.debug "Compiling template '#{@controller}: #{path}'" if $DBG template = File.read(path) code = %{ def #{action}_template #{transform_template(action, template)} end } @controller.class_eval(code, path) end |
#template_for_action(action, ext = Glue::Template.extension) ⇒ Object Also known as: template?
Traverse the template_root stack to find a template for this action.
Action names with double underscores (__) are converted to subdirectories. Here are some example mappings:
hello_world -> template_root/hello_world.xhtml this__is__my__hello_world -> template_root/this/is/my/hello_world
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nitro/compiler.rb', line 78 def template_for_action(action, ext = Glue::Template.extension) for template_root in @controller.instance_variable_get(:@template_root) # attempt to find a template of the form # template_root/action.xhtml path = "#{template_root}/#{action.gsub(/__/, '/')}.#{ext}".squeeze('/') return path if File.exist?(path) # attempt to find a template of the form # template_root/action/index.xhtml path = "#{template_root}/#{action.gsub(/__/, '/')}/#{Glue::Template.default}.#{ext}".squeeze('/') return path if File.exist?(path) end return nil end |
#transform_template(action, template) ⇒ Object
This method transforms the template. Typically template processors are added as aspects to this method to allow for customized template transformation prior to compilation.
The default transformation extracts the Ruby code from processing instructions, and uses the StaticInclude, Morphing, Elements and Markup compiler modules.
The Template transformation stage is added by default.
You can override this method or use aspects for really special transformation pipelines. Your imagination is the limit. – TODO: make Template stage pluggable. gmosx: This method is also called from the scaffolding code. ++
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/nitro/compiler.rb', line 122 def transform_template(action, template) # Check for an action specific transformation pipeline. if (transformers = @controller.ann(action.to_sym).transformation_pipeline).nil? # Check for a controller specific transformation pipeline. if (transformers = @controller.ann.self.transformation_pipeline).nil? # Use the default transformation pipeline. transformers = Compiler.transformation_pipeline end end transformers.each do |transformer| template = transformer.transform(template, self) end # Add Template transformation stage by default. template = Template.transform(template) end |