Module | ParameterValidator |
In: |
lib/parmvalid.rb
|
ParameterValidator is a mixin module providing parameter validation for functions and methods that employ Ruby‘s "keyword arguments" implemention.
To use it in a class:
Example:
require 'parmvalid' class Demo include ParameterValidator def initialize( args ) validate_and_instantiate_parameters( args, { :name=>REQUIRED, :date=>Time.now, :location=nil, :memo=>REQUIRED } ) p @name p @date p @location p @memo end end d = Demo.new( :name=>"Jamis", :memo=>"Some memo" )
Author: Jamis Buck (jgb3@email.byu.edu)
REQUIRED | = | Object.new | A unique constant, used as the default value for a parameter to indicate that it is required. |
For each key in the given hash, create an identically-named instance variable and set its value to the value of the given key.
For each key in the given hash, assign its value to an identically-named setter in the current class.
Validate and instantiate the given parameters by calling both validate_parameters and instantiate_parameters. Return the hash of all parameters.
Validate and set the given parameters by calling both validate_parameters and set_parameters. Return the hash of all parameters.
Validates a hash of actual parameters (as created by Ruby‘s pseudo-"keyword arguments" feature) against a list of valid parameters.
Parameters:
Returns: a hash of all parameters, together with either the actual value that was passed in, or (in the case of optional parameters that were not passed in) the default value.