Class: RDF::LDP::Container

Inherits:
RDFSource show all
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

DirectContainer

Constant Summary

Constants inherited from Resource

Resource::CONTAINS_URI, Resource::INVALIDATED_AT_URI, Resource::MODIFIED_URI

Instance Attribute Summary

Attributes inherited from Resource

#metagraph, #subject_uri

Class Method Summary collapse

Instance Method Summary collapse

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_uriRDF::URI

Returns uri with lexical representation ‘www.w3.org/ns/ldp#Container’.

Returns:



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.

Parameters:

  • a (RDF::Term)

    new member for this container

  • transaction (RDF::Transaction) (defaults to: nil)

    an active transaction as context for the addition

Returns:



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`.

Parameters:

  • resource (RDF::Term)

    a new member for this container

  • transaction (RDF::Transaction) (defaults to: nil)

Returns:



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.

Returns:

  • (Boolean)

    whether this is an ldp:Container



26
27
28
# File 'lib/rdf/ldp/container.rb', line 26

def container?
  true
end

#container_classRDF::URI

Returns a URI representing the container type.

Returns:

  • (RDF::URI)

    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_triplesRDF::Query::Enumerator

Returns the containment triples.

Returns:

  • (RDF::Query::Enumerator)

    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.

Raises:

  • (RDF::LDP::Conflict)

    if the create inserts triples that are not allowed by LDP for the container type

See Also:



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.

Parameters:

  • statement (RDF::Statement)

Returns:

  • (Boolean)

    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`.

Parameters:

  • a (RDF::Term)

    member to be represented in the containment triple

Returns:

  • (RDF::URI)

    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.

Parameters:

  • a (RDF::Term)

    new member for this container

  • transaction (RDF::Transaction) (defaults to: nil)

    an active transaction as context for the removal

Returns:



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`.

Parameters:

  • resource (RDF::Term)

    a member to remove from this container

  • transaction (RDF::Transaction) (defaults to: nil)

Returns:



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.

Raises:

  • (RDF::LDP::Conflict)

    if the update edits triples that are not allowed by LDP for the container type

See Also:



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