class Oval::OneOf

Describe a value that must match one of the shapes.

**Example 1**: Value must be an Array or nil

“‘ruby OneOf[Container, nil] “`

Attributes

decls[R]

Public Class Methods

new(*decls) click to toggle source
# File lib/oval/one_of.rb, line 38
def initialize(*decls)
  self.decls = decls
end

Public Instance Methods

it_should() click to toggle source
# File lib/oval/one_of.rb, line 28
def it_should
  if decls.empty?
    "be absent"
  else
    output = decls[0..-2].map{|k| self.class.it_should(k)}.join(', ')
    output.empty? ? self.class.it_should(decls[0]) :
                    [output, self.class.it_should(decls[-1])].join(' or ')
  end
end
validate(thing, subject = nil) click to toggle source
# File lib/oval/one_of.rb, line 11
def validate(thing, subject = nil)
  ok = false
  self.decls.each do |decl|
    begin
      self.class.validate(thing, decl)
      ok = true
      break
    rescue Oval::ValueError
    end
  end
  unless ok
    raise Oval::ValueError,
      "Invalid value #{thing.inspect}#{for_subject(subject)}. " +
      "Should #{self.it_should}"
  end
end

Private Instance Methods

decls=(decls) click to toggle source
# File lib/oval/one_of.rb, line 45
def decls=(decls)
  @decls = decls
end