Sha256: 9d67bfd642282139dcf85300e52ddb56ed5c7a2fa90bca5bcfb80dd99e6e5d1c

Contents?: true

Size: 902 Bytes

Versions: 3

Compression:

Stored size: 902 Bytes

Contents

module Chronicle
  module Serialization
    class JSONLDSerializer < Chronicle::Serialization::Serializer
      DEFAULT_CONTEXT = 'https://schema.chronicle.app/'.freeze

      def serializable_hash
        {
          '@context': DEFAULT_CONTEXT
        }.merge(serialize_record(@record))
      end

      private

      def serialize_record(record)
        properties = record.properties.to_h.compact.transform_values do |value|
          if value.is_a?(Array)
            value.map { |v| serialize_value(v) }
          else
            serialize_value(value)
          end
        end

        {
          '@type': record.type.to_s,
          '@id': record.id
        }.merge(properties).compact
      end

      def serialize_value(value)
        if value.is_a?(Chronicle::Serialization::Record)
          serialize_record(value)
        else
          value
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chronicle-core-0.3.2 lib/chronicle/serialization/jsonld_serializer.rb
chronicle-core-0.3.1 lib/chronicle/serialization/jsonld_serializer.rb
chronicle-core-0.3.0 lib/chronicle/serialization/jsonld_serializer.rb