Module: Ladder::Resource::Serializable

Included in:
Ladder::Resource
Defined in:
lib/ladder/resource/serializable.rb

Instance Method Summary collapse

Instance Method Details

#as_framed_jsonldHash

Return a framed, compacted JSON-LD representation by embedding related objects from the graph

NB: Will NOT embed related objects with same @type. Spec under discussion, see github.com/json-ld/json-ld.org/issues/110

Returns:

  • (Hash)

    a serialized JSON-LD version of the resource



39
40
41
42
43
44
45
46
47
# File 'lib/ladder/resource/serializable.rb', line 39

def as_framed_jsonld
  json_hash = as_jsonld related: true

  context = json_hash['@context']
  frame = { '@context' => context }
  frame['@type'] = type.first.pname unless type.empty?

  JSON::LD::API.compact(JSON::LD::API.frame(json_hash, frame), context)
end

#as_jsonld(opts = { related: false }) ⇒ Hash

Return a JSON-LD representation for the resource

Parameters:

  • opts (Hash) (defaults to: { related: false })

    options to pass to ActiveTriples

Options Hash (opts):

  • :related (Boolean)

    whether to include related resources (default: false)

Returns:

  • (Hash)

    a serialized JSON-LD version of the resource

See Also:

  • ActiveTriples::Resource#dump


27
28
29
# File 'lib/ladder/resource/serializable.rb', line 27

def as_jsonld(opts = { related: false })
  JSON.parse update_resource(opts.slice :related).dump(:jsonld, { standard_prefixes: true }.merge(opts))
end

#as_qname(opts = {}) ⇒ Hash

Return a qname-based JSON representation

Parameters:

  • opts (Hash) (defaults to: {})

    options for serializaiton

Options Hash (opts):

  • :related (Boolean)

    whether to include related resources

Returns:

  • (Hash)

    a serialized ‘qname’ version of the resource



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ladder/resource/serializable.rb', line 55

def as_qname(opts = {})
  qname_hash = type.empty? ? {} : { rdf: { type: type.first.pname } }

  resource_class.properties.each do |field_name, property|
    ns, name = property.predicate.qname
    qname_hash[ns] ||= {}

    if relations[field_name]
      qname_hash[ns][name] = opts[:related] ? send(field_name).to_a.map(&:as_qname) : send(field_name).to_a.map { |obj| "#{obj.class.name.underscore.pluralize}:#{obj.id}" }
    end

    if fields[field_name]
      qname_hash[ns][name] = read_attribute(field_name)
    end

    # Remove empty/null values
    qname_hash[ns].delete_if { |_k, v| v.blank? }
  end

  # Insert labels for boosting search score
  { rdfs: { label: update_resource(opts.slice :related).rdf_label } }.merge qname_hash
end

#as_turtle(opts = { related: false }) ⇒ String

Return a Turtle representation for the resource

Parameters:

  • opts (Hash) (defaults to: { related: false })

    options to pass to ActiveTriples

Options Hash (opts):

  • :related (Boolean)

    whether to include related resources (default: false)

Returns:

  • (String)

    a serialized Turtle version of the resource

See Also:

  • ActiveTriples::Resource#dump


15
16
17
# File 'lib/ladder/resource/serializable.rb', line 15

def as_turtle(opts = { related: false })
  update_resource(opts.slice :related).dump(:ttl, { standard_prefixes: true }.merge(opts))
end