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

Instance Method Details

#[](name) ⇒ Object

Alias for the #parameter getter.



346
347
348
# File 'lib/libis/tools/parameter.rb', line 346

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.



353
354
355
356
357
# File 'lib/libis/tools/parameter.rb', line 353

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.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/libis/tools/parameter.rb', line 330

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