Class: Stannum::Contracts::TupleContract::Builder Private

Inherits:
Stannum::Contract::Builder show all
Defined in:
lib/stannum/contracts/tuple_contract.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Instance Method Summary collapse

Methods inherited from Stannum::Contract::Builder

#property

Constructor Details

#initialize(contract) ⇒ Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Builder.

Parameters:



77
78
79
80
81
# File 'lib/stannum/contracts/tuple_contract.rb', line 77

def initialize(contract)
  super

  @current_index = -1
end

Instance Method Details

#item(constraint, **options) ⇒ Object #item(**options) {|value| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Overloads:

  • #item(constraint, **options) ⇒ Object

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

    Parameters:

    • constraint (Stannum::Constraint::Base)

      The constraint to add.

    • options (Hash<Symbol, Object>)

      Options for the constraint.

  • #item(**options) {|value| ... } ⇒ Object

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

    Parameters:

    • options (Hash<Symbol, Object>)

      Options for the constraint.

    Yield Parameters:

    • value (Object)

      The value of the property when called.



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