Module: Ladder::Resource::Serializable
- Included in:
- Ladder::Resource
- Defined in:
- lib/ladder/resource/serializable.rb
Instance Method Summary collapse
-
#as_framed_jsonld ⇒ Hash
Return a framed, compacted JSON-LD representation by embedding related objects from the graph.
-
#as_jsonld(opts = {}) ⇒ Hash
Return a JSON-LD representation for the resource.
-
#as_qname(opts = {}) ⇒ Hash
Return a qname-based JSON representation.
Instance Method Details
#as_framed_jsonld ⇒ Hash
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
26 27 28 29 30 31 32 33 34 |
# File 'lib/ladder/resource/serializable.rb', line 26 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 = {}) ⇒ Hash
Return a JSON-LD representation for the resource
14 15 16 |
# File 'lib/ladder/resource/serializable.rb', line 14 def as_jsonld(opts = {}) 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ladder/resource/serializable.rb', line 42 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.keys.include? field_name if opts[:related] qname_hash[ns][name] = send(field_name).to_a.map(&:as_qname) else qname_hash[ns][name] = send(field_name).to_a.map { |obj| "#{obj.class.name.underscore.pluralize}:#{obj.id}" } end elsif fields.keys.include? 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 qname_hash end |