Method: JSON::LD::Context#serialize

Defined in:
lib/json/ld/context.rb

#serialize(provided_context: nil, **_options) ⇒ Hash

Generate @context

If a context was supplied in global options, use that, otherwise, generate one from this representation.

Parameters:

  • provided_context (Array, Hash, Context, IO, StringIO) (defaults to: nil)

    (nil) Original context to use, if available

  • options (Hash{Symbol => Object})

    ({})

Returns:

  • (Hash)


1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'lib/json/ld/context.rb', line 1010

def serialize(provided_context: nil, **_options)
  # log_debug("serlialize: generate context")
  # log_debug("") {"=> context: #{inspect}"}
  use_context = case provided_context
  when String, RDF::URI
    # log_debug "serlialize: reuse context: #{provided_context.inspect}"
    provided_context.to_s
  when Hash
    # log_debug "serlialize: reuse context: #{provided_context.inspect}"
    # If it has an @context entry use it, otherwise it is assumed to be the body of a context
    provided_context.fetch('@context', provided_context)
  when Array
    # log_debug "serlialize: reuse context: #{provided_context.inspect}"
    provided_context
  when IO, StringIO
    load_context(provided_context, **@options).fetch('@context', {})
  else
    ctx = {}
    ctx['@version'] = 1.1 if @processingMode == 'json-ld-1.1'
    ctx['@base'] = base.to_s if base
    ctx['@direction'] = default_direction.to_s if default_direction
    ctx['@language'] = default_language.to_s if default_language
    ctx['@vocab'] = vocab.to_s if vocab

    # Term Definitions
    term_definitions.each do |term, defn|
      ctx[term] = defn.to_context_definition(self)
    end
    ctx
  end

  # Return hash with @context, or empty
  use_context.nil? || use_context.empty? ? {} : { '@context' => use_context }
end