Class: RDF::LDP::DirectContainer
- Defined in:
- lib/rdf/ldp/direct_container.rb
Overview
An extension of ‘RDF::LDP::Container` implementing direct containment. This adds the concepts of a membership resource, predicate, and triples to the Basic Container’s containment triples.
When the membership resource is an ‘RDFSource`, the membership triple is added/removed from its graph when the resource created/deleted within the container. When the membership resource is a `NonRDFSource`, the triple is added/removed on its description’s graph instead.
A membership constant URI and membership predicate MUST be specified as described in LDP–exactly one of each. If none is given, we default to the container itself as a membership resource and ‘ldp:member` as predicate. If more than one of either is given, all `#add/#remove` (POST/DELETE) requests will fail.
Direct Known Subclasses
Constant Summary collapse
- MEMBER_URI =
RDF::Vocab::LDP.member.freeze
- MEMBERSHIP_RESOURCE_URI =
RDF::Vocab::LDP.membershipResource.freeze
- RELATION_TERMS =
[RDF::Vocab::LDP.hasMemberRelation.freeze, RDF::Vocab::LDP.isMemberOfRelation.freeze].freeze
Constants inherited from Resource
Resource::CONTAINS_URI, Resource::INVALIDATED_AT_URI, Resource::MODIFIED_URI
Instance Attribute Summary
Attributes inherited from Resource
Class Method Summary collapse
Instance Method Summary collapse
-
#add(resource, transaction = nil) ⇒ Object
Adds a member ‘resource` to the container.
-
#container_class ⇒ RDF::URI
A URI representing the container type.
-
#create(input, content_type) ⇒ Object
Creates and inserts default relation triples if none are given.
-
#make_membership_triple(resource) ⇒ RDF::URI
The membership triple representing membership of the ‘resource` parameter in this container.
-
#membership_constant_uri ⇒ RDF::URI
Gives the membership constant URI.
-
#membership_predicate ⇒ RDF::URI
Gives the membership predicate.
-
#remove(resource, transaction = nil) ⇒ Object
Removes a member ‘resource` to the container.
Methods inherited from Container
#add_containment_triple, #container?, #containment_triples, #has_containment_triple?, #make_containment_triple, #remove_containment_triple, #update
Methods inherited from RDFSource
#destroy, #graph, #initialize, #rdf_source?, #to_response, #update
Methods inherited from Resource
#allowed_methods, #container?, #containers, #destroy, #destroyed?, #etag, #exists?, find, gen_id, #initialize, interaction_model, #last_modified, #ldp_resource?, #match?, metagraph_name, #non_rdf_source?, #rdf_source?, #request, #to_response, #to_uri, #update
Constructor Details
This class inherits a constructor from RDF::LDP::RDFSource
Class Method Details
.to_uri ⇒ Object
27 28 29 |
# File 'lib/rdf/ldp/direct_container.rb', line 27 def self.to_uri RDF::Vocab::LDP.DirectContainer end |
Instance Method Details
#add(resource, transaction = nil) ⇒ Object
Adds a member ‘resource` to the container. Handles containment and adds membership triple to the memebership resource.
67 68 69 70 71 72 73 74 75 |
# File 'lib/rdf/ldp/direct_container.rb', line 67 def add(resource, transaction = nil) process_membership_resource(resource, transaction) do |container, quad, subject| super(subject, transaction) # super handles nil transaction case target = transaction || container.graph target.insert(quad) end self end |
#container_class ⇒ RDF::URI
Returns a URI representing the container type.
33 34 35 |
# File 'lib/rdf/ldp/direct_container.rb', line 33 def container_class CONTAINER_CLASSES[:direct] end |
#create(input, content_type) ⇒ Object
Make atomic. Consider just raising an error instead of adding triples. There’s a need to handle this issue for repositories with snapshot reads, as well as those without.
the addition of default triples is handled in a separate transaction. It is possible for the second transaction to fail, causing the resource to persist in an invalid state. It is also possible for a read to occur between the two transactions.
Creates and inserts default relation triples if none are given.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rdf/ldp/direct_container.rb', line 49 def create(input, content_type) super graph.transaction(mutable: true) do |tx| tx.insert(default_member_relation_statement) if member_relation_statements.empty? tx.insert(default_membership_resource_statement) if membership_resource_statements.empty? end self end |
#make_membership_triple(resource) ⇒ RDF::URI
Returns the membership triple representing membership of the ‘resource` parameter in this container.
134 135 136 137 138 139 |
# File 'lib/rdf/ldp/direct_container.rb', line 134 def make_membership_triple(resource) predicate = membership_predicate return RDF::Statement(membership_constant_uri, predicate, resource) if member_relation_statements.first.predicate == RELATION_TERMS.first RDF::Statement(resource, predicate, membership_constant_uri) end |
#membership_constant_uri ⇒ RDF::URI
Gives the membership constant URI. If none is present in the container state, we add the current resource as a membership constant.
102 103 104 105 106 107 108 |
# File 'lib/rdf/ldp/direct_container.rb', line 102 def membership_constant_uri statements = membership_resource_statements return statements.first.object if statements.count == 1 raise(NotAcceptable, 'An LDP-DC MUST have exactly one membership ' \ "resource; found #{statements.count}.") end |
#membership_predicate ⇒ RDF::URI
Gives the membership predicate. If none is present in the container state, we add the current resource as a membership constant.
119 120 121 122 123 124 125 |
# File 'lib/rdf/ldp/direct_container.rb', line 119 def membership_predicate statements = member_relation_statements return statements.first.object if statements.count == 1 raise(NotAcceptable, 'An LDP-DC MUST have exactly one member ' \ "relation triple; found #{statements.count}.") end |
#remove(resource, transaction = nil) ⇒ Object
Removes a member ‘resource` to the container. Handles containment and removes membership triple to the memebership resource.
82 83 84 85 86 87 88 89 90 |
# File 'lib/rdf/ldp/direct_container.rb', line 82 def remove(resource, transaction = nil) process_membership_resource(resource, transaction) do |container, quad, subject| super(subject, transaction) # super handles nil transaction case target = transaction || container.graph target.delete(quad) end self end |