Class: TSJSON::ScalarUnion

Inherits:
Base
  • Object
show all
Defined in:
lib/types/scalar_union.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#compile, #index, #property, #valid?

Constructor Details

#initialize(types = []) ⇒ ScalarUnion

Returns a new instance of ScalarUnion.



5
6
7
8
9
10
11
12
13
# File 'lib/types/scalar_union.rb', line 5

def initialize(types = [])
  types.each do |t|
    unless t.is_a?(ScalarType)
      raise 'ScalarUnion may contain only Scalar types'
    end
  end

  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



3
4
5
# File 'lib/types/scalar_union.rb', line 3

def types
  @types
end

Instance Method Details

#validate(value) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/types/scalar_union.rb', line 15

def validate(value)
  @types.each { |type| return true if type.valid?(value) }

  raise ScalarUnionValidationError.new(
          expected_types: @types.map(&:name),
          received_type: value.class.name,
          received_value: value
        )
end