class Stannum::Contracts::TupleContract::Builder

Builder class for defining item constraints for a TupleContract.

This class should not be invoked directly. Instead, pass a block to the constructor for TupleContract.

@api private

Public Class Methods

new(contract) click to toggle source

@param contract [Stannum::Contract] The contract to which constraints

are added.
Calls superclass method
# File lib/stannum/contracts/tuple_contract.rb, line 77
def initialize(contract)
  super

  @current_index = -1
end

Public Instance Methods

item(constraint = nil, **options, &block) click to toggle source

Defines an item constraint on the contract.

Each time an item constraint is defined, the constraint is tied to an incrementing index, i.e. the first constraint is matched against the item at index 0, the second at index 1, and so on. This can be overriden by setting the :property option.

@overload item(constraint, **options)

Adds the given constraint to the contract for the next index.

@param constraint [Stannum::Constraint::Base] The constraint to add.
@param options [Hash<Symbol, Object>] Options for the constraint.

@overload item(**options) { |value| }

Creates a new Stannum::Constraint object with the given block, and
adds that constraint to the contract for the next index.

@param options [Hash<Symbol, Object>] Options for the constraint.
@yieldparam value [Object] The value of the property when called.
# File lib/stannum/contracts/tuple_contract.rb, line 102
def item(constraint = nil, **options, &block)
  index = (@current_index += 1)

  self.constraint(
    constraint,
    property:      index,
    property_type: :index,
    **options,
    &block
  )
end