class Stannum::Constraints::Delegator

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.

@example 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

Attributes

receiver[R]

@return [Stannum::Constraints::Base] the constraint that methods will be

delegated to.

Public Class Methods

new(receiver) click to toggle source

@param receiver [Stannum::Constraints::Base] The constraint that methods

will be delegated to.
Calls superclass method Stannum::Constraints::Base::new
# File lib/stannum/constraints/delegator.rb, line 30
def initialize(receiver)
  super()

  self.receiver = receiver
end

Public Instance Methods

receiver=(value) click to toggle source

@param value [Stannum::Constraints::Base] The constraint that methods

will be delegated to.
# File lib/stannum/constraints/delegator.rb, line 55
def receiver=(value)
  validate_receiver(value)

  @receiver = value
end

Private Instance Methods

validate_receiver(receiver) click to toggle source
# File lib/stannum/constraints/delegator.rb, line 63
def validate_receiver(receiver)
  return if receiver.is_a?(Stannum::Constraints::Base)

  raise ArgumentError,
    'receiver must be a Stannum::Constraints::Base',
    caller(1..-1)
end