Class: Handshake::Clause

Inherits:
Object
  • Object
show all
Defined in:
lib/handshake/clause_methods.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.



11
12
13
# File 'lib/handshake/clause_methods.rb', line 11

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

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/handshake/clause_methods.rb', line 22

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.



16
17
18
# File 'lib/handshake/clause_methods.rb', line 16

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

#inspectObject

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



21
# File 'lib/handshake/clause_methods.rb', line 21

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