Class: Stannum::Contracts::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/stannum/contracts/builder.rb

Overview

Abstract base class for contract builder classes.

A contract builder provides a Domain-Specific Language for defining constraints on a contract. They are typically used during initialization if a block is passed to Contract#new.

Direct Known Subclasses

Stannum::Constraints::Base::Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract) ⇒ Builder

Returns a new instance of Builder.

Parameters:



14
15
16
# File 'lib/stannum/contracts/builder.rb', line 14

def initialize(contract)
  @contract = contract
end

Instance Attribute Details

#contractStannum::Contract (readonly)

Returns The contract to which constraints are added.

Returns:



19
20
21
# File 'lib/stannum/contracts/builder.rb', line 19

def contract
  @contract
end

Instance Method Details

#concat(other) ⇒ self

Concatenate the constraints from the given other contract.

Parameters:

Returns:

  • (self)

    the contract builder.

See Also:

  • Stannum::Contracts::Base#concat.


28
29
30
# File 'lib/stannum/contracts/builder.rb', line 28

def concat(other)
  contract.concat(other)
end

#constraint(constraint, **options) ⇒ Object #constraint(**options, &block) ⇒ Object

Adds a constraint to the contract.

Overloads:

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

    Adds the given constraint to the contract.

    Parameters:

    • constraint (Stannum::Constraints::Base)

      The constraint to add to the contract.

    • options (Hash<Symbol, Object>)

      Options for the constraint.

  • #constraint(**options, &block) ⇒ Object

    Creates an instance of Stannum::Constraint using the given block and adds it to the contract.

    Parameters:

    • options (Hash<Symbol, Object>)

      Options for the constraint.

    Options Hash (**options):

    • negated_type (String)

      The error type generated for a matching object.

    • type (String)

      The error type generated for a non-matching object.



50
51
52
53
54
55
56
# File 'lib/stannum/contracts/builder.rb', line 50

def constraint(constraint = nil, **options, &block)
  constraint = resolve_constraint(constraint, **options, &block)

  contract.add_constraint(constraint, **options)

  self
end