Class: Oval::OneOf
Overview
Describe a value that must match one of the shapes.
Example 1: Value must be an Array or nil
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.
- #it_should ⇒ Object
- #validate(thing, subject = nil) ⇒ Object
Methods inherited from Base
[], ensure_equal, it_should, validate
Constructor Details
#initialize(*decls) ⇒ OneOf
Returns a new instance of OneOf.
38 39 40 |
# File 'lib/oval/one_of.rb', line 38 def initialize(*decls) self.decls = decls end |
Instance Attribute Details
#decls ⇒ Object
Returns the value of attribute decls.
42 43 44 |
# File 'lib/oval/one_of.rb', line 42 def decls @decls end |
Instance Method Details
#it_should ⇒ Object
28 29 30 31 32 33 34 35 36 |
# 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) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |