Module: Stannum::Entities::Constraints::ClassMethods

Defined in:
lib/stannum/entities/constraints.rb

Overview

Class methods to extend the class when including Constraints.

Instance Method Summary collapse

Instance Method Details

#constraint {|entity| ... } ⇒ Object #constraint(constraint) ⇒ Object #constraint(attr_name) {|value| ... } ⇒ Object #constraint(attr_name, constraint) ⇒ Object

Defines a constraint on the entity or one of its properties.

Overloads:

  • #constraint {|entity| ... } ⇒ Object

    Defines a constraint on the entity.

    A new Stannum::Constraint instance will be generated, passing the block from .constraint to the new constraint. This constraint will be added to the contract.

    Yield Parameters:

  • #constraint(constraint) ⇒ Object

    Defines a constraint on the entity.

    The given constraint is added to the contract. When the contract is evaluated, this constraint will be matched against the entity.

    Parameters:

  • #constraint(attr_name) {|value| ... } ⇒ Object

    Defines a constraint on the given attribute or property.

    A new Stannum::Constraint instance will be generated, passing the block from .constraint to the new constraint. This constraint will be added to the contract.

    Parameters:

    • attr_name (String, Symbol)

      The name of the attribute or property to constrain.

    Yield Parameters:

    • value (Object)

      The value of the attribute or property of the entity at the time the constraint is evaluated.

  • #constraint(attr_name, constraint) ⇒ Object

    Defines a constraint on the given attribute or property.

    The given constraint is added to the contract. When the contract is evaluated, this constraint will be matched against the value of the attribute or property.

    Parameters:

    • attr_name (String, Symbol)

      The name of the attribute or property to constrain.

    • constraint (Stannum::Constraints::Base)

      The constraint to add.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/stannum/entities/constraints.rb', line 79

def constraint(attr_name = nil, constraint = nil, &block)
  attr_name, constraint = resolve_constraint(attr_name, constraint)

  if block_given?
    constraint = Stannum::Constraint.new(&block)
  else
    validate_constraint(constraint)
  end

  contract.add_constraint(constraint, property: attr_name)
end

#contractStannum::Contract

Returns The Contract object for the entity.

Returns:



92
93
94
# File 'lib/stannum/entities/constraints.rb', line 92

def contract
  self::Contract
end