| Path: | lib/mues/filters/questionnaire.rb (CVS) |
| Last Update: | Sat Aug 18 22:54:10 -0700 2007 |
This file contains the MUES::Questionnaire class, a derivative of MUES::IOEventFilter. Instances of this class are dynamically-configurable question and answer session objects which are inserted into a MUES::User‘s MUES::IOEventStream to gather useful information about a subject or task. It does this by iterative over one or more steps, prompting the user with a question specified by each one, and gathering and validating input sent in reply.
When an Questionnaire is created, it is given a procedure for gathering the needed information in the form of an Array of one or more "steps", and an optional block to be called when all of the necessary questions are answered.
A "step" can be any object which responds to the element reference (#[]) and the key? methods. Both methods will be called with Symbol arguments like :name and :question, and are expected to behave like a Hash when responding to them. This makes a Hash a particularly good choice for specifying steps. Each step should describe (via responses to said methods) the question to be answered and what constitutes acceptable input for each step. It may also specify other optional keys to further control the way the step is presented or processed. The steps are executed one at a time in the order they are in the steps array at the moment input arrives.
The mandatory keys for a step are:
The optional keys for a step are:
If no validator is specified, any input except empty input is accepted as-is. On empty input, the default will be used if specified (see below), or it will cause the Questionnaire to abort if no default exists.
If a block is given at construction, or set later in the object‘s lifecycle via the finalizer= method, it will be called once all the steps are completed, passing the Questionnaire object as an argument.
require 'mues/filters/questionnaire'
steps = [
{
:name => 'height',
:question => "Height: ",
:validator => /\d+/,
:errorMsg => "Height must be a number.",
},
{
:name => 'color',
:question => "What color [red,green,blue]?",
:validator => %w{red green blue},
}
]
questionnaire = MUES::Questionnaire::new( 'Configure your new Thingie', steps ) {|questionnaire|
thingie = Thingie::new
thingie.height = questionnaire.answers[:height]
thingie.color = questionnaire.answers[:color]
thingie.save
}
$Id: questionnaire.rb 1218 2004-06-14 06:07:54Z deveiant $
Copyright (c) 2000-2003 The FaerieMUD Consortium. All rights reserved.
This module is free software. You may use, modify, and/or redistribute this software under the terms of the Perl Artistic License. (See language.perl.com/misc/Artistic.html)
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.