Class: Stannum::Constraints::Delegator

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stannum/constraints/delegator.rb

Overview

A Delegator constraint delegates the constraint methods to a receiver.

Use the Delegator constraint when the behavior of a constraint needs to change based on the context. For example, a contract may use a Delegator constraint to wrap changes made after the contract is first initialized.

Examples:

Using a Delegator constraint

receiver   = Stannum::Constraints::Type.new(String)
constraint = Stannum::Constraints::Delegator.new(receiver)

constraint.matches?('a string') #=> true
constraint.matches?(:a_symbol)  #=> false

constraint.receiver = Stannum::Constraints::Type.new(Symbol)

constraint.matches?('a string') #=> false
constraint.matches?(:a_symbol)  #=> true

Constant Summary

Constants inherited from Base

Base::NEGATED_TYPE, Base::TYPE

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#==, #clone, #does_not_match?, #dup, #errors_for, #match, #matches?, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options

Constructor Details

#initialize(receiver) ⇒ Delegator

Returns a new instance of Delegator.

Parameters:



30
31
32
33
34
# File 'lib/stannum/constraints/delegator.rb', line 30

def initialize(receiver)
  super()

  self.receiver = receiver
end

Instance Attribute Details

#receiverStannum::Constraints::Base

Returns the constraint that methods will be delegated to.

Returns:



51
52
53
# File 'lib/stannum/constraints/delegator.rb', line 51

def receiver
  @receiver
end