Class: Structure::Type::Single
- Inherits:
-
Object
- Object
- Structure::Type::Single
- Defined in:
- lib/structure.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns the value of attribute classes.
Instance Method Summary collapse
- #class? ⇒ Boolean
-
#initialize(classes) ⇒ Single
constructor
A new instance of Single.
- #inspect ⇒ Object
- #matches?(json) ⇒ Boolean
Constructor Details
#initialize(classes) ⇒ Single
Returns a new instance of Single.
16 17 18 |
# File 'lib/structure.rb', line 16 def initialize(classes) @classes = classes end |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns the value of attribute classes.
14 15 16 |
# File 'lib/structure.rb', line 14 def classes @classes end |
Instance Method Details
#class? ⇒ Boolean
20 21 22 |
# File 'lib/structure.rb', line 20 def class? @classes.all? { |c| c.is_a?(Class) || c.is_a?(Module) } end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/structure.rb', line 35 def inspect "one_of(#{@classes})" end |
#matches?(json) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/structure.rb', line 24 def matches?(json) result = classes.any? do |s| yield s, json rescue Structure::Type::Error false end raise MatchError, "#{json}\n is not one of #{inspect}" unless result true end |