Class: RDF::LDP::RDFSource

Inherits:
Resource show all
Defined in:
lib/rdf/ldp/rdf_source.rb

Overview

The base class for all directly usable LDP Resources that *are not* ‘NonRDFSources`. RDFSources are implemented as a resource with:

- a `#graph` representing the "entire persistent state"
- a `#metagraph` containing internal properties of the RDFSource

Persistence schemes must be able to reconstruct both ‘#graph` and `#metagraph` accurately and separately (e.g. by saving them as distinct named graphs). Statements in `#metagraph` are considered canonical for the purposes of server-side operations; in the `RDF::LDP` core, this means they determine interaction model.

Note that the contents of ‘#metagraph`’s are not the same as LDP-server-managed triples. ‘#metagraph` contains statements internal properties of the RDFSource which are necessary for the server’s management purposes, but MAY be absent from the representation of its state in ‘#graph`.

Direct Known Subclasses

Container

Instance Attribute Summary collapse

Attributes inherited from Resource

#metagraph, #subject_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#allowed_methods, #container?, #containers, #destroyed?, #exists?, find, gen_id, interaction_model, #ldp_resource?, #match?, #non_rdf_source?, #request, #to_uri

Constructor Details

#initialize(subject_uri, data = RDF::Repository.new) ⇒ RDFSource

Returns a new instance of RDFSource.



44
45
46
47
48
# File 'lib/rdf/ldp/rdf_source.rb', line 44

def initialize(subject_uri, data = RDF::Repository.new)
  @graph = RDF::Graph.new(subject_uri, data: data)
  super
  self
end

Instance Attribute Details

#graphObject

a graph representing the current persistent state of the resource.



29
30
31
# File 'lib/rdf/ldp/rdf_source.rb', line 29

def graph
  @graph
end

Class Method Details

.to_uriRDF::URI

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

Returns:

See Also:



37
38
39
# File 'lib/rdf/ldp/rdf_source.rb', line 37

def to_uri 
  RDF::Vocab::LDP.RDFSource
end

Instance Method Details

#create(input, content_type) {|the| ... } ⇒ RDF::LDP::Resource

Creates the RDFSource, populating its graph from the input given

Parameters:

  • input (IO, File, #to_s)

    input (usually from a Rack env’s ‘rack.input` key) used to determine the Resource’s initial state.

  • content_type (#to_s)

    a MIME content_type used to read the graph.

Yields:

  • gives the new contents of ‘graph` to the caller’s block before altering the state of the resource. This is useful when validation is required or triples are to be added by a subclass.

Yield Parameters:

  • the (RDF::Enumerable)

    contents parsed from input.

Returns:

Raises:



70
71
72
73
74
75
76
# File 'lib/rdf/ldp/rdf_source.rb', line 70

def create(input, content_type, &block)
  super
  statements = parse_graph(input, content_type)
  yield statements if block_given?
  graph << statements
  self
end

#destroyObject

Clears the graph and marks as destroyed.



110
111
112
113
# File 'lib/rdf/ldp/rdf_source.rb', line 110

def destroy
  @graph.clear
  super
end

#etagString

TODO:

add an efficient hash function for RDF Graphs to RDF.rb and use that here?

Note:

the current implementation is a naive one that combines a couple of

Returns an Etag. This may be a strong or a weak ETag.

blunt heurisitics.



133
134
135
136
137
# File 'lib/rdf/ldp/rdf_source.rb', line 133

def etag
  subs = graph.subjects.map { |s| s.node? ? nil : s.to_s }
         .compact.sort.join()
  "\"#{Digest::SHA1.base64digest(subs)}#{graph.statements.count}\""
end

#rdf_source?Boolean

Returns whether this is an ldp:RDFSource.

Returns:

  • (Boolean)

    whether this is an ldp:RDFSource



148
149
150
# File 'lib/rdf/ldp/rdf_source.rb', line 148

def rdf_source?
  true
end

#to_responseObject

Returns the graph representing this resource’s state, without the graph context.



155
156
157
# File 'lib/rdf/ldp/rdf_source.rb', line 155

def to_response
  RDF::Graph.new << graph
end

#update(input, content_type) {|the| ... } ⇒ RDF::LDP::Resource

Updates the resource. Replaces the contents of ‘graph` with the parsed input.

Parameters:

  • input (IO, File, #to_s)

    input (usually from a Rack env’s ‘rack.input` key) used to determine the Resource’s new state.

  • content_type (#to_s)

    a MIME content_type used to interpret the input.

Yields:

  • gives the new contents of ‘graph` to the caller’s block before altering the state of the resource. This is useful when validation is required or triples are to be added by a subclass.

Yield Parameters:

  • the (RDF::Enumerable)

    triples parsed from input.

Returns:

Raises:



97
98
99
100
101
102
103
104
# File 'lib/rdf/ldp/rdf_source.rb', line 97

def update(input, content_type, &block)
  return create(input, content_type) unless exists?
  statements = parse_graph(input, content_type)
  yield statements if block_given?
  graph.clear!
  graph << statements
  self
end