Sha256: f4b1c3f65f1c8e54973d2c17c093ec3790c1b2213f0e02596fa812068772ca3a

Contents?: true

Size: 871 Bytes

Versions: 5

Compression:

Stored size: 871 Bytes

Contents

# frozen_string_literal: true

require 'avromatic/model/types/abstract_type'

module Avromatic
  module Model
    module Types
      class FixedType < AbstractType
        VALUE_CLASSES = [::String].freeze

        attr_reader :size

        def initialize(size)
          @size = size
        end

        def name
          "fixed(#{size})"
        end

        def value_classes
          VALUE_CLASSES
        end

        def coerce(input)
          if coercible?(input)
            input
          else
            raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
          end
        end

        def coercible?(input)
          input.nil? || (input.is_a?(::String) && input.length == size)
        end

        alias_method :coerced?, :coercible?

        def serialize(value, **)
          value
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
avromatic-2.2.1 lib/avromatic/model/types/fixed_type.rb
avromatic-2.2.0 lib/avromatic/model/types/fixed_type.rb
avromatic-2.1.0 lib/avromatic/model/types/fixed_type.rb
avromatic-2.0.1 lib/avromatic/model/types/fixed_type.rb
avromatic-2.0.0 lib/avromatic/model/types/fixed_type.rb