Sha256: 360eaefa7ffcd48a861f6f198ed3681811877a0e14ae5b911173c99b7a9193d2

Contents?: true

Size: 948 Bytes

Versions: 7

Compression:

Stored size: 948 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, _strict)
          value
        end

        def referenced_model_classes
          EMPTY_ARRAY
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
avromatic-3.0.2 lib/avromatic/model/types/fixed_type.rb
avromatic-3.0.1 lib/avromatic/model/types/fixed_type.rb
avromatic-3.0.0 lib/avromatic/model/types/fixed_type.rb
avromatic-2.4.0 lib/avromatic/model/types/fixed_type.rb
avromatic-2.3.0 lib/avromatic/model/types/fixed_type.rb
avromatic-2.2.6 lib/avromatic/model/types/fixed_type.rb
avromatic-2.2.5 lib/avromatic/model/types/fixed_type.rb