Class: CoreLibrary::LeafType

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

Overview

Represents a leaf type in a UnionType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_to_match, union_type_context = UnionTypeContext.new) ⇒ LeafType

Initializes a new instance of LeafType

Parameters:

  • type_to_match (Class)

    The type to match against

  • union_type_context (UnionTypeContext) (defaults to: UnionTypeContext.new)

    The UnionTypeContext associated with the leaf type



9
10
11
12
# File 'lib/apimatic-core/types/union_types/leaf_type.rb', line 9

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

Instance Attribute Details

#type_to_matchObject (readonly)

Returns the value of attribute type_to_match.



4
5
6
# File 'lib/apimatic-core/types/union_types/leaf_type.rb', line 4

def type_to_match
  @type_to_match
end

Instance Method Details

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

Deserializes a value based on the leaf type

Parameters:

  • value (Object)

    The value to deserialize

  • should_symbolize (Boolean) (defaults to: false)

    Indicates whether the deserialized value should be symbolized.

Returns:

  • (Object, nil)

    The deserialized value or nil if the input value is nil



39
40
41
42
43
# File 'lib/apimatic-core/types/union_types/leaf_type.rb', line 39

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

  deserialize_value_against_case(value, @union_type_context, should_symbolize: should_symbolize)
end

#serialize(value) ⇒ Object



29
30
31
32
33
# File 'lib/apimatic-core/types/union_types/leaf_type.rb', line 29

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

  serialize_value_against_case(value, @union_type_context)
end

#validate(value) ⇒ LeafType

Validates a value against the leaf type

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (LeafType)

    The current LeafType object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/apimatic-core/types/union_types/leaf_type.rb', line 17

def validate(value)
  context = @union_type_context

  @is_valid = if value.nil?
                context.nullable_or_optional?
              else
                validate_value_against_case(value, context)
              end

  self
end