Class: Ree::Contracts::ArgContracts::Or

Inherits:
Object
  • Object
show all
Extended by:
Squarable
Includes:
Truncatable
Defined in:
lib/ree/contracts/arg_contracts/or.rb

Direct Known Subclasses

Nilor

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Squarable

[]

Methods included from Truncatable

#truncate

Constructor Details

#initialize(*contracts) ⇒ Or

Returns a new instance of Or.



11
12
13
# File 'lib/ree/contracts/arg_contracts/or.rb', line 11

def initialize(*contracts)
  @validators = contracts.map(&Validators.method(:fetch_for))
end

Instance Attribute Details

#validatorsObject (readonly)

Returns the value of attribute validators.



9
10
11
# File 'lib/ree/contracts/arg_contracts/or.rb', line 9

def validators
  @validators
end

Instance Method Details

#message(value, name, lvl = 1) ⇒ Object



25
26
27
# File 'lib/ree/contracts/arg_contracts/or.rb', line 25

def message(value, name, lvl = 1)
  "expected #{truncate(self.to_s, 30)}, got #{truncate(value.inspect)}"
end

#to_sObject



15
16
17
# File 'lib/ree/contracts/arg_contracts/or.rb', line 15

def to_s
  "Or[#{validators.map(&:to_s).join(', ')}]"
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/ree/contracts/arg_contracts/or.rb', line 19

def valid?(value)
  validators.any? do |validator|
    validator.call(value)
  end
end