Class: Handshake::Clause

Inherits:
Object
  • Object
show all
Defined in:
lib/handshake/handshake.rb

Overview

Transforms the given block into a contract clause. Clause fails if the given block returns false or nil, passes otherwise. See Handshake::ClauseMethods for more examples of its use. This object may be instantiated directly but calling Handshake::ClauseMethods#clause is generally preferable.

Instance Method Summary collapse

Constructor Details

#initialize(mesg = nil, &block) ⇒ Clause

Defines a new Clause object with a block and a message. The block should return a boolean value. The message is optional but strongly recommended for human-readable contract violation errors.



563
564
565
# File 'lib/handshake/handshake.rb', line 563

def initialize(mesg=nil, &block) # :yields: argument
  @mesg, @block = mesg, block
end

Instance Method Details

#==(other) ⇒ Object



574
575
576
# File 'lib/handshake/handshake.rb', line 574

def ==(other)
  other.class == self.class && other.mesg == @mesg && other.block == @block
end

#===(o) ⇒ Object

Returns true if the block passed to the constructor returns true when called with the given argument.



568
569
570
# File 'lib/handshake/handshake.rb', line 568

def ===(o)
  @block.call(o)
end

#inspectObject

Returns the message defined for this Clause, or “undocumented clause” if none is defined.



573
# File 'lib/handshake/handshake.rb', line 573

def inspect; @mesg || "undocumented clause"; end