Sha256: 8a41e0cc0e28f9ae7fa4d90f0c37a082ca0819a4ecb9ccd3085460e34e2e417e

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'avromatic/model/types/abstract_type'

module Avromatic
  module Model
    module Types
      class ArrayType < AbstractType
        VALUE_CLASSES = [::Array].freeze

        attr_reader :value_type

        def initialize(value_type:)
          super()
          @value_type = value_type
        end

        def value_classes
          VALUE_CLASSES
        end

        def name
          "array[#{value_type.name}]"
        end

        def coerce(input)
          if input.nil?
            input
          elsif input.is_a?(::Array)
            input.map { |element_input| value_type.coerce(element_input) }
          else
            raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
          end
        end

        def coercible?(input)
          input.nil? || (input.is_a?(::Array) && input.all? { |element_input| value_type.coercible?(element_input) })
        end

        def coerced?(value)
          value.nil? || (value.is_a?(::Array) && value.all? { |element| value_type.coerced?(element) })
        end

        def serialize(value, strict)
          if value.nil?
            value
          else
            value.map { |element| value_type.serialize(element, strict) }
          end
        end

        def referenced_model_classes
          value_type.referenced_model_classes
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
avromatic-5.1.0 lib/avromatic/model/types/array_type.rb
avromatic-5.0.0 lib/avromatic/model/types/array_type.rb
avromatic-4.3.0 lib/avromatic/model/types/array_type.rb
avromatic-4.2.0 lib/avromatic/model/types/array_type.rb
avromatic-4.1.1 lib/avromatic/model/types/array_type.rb
avromatic-4.1.0 lib/avromatic/model/types/array_type.rb
avromatic-4.0.0 lib/avromatic/model/types/array_type.rb