Sha256: 3927c80aa643aad3209ffe46c536ec8dd8f31e793251da95470723e5b6db2027

Contents?: true

Size: 897 Bytes

Versions: 1

Compression:

Stored size: 897 Bytes

Contents

module SoberSwag
  module Serializer
    ##
    # Extract out a hash from a list of
    # name/serializer pairs.
    class FieldList < Base

      def initialize(field_list)
        @field_list = field_list
      end

      attr_reader :field_list

      ##
      # Alias to make writing primitive stuff much easier
      def primitive(symbol)
        SoberSwag::Serializer.Primitive(SoberSwag::Types.const_get(symbol))
      end


      def serialize(object, options)
        field_list.map { |field|
          [field.name, field.serializer.serialize(object, options)]
        }.to_h
      end

      def type
        @type ||= make_struct_type!
      end

      private

      def make_struct_type!
        f = field_list
        Class.new(Dry::Struct) do
          f.each do |field|
            attribute field.name, field.serializer.type
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sober_swag-0.1.0 lib/sober_swag/serializer/field_list.rb