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:

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

included(base) click to toggle source

@!visibility private

# File lib/libis/tools/parameter.rb, line 315
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

[](name) click to toggle source

Alias for the {#parameter} getter.

# File lib/libis/tools/parameter.rb, line 346
def [](name)
  parameter(name)
end
[]=(name, value) click to toggle source

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 353
def []=(name, value)
  parameter name, value
rescue ParameterFrozenError
  # ignored
end
parameter(name, value = NO_VALUE) click to toggle source

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 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

Protected Instance Methods

get_parameter_definition(name) click to toggle source
# File lib/libis/tools/parameter.rb, line 365
def get_parameter_definition(name)
  self.class.parameter_defs[name]
end
parameters() click to toggle source
# File lib/libis/tools/parameter.rb, line 361
def parameters
  @parameter_values ||= Hash.new
end