Sha256: 45177f04cd85b15e552ca6f30cd3e4e80a0d37af0a17caf1d0d442df1ae3dd37

Contents?: true

Size: 966 Bytes

Versions: 9

Compression:

Stored size: 966 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)
          super()
          @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, _strict)
          value
        end

        def referenced_model_classes
          EMPTY_ARRAY
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

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