Sha256: 00f6ffc658cee826d8eec0c3e0a32a7ea6a324ed133d95c85ecbde697c62593d

Contents?: true

Size: 736 Bytes

Versions: 1

Compression:

Stored size: 736 Bytes

Contents

module Chronicle
  module ETL
    module Utils
      module JSONAPI
        # For a given Chronicle::ETL::Model, serialize it as jsonapi
        def self.serialize(record)
          return unless record.is_a? Chronicle::ETL::Models::Base

          obj = record.identifier_hash
          obj[:attributes] = record.attributes

          relationships = Hash[record.associations.map do |k, v|
            if v.is_a?(Array)
              data = { data: v.map{ |association| serialize(association) } }
            else
              data = { data: serialize(v) }
            end

            [k, data]
          end]

          obj[:relationships] = relationships if relationships.any?
          obj
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-etl-0.2.4 lib/chronicle/etl/utils/jsonapi.rb