Sha256: af4be7a851f0d7885b63df08b06b02622dfa9f42a09a3954b22fc2558b191988

Contents?: true

Size: 918 Bytes

Versions: 10

Compression:

Stored size: 918 Bytes

Contents

module Chronicle
  module ETL
    class JSONAPISerializer < Chronicle::ETL::Serializer
      def initialize(*args)
        super

        raise(SerializationError, "Record must be a subclass of Chronicle::ETL::Model::Base") unless @record.is_a?(Chronicle::ETL::Models::Base)
      end

      def serializable_hash
        @record
          .identifier_hash
          .merge({ attributes: @record.attributes })
          .merge({ relationships: build_associations })
          .merge(@record.meta_hash)
      end

      def build_associations
        @record.associations.transform_values do |value|
          association_data =
            if value.is_a?(Array)
              value.map { |record| JSONAPISerializer.new(record).serializable_hash }
            else
              JSONAPISerializer.new(value).serializable_hash
            end
          { data: association_data }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
chronicle-etl-0.5.5 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.5.4 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.5.3 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.5.2 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.5.1 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.5.0 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.4.4 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.4.3 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.4.2 lib/chronicle/etl/serializers/jsonapi_serializer.rb
chronicle-etl-0.4.1 lib/chronicle/etl/serializers/jsonapi_serializer.rb