module Libis::Tools::ParameterContainer
To use the parameters a class should include the ParameterContainer
module and add parameter statements to the body of the class definition.
Besides enabling the {::Libis::Tools::ParameterContainer::ClassMethods#parameter parameter} class method to define parameters, the module adds the class method {::Libis::Tools::ParameterContainer::ClassMethods#parameter_defs parameter_defs} that will return a Hash
with parameter names as keys and their respective parameter definitions as values.
On each class instance the {::Libis::Tools::ParameterContainer#parameter parameter} method is added and serves as both getter and setter for parameter values. The methods {::Libis::Tools::ParameterContainer#[] []} and {::Libis::Tools::ParameterContainer#[]= []=} serve as aliases for the getter and setter calls.
Additionally two protected methods are available on the instance:
-
{::Libis::Tools::ParameterContainer#parameters parameters}: returns the
Hash
that keeps track of the current parameter values for the instance. -
{::Libis::Tools::ParameterContainer#get_parameter_definition get_parameter_defintion}: retrieves the parameter definition from the instance’s class for the given parameter name.
Any class that derives from a class that included the ParameterContainer
module will automatically inherit all parameter definitions from all of it’s base classes and can override any of these parameter definitions e.g. to change the default values for the parameter.
Constants
- NO_VALUE
Special constant to indicate a parameter has no value set. Nil cannot be used as it is a valid value.
Public Class Methods
@!visibility private
# File lib/libis/tools/parameter.rb, line 318 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
Alias for the {#parameter} getter.
# File lib/libis/tools/parameter.rb, line 349 def [](name) parameter(name) end
Alias for the {#parameter} setter. The only difference is that in case of a frozen parameter, this method silently ignores the exception, but the default value still will not be changed.
# File lib/libis/tools/parameter.rb, line 356 def []=(name, value) parameter name, value rescue ParameterFrozenError # ignored end
Getter/setter for parameter instances With only one argument (the parameter name) it returns the current value for the parameter, but the optional second argument will cause the method to set the parameter value. If the parameter is not available or the given value is not a valid value for the parameter, the method will return the special constant {::Libis::Tools::ParameterContainer::NO_VALUE NO_VALUE
}.
Setting a value on a frozen parameter with the ‘parameter(name,value)’ method throws a {::Libis::Tools::ParameterFrozenError} exception.
# File lib/libis/tools/parameter.rb, line 333 def parameter(name, value = NO_VALUE) param_def = get_parameter_definition(name) return NO_VALUE unless param_def if value.equal? NO_VALUE param_value = parameters[name] param_def.parse(param_value) else return NO_VALUE unless param_def.valid_value?(value) if param_def[:frozen] raise ParameterFrozenError, "Parameter '#{param_def[:name]}' is frozen in '#{self.class.name}'" end parameters[name] = value end end
Protected Instance Methods
# File lib/libis/tools/parameter.rb, line 368 def get_parameter_definition(name) self.class.parameter_defs[name] end
# File lib/libis/tools/parameter.rb, line 364 def parameters @parameter_values ||= Hash.new end