Class: CoreLibrary::UnionType

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core-interfaces/types/union_type.rb

Overview

Represents a union type that can validate, serialize and deserialize values based on a set of allowed types.

Constant Summary collapse

NATIVE_TYPES =

NATIVE_TYPES represents the list of native types in Ruby. These types are commonly used and built-in to the Ruby language. The constant contains the following types:

  • Integer: Represents whole numbers without a decimal point.

  • String: Represents a sequence of characters.

  • Float: Represents floating-point numbers with a decimal point.

  • TrueClass: Represents the boolean value ‘true`.

  • FalseClass: Represents the boolean value ‘false`.

This constant is used within the UnionType class to define the allowed native types.

[Integer, String, Float, TrueClass, FalseClass].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(union_types, union_type_context) ⇒ UnionType

Initializes a new instance of UnionType.

Parameters:

  • union_types (Array<Class>)

    An array of allowed types for the union.

  • union_type_context (Object)

    The context of the union type.



20
21
22
23
24
25
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 20

def initialize(union_types, union_type_context)
  @union_types = union_types
  @union_type_context = union_type_context
  @is_valid = false
  @error_messages = Set.new
end

Instance Attribute Details

#error_messagesObject

Returns the value of attribute error_messages.



15
16
17
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 15

def error_messages
  @error_messages
end

#is_validObject

Returns the value of attribute is_valid.



15
16
17
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 15

def is_valid
  @is_valid
end

#union_type_contextObject

Returns the value of attribute union_type_context.



15
16
17
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 15

def union_type_context
  @union_type_context
end

#union_typesObject

Returns the value of attribute union_types.



15
16
17
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 15

def union_types
  @union_types
end

Instance Method Details

#deserialize(value, should_symbolize: false) ⇒ Object

Deserializes a value based on the union type. This method should be implemented by subclasses.

Parameters:

  • value (Object)

    The value to deserialize.

  • should_symbolize (Boolean) (defaults to: false)

    Indicates whether the deserialized value should be symbolized.

Raises:

  • (NotImplementedError)

    If the method is not implemented in a subclass.



50
51
52
53
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 50

def deserialize(value, should_symbolize: false)
  raise NotImplementedError, 'This method needs
      to be implemented in a child class.'
end

#serialize(value) ⇒ String

Serializes a given value.

Parameters:

  • value (Object)

    The value to be serialized.

Returns:

  • (String)

    The serialized representation of the value.

Raises:

  • (NotImplementedError)

    If the method is not implemented in the child class.



40
41
42
43
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 40

def serialize(value)
  raise NotImplementedError, 'This method needs
      to be implemented in a child class.'
end

#validate(value) ⇒ Object

Validates a value against the union type. This method should be implemented by subclasses.

Parameters:

  • value (Object)

    The value to validate.

Raises:

  • (NotImplementedError)

    If the method is not implemented in a subclass.



31
32
33
34
# File 'lib/apimatic-core-interfaces/types/union_type.rb', line 31

def validate(value)
  raise NotImplementedError, 'This method needs
      to be implemented in a child class.'
end