Class: TypedData::Schema::UnionType

Inherits:
Type
  • Object
show all
Defined in:
lib/typed_data/schema/union_type.rb

Constant Summary

Constants inherited from Type

Type::SUPPORTED_LOGICAL_TYPES

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ UnionType

Returns a new instance of UnionType.

Parameters:

  • types (Array<String>)


8
9
10
11
12
# File 'lib/typed_data/schema/union_type.rb', line 8

def initialize(types)
  @types = types.map(&Schema.method(:build_type))
  @nullable_single = @types.size == 2 && @types.any? { |t| t.is_a?(NullType) }
  @nullable_primitive_type = @types.find(&:primitive?) if @nullable_single
end

Instance Method Details

#accept(visitor, value) ⇒ Object



14
15
16
# File 'lib/typed_data/schema/union_type.rb', line 14

def accept(visitor, value)
  visitor.visit_union(self, @types, value)
end

#match?(value) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/typed_data/schema/union_type.rb', line 26

def match?(value)
  @types.any? { |t| t.match?(value) }
end

#nullable_single?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/typed_data/schema/union_type.rb', line 30

def nullable_single?
  @nullable_single
end

#primitive?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/typed_data/schema/union_type.rb', line 22

def primitive?
  false
end

#to_sObject



18
19
20
# File 'lib/typed_data/schema/union_type.rb', line 18

def to_s
  @nullable_primitive_type&.to_s || "union_#{@types.map(&:to_s).join("_")}"
end