Module: Libis::Tools::ParameterContainer
- Defined in:
- lib/libis/tools/parameter.rb
Overview
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 parameter class method to define parameters, the module adds the class method parameter_defs that will return a Hash with parameter names as keys and their respective parameter definitions as values.
On each class instance the parameter method is added and serves as both getter and setter for parameter values. The methods [] and []= serve as aliases for the getter and setter calls.
Additionally two protected methods are available on the instance:
-
parameters: returns the Hash that keeps track of the current parameter values for the instance.
-
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.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- NO_VALUE =
Special constant to indicate a parameter has no value set. Nil cannot be used as it is a valid value.
'##NAV##'
Instance Method Summary collapse
-
#[](name) ⇒ Object
Alias for the #parameter getter.
-
#[]=(name, value) ⇒ Object
Alias for the #parameter setter.
-
#parameter(name, value = NO_VALUE) ⇒ Object
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.
Instance Method Details
#[](name) ⇒ Object
Alias for the #parameter getter.
349 350 351 |
# File 'lib/libis/tools/parameter.rb', line 349 def [](name) parameter(name) end |
#[]=(name, value) ⇒ Object
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.
356 357 358 359 360 |
# File 'lib/libis/tools/parameter.rb', line 356 def []=(name, value) parameter name, value rescue ParameterFrozenError # ignored end |
#parameter(name, value = NO_VALUE) ⇒ Object
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 NO_VALUE.
Setting a value on a frozen parameter with the ‘parameter(name,value)’ method throws a Libis::Tools::ParameterFrozenError exception.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# 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 |