Class: Nitro::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/nitro/part.rb

Overview

A part is a module of reusable functionality encapsulated as a mini site/app. You can require (include) multiple parts in your application. A part is in essence a high level component.

The directory structure of a part mirrors the structure of a typicall web application. By conventions we put the main directories of parts in a root directory called ‘part’.

Let’s demonstrate the above with an example. Two parts are defined here. A user management part (users) and a CMS (content). A typical dir structure goes like this ($ is a directory in the load path, this means you can put parts in multiple places as long as the are in the load path):

$/part # parts will be stored here.

$/part/users.rb # helper file used to ‘require’ the part. $/part/users/public/ $/part/users/controller.rb $/part/users/controller/xml.rb $/part/users/model/user.rb $/part/users/model/acl.rb $/part/users/template/login.xhtml $/part/users/template/form.xinc $/part/users/run.rb # starts an ‘example’ application for this part.

$/part/content.rb $/part/content/controller.rb $/part/content/model.rb …

Given this direcotry structure you can ‘require’ a part like this:

require ‘part/users’ require ‘part/content’

The helper files (for example the file part/users.rb) typically require the part files needed by default.

The ‘example’ application start files (for example part/users/run.rb) are optional. If present, they start a small application that demonstrates the usage of the part. In the example app, the main part controller is mounted at the root (‘/’). Typically, in your own applications, you will mount the controller as needed, (for example: ‘users’ => UsersController, ‘blog’ => ‘ContentController’)

The files that reside in the public directory are typically copied by a code generator to your application public dir.

Part controllers setup the template root stack to lookup templates in their local template dir (for example part/users/template) if a template is not found in the applications normal template root. In essence, by requiring a part a target application, ‘inherits’ its templates. If you want to customize (override) one template, just place a template with the same name in the respective directory in the application template root.

– TODO:

  • add support for autoload

  • add support for install/uninstall

++

Direct Known Subclasses

AdminPart

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePart

Perform part initialization, just before the server is started. Override this in your parts.



76
77
# File 'lib/nitro/part.rb', line 76

def initialize
end

Class Method Details

.require(name) ⇒ Object

Require (include) a part in the current application.



102
103
104
105
# File 'lib/nitro/part.rb', line 102

def require(name)
  Logger.debug "Requiring part '#{name}'." if $DBG
  Kernel.require 'part/' + name + '/run.rb'
end

.setupObject

Call the initialization code of all parts. Typically called just before the server starts.



110
111
112
113
114
# File 'lib/nitro/part.rb', line 110

def setup
  for klass in self.descendents
    p = klass.new
  end
end

Instance Method Details

#finalizeObject

Perform part finalization.



81
82
# File 'lib/nitro/part.rb', line 81

def finalize
end

#installObject

Perform part initialization when the part is installed, ie you add this in your application. Here comes one time initialization code.



88
89
# File 'lib/nitro/part.rb', line 88

def install
end

#uninstallObject Also known as: cleanup

Perform part cleanup when you want to remove the part from your application.



94
95
# File 'lib/nitro/part.rb', line 94

def uninstall
end