Class ConfigToolkit::ConstrainedArray

  1. lib/configtoolkit/types.rb
Parent: Object
BaseConfig KeyValueConfig ConfigToolkitConfig Reader YAMLReader KeyValueReader OverrideReader RubyReader HashReader Writer PrettyPrintWriter HashWriter KeyValueWriter YAMLWriter RuntimeError Error ConstrainedArray HashArrayVisitor Boolean ConfigToolkit dot/f_9.png

ConstrainedArray classes model Arrays with specified sizes and with all elements being instances of specified classes (basically, they model the arrays generally offerred by statically typed languages). A ConstrainedArray has:

  • A minimum number of elements (could be zero)
  • A maximum number of elements (could be infinity)
  • An element class (could be Object, which allows any class)

A future enhancement might be to allow users to specify a different class for each element. Note that the ConstrainedArray also allows elements to be instances of a child class of its element class, not just instances of its element class. Thus, a ConstrainedArray containing class Object essentially would have no class constraints.

ConstrainedArray actually is a class generator, similar to Struct. Its new method does not return a ConstrainedArray instance but instead returns a new class with the specified constraints that descends from ConstrainedArray. The class returned by new is meant to be used in the BaseConfig methods to add new parameters (BaseConfig.add_required_param and BaseConfig.add_optional_param). The value of one of these parameters actually will be a native Ruby Array, but one that is guaranteed to satisify the constraints contained in the ConstrainedArray class.

Methods

public class

  1. new

Attributes

element_class [R] The class of all of the elements.
max_num_elements [R] The maximum number of elements, or nil if there is no maximum.
min_num_elements [R] The minimum number of elements, or nil if there is no minimum.

Public class methods

new (element_class, min_num_elements = nil, max_num_elements = nil)

Description:

This method does not return a ConstrainedArray instance. Instead, it returns a new ConstrainedArray child class that represents an Array with the constraints specified in the method arguments. This class can be passed as a parameter class into the BaseConfig methods to add parameters (BaseConfig.add_required_param and BaseConfig.add_optional_param).

Parameters:

element_class
This constrains all elements of the generated ConstrainedArray class to be of this class or of one of its child classes. If this argument is Object, then this constraint effectively disappears.
min_num_elements
This constrains the generated ConstrainedArray class to have at least min_num_elements elements; if this argument is nil or zero, the constraint effectively disappears.
max_num_elements
This constrains the generated ConstrainedArray class to have at most max_num_elements elements; if this argument is nil, the constraint effectively disappears.

Returns:

A ConstrainedArray child class with the specified constraints.

[show source]
# File lib/configtoolkit/types.rb, line 103
  def self.new(element_class, min_num_elements = nil, max_num_elements = nil)
    return Class.new(ConstrainedArray) do
      @element_class = element_class
      @min_num_elements = min_num_elements
      @max_num_elements = max_num_elements
    end
  end