Class: ConfigToolkit::ConstrainedArray

Inherits:
Object
  • Object
show all
Defined in:
lib/configtoolkit/types.rb

Overview

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.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.element_classObject (readonly)

The class of all of the elements.



61
62
63
# File 'lib/configtoolkit/types.rb', line 61

def element_class
  @element_class
end

.max_num_elementsObject (readonly)

The maximum number of elements, or nil if there is no maximum.



73
74
75
# File 'lib/configtoolkit/types.rb', line 73

def max_num_elements
  @max_num_elements
end

.min_num_elementsObject (readonly)

The minimum number of elements, or nil if there is no minimum.



67
68
69
# File 'lib/configtoolkit/types.rb', line 67

def min_num_elements
  @min_num_elements
end

Class Method Details

.new(element_class, min_num_elements = nil, max_num_elements = nil) ⇒ Object

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.



103
104
105
106
107
108
109
# 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