Class: Oval::OneOf
Overview
Describe a value that must match one of the shapes.
Example 1: Value must be an Array or nil
ruby
OneOf[Container[Array], nil]
Instance Attribute Summary collapse
-
#decls ⇒ Object
readonly
Returns the value of attribute decls.
Instance Method Summary collapse
-
#initialize(*decls) ⇒ OneOf
constructor
A new instance of OneOf.
-
#validate(thing, subject = nil) ⇒ Object
Methods inherited from Base
[], ensure_equal, ensure_match
Constructor Details
#initialize(*decls) ⇒ OneOf
Returns a new instance of OneOf.
26 27 28 |
# File 'lib/oval/one_of.rb', line 26 def initialize(*decls) self.decls = decls end |
Instance Attribute Details
#decls ⇒ Object
Returns the value of attribute decls.
30 31 32 |
# File 'lib/oval/one_of.rb', line 30 def decls @decls end |
Instance Method Details
#validate(thing, subject = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/oval/one_of.rb', line 11 def validate(thing, subject = nil) ok = false self.decls.each do |decl| begin self.class.ensure_match(thing, decl) ok = true break rescue Oval::ValueError end end unless ok raise Oval::ValueError, "Invalid value #{thing.inspect}#{for_subject(subject)}" end end |