Sha256: c2423fd96010cfe9a9536e0a5a3ed5ac6dbc8e226fafe48ee74b4360a765ae49

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# coding: utf-8
require 'aepic'

module Aepic
  module Concerns
    module Serializer
      extend ActiveSupport::Concern

      XSD_TYPES = Hash.new do |hash, key|
        hash[key] = "xsd:#{key}"
      end.merge({
      })

      included { attributes :id }

      module ClassMethods
        def jsonld_context
          {}.tap do |context|
            schema[:attributes].each do |name, type|
              context[name] = XSD_TYPES[type]
            end
            schema[:associations].each do |name, type|
              class_name = name.to_s
              if class_name =~ /_ids?\Z/
                context[name] = 'xsd:integer'
              else
                class_name = name.to_s.classify
                associated_class = "#{class_name}Decorator".constantize
                context[name] = associated_class.jsonld_context
              end
            end
          end
        end

        def jsonld
          {'@context' => {xsd: 'http://www.w3.org/2001/XMLSchema#'}.merge(jsonld_context)}
        end
      end

      #def root_name
      #  self.class._root
      #end

      def attributes
        {'@context' => jsonld_context}.merge(super)
      end

      def jsonld_context
        object.jsonld_context
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aepic-0.0.4 lib/aepic/concerns/serializer.rb
aepic-0.0.2 lib/aepic/concerns/serializer.rb