Class SmerfFile
In: app/models/smerf_file.rb
Parent: SmerfItem

This class contains functions to work with the smerf form definition file. It will read the file validating the format of the file, if any errors are found a RuntimeError exception will be raised.

Once the file has been validated all objects created during the process will be serialized to the smerf_forms DB table. Subsequent calls to the form will simplay unserialize the objects from the DB rather then reporcessing the definition file.

If changes are made to the definition file the system will again call functions within this class to rebuild the form objects. Refer to the rebuild_cache method within the smerfform.rb file to see how this all works.

When setting up a new form the first thing we do is define some settings for the form as a whole. Currently the following items can be defined for the form:

name:Name of the form (mandatory)
welcome:Message displayed at the start of the form (optional)
thank_you:Message displayed at the end of the form (optional)
group_sort_order_field:Nominates which group field to use when sorting groups for display (mandatory)
groups:Defines the question groups within the form (mandatory)

Here is the definition for the test form included with the plugin:

     name: Test SMERF Form
     welcome: |
       Thank you for taking part in our Test survey we appreciate your

       <b>PRIVACY STATEMENT</b><br>
       We will keep all the information you provide private and not share
       it with anyone else....<br>

     thank_you: |
       <b>Thank you for your input.</b><br><br>

       Should you wish to discuss this survey please contact<br>
       Joe Bloggs<br>
       Tel. 12 345 678<br>
       e-mail <A HREF=\"\">Joe's email</A><br><br>

       February 2007
     group_sort_order_field: code



clear   modified?   new   validate  


code  [RW] 
group_objects  [RW] 
group_sort_order_field  [RW] 
name  [RW] 
smerf_file_name  [RW] 
smerf_record  [RW] 
thank_you  [RW] 
welcome  [RW] 

Public Class methods

This method checks if the form definition file has been modified by comparing the timestamp of the form definition file against the timestamp stored in the smerf_forms DB table, if they are not the same the form is rebuilt.

When a new SmerfFile object is created this method gets executed. It performs a number of function including:

  • make sure the form definition file actually exists
  • opens and reads all the data from the file
  • makes sure the file contains a form definition
  • initialises variables

Refer to the rebuild_cache method within the smerfform.rb file.

Public Instance methods

This method clears all class variables, as these are class vars they are created at startup and then exists for the duration of the app. We want to clear all values in these vars when we process a new form definition file.

This method performs the bulk of the processing. Once the file is opened and the data has been read into memory this method processes the data. It processes all the groups, questions, answers and subquestions of the form creating the appropriate objects as required.

As it processes the data it also performs validation on the data to make sure all required fields have been specified in the definition file. If any errors are found a RuntimeError exception will be raised.

Refer to the rebuild_cache method within the smerfform.rb file.

The following object will be created during this process: