Class: Contracts::Or

Inherits:
CallableClass show all
Defined in:
lib/contracts/builtin_contracts.rb

Overview

Takes a variable number of contracts. The contract passes if any of the contracts pass. Example: Or[Fixnum, Float]

Direct Known Subclasses

Maybe

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ Or

Returns a new instance of Or.


83
84
85
# File 'lib/contracts/builtin_contracts.rb', line 83

def initialize(*vals)
  @vals = vals
end

Instance Method Details

#to_sObject


94
95
96
97
98
# File 'lib/contracts/builtin_contracts.rb', line 94

def to_s
  @vals[0, @vals.size-1].map do |x|
    InspectWrapper.create(x)
  end.join(", ") + " or " + InspectWrapper.create(@vals[-1]).to_s
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)

87
88
89
90
91
92
# File 'lib/contracts/builtin_contracts.rb', line 87

def valid?(val)
  @vals.any? do |contract|
    res, _ = Contract.valid?(val, contract)
    res
  end
end