Class: CoreLibrary::OneOf

Inherits:
UnionType
  • Object
show all
Defined in:
lib/apimatic-core/types/union_types/one_of.rb

Overview

Represents the OneOf union type in the core library

Instance Method Summary collapse

Constructor Details

#initialize(union_types, union_type_context = UnionTypeContext.new) ⇒ OneOf

Initializes a new instance of OneOf



7
8
9
10
# File 'lib/apimatic-core/types/union_types/one_of.rb', line 7

def initialize(union_types, union_type_context = UnionTypeContext.new)
  super(union_types, union_type_context)
  @collection_cases = nil
end

Instance Method Details

#deserialize(value, should_symbolize: false) ⇒ Object?

Deserializes a value based on the OneOf union type



62
63
64
65
66
67
# File 'lib/apimatic-core/types/union_types/one_of.rb', line 62

def deserialize(value, should_symbolize: false)
  return nil if value.nil?

  UnionTypeHelper.deserialize_value(value, @union_type_context, @collection_cases,
                                    @union_types, should_symbolize: should_symbolize)
end

#serialize(value) ⇒ Object?

Serializes a given value.



47
48
49
50
51
52
53
54
55
56
# File 'lib/apimatic-core/types/union_types/one_of.rb', line 47

def serialize(value)
  return nil if value.nil?

  UnionTypeHelper.serialize_value(
    value,
    @union_type_context,
    @collection_cases,
    @union_types
  )
end

#validate(value) ⇒ OneOf

Validates a value against the OneOf union type



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/apimatic-core/types/union_types/one_of.rb', line 15

def validate(value)
  context = @union_type_context
  UnionTypeHelper.update_nested_flag_for_union_types(@union_types)
  is_optional_or_nullable = UnionTypeHelper.optional_or_nullable_case?(
    context,
    @union_types.map(&:union_type_context)
  )

  if value.nil? && is_optional_or_nullable
    @is_valid = true
    return self
  end

  if value.nil?
    @is_valid = false
    @error_messages = UnionTypeHelper.process_errors(value, @union_types, @error_messages,
                                                     union_type_context.is_nested, true)
  end

  _validate_value_against_case(value, context)

  unless @is_valid
    @error_messages = UnionTypeHelper.process_errors(value, @union_types, @error_messages,
                                                     union_type_context.is_nested, true)
  end

  self
end