Module: RDF::Serializers::HextupleSerializer

Included in:
Relationship, Scalar
Defined in:
lib/rdf/serializers/hextuple_serializer.rb

Instance Method Summary collapse

Instance Method Details

#iri_from_record(record) ⇒ Object

Raises:

  • (FastJsonapi::MandatoryField)


6
7
8
9
10
11
12
# File 'lib/rdf/serializers/hextuple_serializer.rb', line 6

def iri_from_record(record)
  return record if record.try(:uri?)

  raise FastJsonapi::MandatoryField, 'record has no iri' unless record.respond_to?(:iri)

  record.iri
end

#normalized_object(object) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rdf/serializers/hextuple_serializer.rb', line 14

def normalized_object(object)
  case object
  when ::RDF::Term
    object
  when ActiveSupport::TimeWithZone
    ::RDF::Literal(object.to_datetime)
  else
    ::RDF::Literal(object)
  end
end

#object_datatype(obj) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rdf/serializers/hextuple_serializer.rb', line 35

def object_datatype(obj)
  if obj.is_a?(::RDF::URI)
    'http://www.w3.org/1999/02/22-rdf-syntax-ns#namedNode'
  elsif obj.is_a?(::RDF::Node)
    'http://www.w3.org/1999/02/22-rdf-syntax-ns#blankNode'
  elsif obj.try(:datatype?)
    obj.datatype
  else
    lit = RDF::Literal(obj)
    lit.datatype.to_s
  end
end

#object_value(obj) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rdf/serializers/hextuple_serializer.rb', line 25

def object_value(obj)
  if obj.is_a?(::RDF::URI)
    obj.value
  elsif obj.is_a?(::RDF::Node)
    obj.to_s
  else
    obj.value.to_s
  end
end

#operation(operation, graph_name) ⇒ Object



63
64
65
66
67
68
# File 'lib/rdf/serializers/hextuple_serializer.rb', line 63

def operation(operation, graph_name)
  return nil if operation.blank?
  return operation if graph_name.blank?

  "#{operation}?graph=#{WEBrick::HTTPUtils.escape_form(graph_name.to_s)}"
end

#value_to_hex(iri, predicate, object, graph = nil, serialization_params = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rdf/serializers/hextuple_serializer.rb', line 48

def value_to_hex(iri, predicate, object, graph = nil, serialization_params = {})
  return if object.nil?

  obj = normalized_object(object)

  [
    iri.to_s,
    predicate.to_s,
    object_value(obj),
    object_datatype(obj),
    obj.try(:language) || '',
    operation((graph || ::RDF::Serializers.config.default_graph)&.value, serialization_params[:context])
  ]
end