Module: RDF::Serializers::Scalar

Includes:
HextupleSerializer
Defined in:
lib/rdf/serializers/scalar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HextupleSerializer

#iri_from_record, #normalized_object, #object_datatype, #object_value, #operation, #value_to_hex

Instance Attribute Details

#datatypeObject

Returns the value of attribute datatype.



8
9
10
# File 'lib/rdf/serializers/scalar.rb', line 8

def datatype
  @datatype
end

#imageObject

Returns the value of attribute image.



8
9
10
# File 'lib/rdf/serializers/scalar.rb', line 8

def image
  @image
end

#predicateObject

Returns the value of attribute predicate.



8
9
10
# File 'lib/rdf/serializers/scalar.rb', line 8

def predicate
  @predicate
end

Instance Method Details

#initialize(key:, method:, options: {}) ⇒ Object



10
11
12
13
14
15
# File 'lib/rdf/serializers/scalar.rb', line 10

def initialize(key:, method:, options: {})
  super
  @predicate = options[:predicate]
  @image = options[:image]
  @datatype = options[:datatype]
end

#serialize_hex(record, serialization_params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rdf/serializers/scalar.rb', line 17

def serialize_hex(record, serialization_params)
  return [] unless conditionally_allowed?(record, serialization_params) && predicate.present?

  value = value_from_record(record, method, serialization_params)

  return [] if value.nil?

  if value.is_a?(Array)
    value.map { |arr_item| value_to_hex(iri_from_record(record).to_s, predicate, arr_item, nil, serialization_params) }
  elsif value.is_a?(::RDF::List)
    first = value.statements.first&.subject || RDF.nil
    value.statements.map do |statement|
      value_to_hex(statement.subject.to_s, statement.predicate, statement.object, statement.graph_name, serialization_params)
    end + [
      value_to_hex(iri_from_record(record).to_s, predicate, first, nil, serialization_params)
    ]
  else
    [value_to_hex(iri_from_record(record).to_s, predicate, value, nil, serialization_params)]
  end
end

#value_from_record(record, method, serialization_params) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rdf/serializers/scalar.rb', line 38

def value_from_record(record, method, serialization_params)
  if method.is_a?(Proc)
    FastJsonapi.call_proc(method, record, serialization_params)
  else
    v = record.public_send(method)
    v.is_a?('ActiveRecord'.safe_constantize&.const_get('Relation') || NilClass) ? v.to_a : v
  end
end