Class: DuckPond::Contract

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

Defined Under Namespace

Classes: ContractInfringementError

Class Method Summary collapse

Class Method Details

.clausesObject

clauses

Gets this contract’s list of clauses or returns an empty array if uninitialized.



20
21
22
# File 'lib/duckpond/contract.rb', line 20

def clauses
  @clauses ||= []
end

.each_clauseObject

each_clause

Iterator for clauses



29
30
31
32
33
# File 'lib/duckpond/contract.rb', line 29

def each_clause
  0.upto(clauses.size-1) do |i|
    yield @clauses[i]
  end
end

.fulfills!(obj) ⇒ Object

fulfills!

Raises a ContractInfringementError unless this contract fulfills the passed in object, otherwise returns true.



87
88
89
90
# File 'lib/duckpond/contract.rb', line 87

def fulfills!(obj)
  raise ContractInfringementError unless fulfills?(obj)
  true
end

.fulfills?(obj) ⇒ Boolean

fulfills?

Returns true if this contract fulfills the passed-in object, otherwise false.

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/duckpond/contract.rb', line 75

def fulfills?(obj)
  inspection = DuckPond::Inspection.new(obj)
  return false unless inspection.fulfilled_by? self
  true
end

.has_clause(clause_klass, opts = {}, &block) ⇒ Object

has_clause

add a clause to the contract’s list of clauses



40
41
42
# File 'lib/duckpond/contract.rb', line 40

def has_clause(clause_klass, opts = {}, &block)
  clauses << clause_klass.new(opts, block)
end

.has_method(method_name, opts = {}) ⇒ Object

has_method

Adds a method clause to the contract



50
51
52
53
54
55
56
# File 'lib/duckpond/contract.rb', line 50

def has_method(method_name, opts = {})
  if block_given?
    has_clause(MethodClause, opts.merge(:method_name => method_name), &Proc.new)
  else
    has_clause(MethodClause, opts.merge(:method_name => method_name))
  end
end

.include_clauses_from(other_contract) ⇒ Object

include_clauses_from

Facilitates composition of multiple contracts



63
64
65
66
67
# File 'lib/duckpond/contract.rb', line 63

def include_clauses_from(other_contract)
  other_contract.each_clause do |other_clause|
    has_method other_clause.method_name
  end
end