Class: RDF::LDP::Container
- Defined in:
- lib/rdf/ldp/container.rb
Overview
An LDP Basic Container. This also serves as a base class for other container types. Containers are implemented as ‘RDF::LDP::RDFSources` with the ability to contain other resources.
Containers respond to ‘#post`, allowing new resources to be added to them. On the public interface (not running through HTTP/`#request`), this is supported by `#add` and `#remove`.
Containers will throw errors when attempting to edit them in conflict with LDP’s restrictions on changing containment triples.
Direct Known Subclasses
Constant Summary
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
-
.to_uri ⇒ RDF::URI
Uri with lexical representation ‘www.w3.org/ns/ldp#Container’.
Instance Method Summary collapse
-
#add(resource, transaction = nil) ⇒ Container
Adds a member ‘resource` to the container.
-
#add_containment_triple(resource, transaction = nil) ⇒ Container
Adds a containment triple for ‘resource` to the container’s ‘#graph`.
-
#container? ⇒ Boolean
Whether this is an ldp:Container.
-
#container_class ⇒ RDF::URI
A URI representing the container type.
-
#containment_triples ⇒ RDF::Query::Enumerator
The containment triples.
-
#create(input, content_type, &block) ⇒ Object
Create with validation as required for the LDP container.
-
#has_containment_triple?(statement) ⇒ Boolean
True if the containment triple exists.
-
#make_containment_triple(resource) ⇒ RDF::URI
The containment triple, with a graph_name pointing to ‘#graph`.
-
#remove(resource, transaction = nil) ⇒ Container
Removes a member ‘resource` from the container.
-
#remove_containment_triple(resource, transaction = nil) ⇒ Container
Remove a containment triple for ‘resource` to the container’s ‘#graph`.
-
#update(input, content_type, &block) ⇒ Object
Updates with validation as required for the LDP container.
Methods inherited from RDFSource
#destroy, #graph, #initialize, #rdf_source?, #to_response
Methods inherited from Resource
#allowed_methods, #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
Constructor Details
This class inherits a constructor from RDF::LDP::RDFSource
Class Method Details
.to_uri ⇒ RDF::URI
Returns uri with lexical representation ‘www.w3.org/ns/ldp#Container’.
20 21 22 |
# File 'lib/rdf/ldp/container.rb', line 20 def self.to_uri RDF::Vocab::LDP.Container end |
Instance Method Details
#add(resource, transaction = nil) ⇒ Container
Adds a member ‘resource` to the container. Handles containment and membership triples as appropriate for the container type.
If a transaction is passed as the second argument, the additon of the containment triple is completed when the transaction closes; otherwise it is handled atomically.
76 77 78 |
# File 'lib/rdf/ldp/container.rb', line 76 def add(resource, transaction = nil) add_containment_triple(resource.to_uri, transaction) end |
#add_containment_triple(resource, transaction = nil) ⇒ Container
Adds a containment triple for ‘resource` to the container’s ‘#graph`.
If a transaction is passed as the second argument, the triple is added to the transaction’s inserts; otherwise it is added directly to ‘#graph`.
119 120 121 122 123 124 |
# File 'lib/rdf/ldp/container.rb', line 119 def add_containment_triple(resource, transaction = nil) target = transaction || graph target.insert make_containment_triple(resource) set_last_modified(transaction) # #set_last_modified handles nil case self end |
#container? ⇒ Boolean
Returns whether this is an ldp:Container.
26 27 28 |
# File 'lib/rdf/ldp/container.rb', line 26 def container? true end |
#container_class ⇒ RDF::URI
Returns a URI representing the container type.
32 33 34 |
# File 'lib/rdf/ldp/container.rb', line 32 def container_class CONTAINER_CLASSES[:basic] end |
#containment_triples ⇒ RDF::Query::Enumerator
Returns the containment triples.
98 99 100 |
# File 'lib/rdf/ldp/container.rb', line 98 def containment_triples graph.query([subject_uri, CONTAINS_URI, nil]).statements end |
#create(input, content_type, &block) ⇒ Object
Create with validation as required for the LDP container.
42 43 44 45 46 47 48 |
# File 'lib/rdf/ldp/container.rb', line 42 def create(input, content_type, &block) super do |transaction| validate_triples!(transaction) yield transaction if block_given? end self end |
#has_containment_triple?(statement) ⇒ Boolean
Returns true if the containment triple exists.
106 107 108 |
# File 'lib/rdf/ldp/container.rb', line 106 def has_containment_triple?(statement) !containment_triples.find { |t| statement == t }.nil? end |
#make_containment_triple(resource) ⇒ RDF::URI
Returns the containment triple, with a graph_name pointing to ‘#graph`.
147 148 149 150 |
# File 'lib/rdf/ldp/container.rb', line 147 def make_containment_triple(resource) RDF::Statement(subject_uri, CONTAINS_URI, resource, graph_name: subject_uri) end |
#remove(resource, transaction = nil) ⇒ Container
Removes a member ‘resource` from the container. Handles containment and membership triples as appropriate for the container type.
If a transaction is passed as the second argument, the removal of the containment triple is completed when the transaction closes; otherwise it is handled atomically.
92 93 94 |
# File 'lib/rdf/ldp/container.rb', line 92 def remove(resource, transaction = nil) remove_containment_triple(resource.to_uri, transaction) end |
#remove_containment_triple(resource, transaction = nil) ⇒ Container
Remove a containment triple for ‘resource` to the container’s ‘#graph`.
If a transaction is passed as the second argument, the triple is added to the transaction’s deletes; otherwise it is deleted directly from ‘#graph`.
135 136 137 138 139 140 |
# File 'lib/rdf/ldp/container.rb', line 135 def remove_containment_triple(resource, transaction = nil) target = transaction || graph target.delete(make_containment_triple(resource)) set_last_modified(transaction) # #set_last_modified handles nil case self end |
#update(input, content_type, &block) ⇒ Object
Updates with validation as required for the LDP container.
56 57 58 59 60 61 62 |
# File 'lib/rdf/ldp/container.rb', line 56 def update(input, content_type, &block) super do |transaction| validate_triples!(transaction) yield transaction if block_given? end self end |