Class: Contracts::Builtin::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[Integer, Float]

Direct Known Subclasses

Maybe

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ Or

Returns a new instance of Or.



100
101
102
103
# File 'lib/contracts/builtin_contracts.rb', line 100

def initialize(*vals)
  super()
  @vals = vals
end

Instance Method Details

#to_sObject



112
113
114
115
116
117
118
# File 'lib/contracts/builtin_contracts.rb', line 112

def to_s
  # rubocop:disable Style/StringConcatenation
  @vals[0, @vals.size-1].map do |x|
    InspectWrapper.create(x)
  end.join(", ") + " or " + InspectWrapper.create(@vals[-1]).to_s
  # rubocop:enable Style/StringConcatenation
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/contracts/builtin_contracts.rb', line 105

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