Class: Structure::Type::Single

Inherits:
Object
  • Object
show all
Defined in:
lib/structure.rb

Direct Known Subclasses

Array

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#classesObject (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

Returns:

  • (Boolean)


20
21
22
# File 'lib/structure.rb', line 20

def class?
  @classes.all? { |c| c.is_a?(Class) || c.is_a?(Module) }
end

#inspectObject



35
36
37
# File 'lib/structure.rb', line 35

def inspect
  "one_of(#{@classes})"
end

#matches?(json) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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