Class: RDF::LDP::RDFSource

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

Overview

Note:

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

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

Repository implementations must be able to reconstruct both ‘#graph` and `#metagraph` accurately and separately (e.g., by saving them as distinct named graphs).

The implementations of ‘#create` and `#update` in `RDF::LDP::Resource` are overloaded to handle the edits to `#graph` within the same transaction as the base `#metagraph` updates. `#to_response` is overloaded to return an unnamed `RDF::Graph`, to be transformed into an HTTP Body by `Rack::LDP::ContentNegotiation`.

Direct Known Subclasses

Container

Constant Summary

Constants inherited from Resource

RDF::LDP::Resource::CONTAINS_URI, RDF::LDP::Resource::INVALIDATED_AT_URI, RDF::LDP::Resource::MODIFIED_URI

Instance Attribute Summary

Attributes inherited from Resource

#metagraph, #subject_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#allowed_methods, #container?, #containers, #destroyed?, #etag, #exists?, find, gen_id, interaction_model, #last_modified, #ldp_resource?, #match?, metagraph_name, #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
49
# File 'lib/rdf/ldp/rdf_source.rb', line 44

def initialize(subject_uri, data = RDF::Repository.new)
  @subject_uri = subject_uri
  @data = data
  super
  self
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) {|tx| ... } ⇒ RDF::LDP::Resource

Creates the RDFSource, populating its graph from the input given

Examples:

repository = RDF::Repository.new
content = StringIO.new('<http://ex.org/1> <http://ex.org/p> "moomin" .')

ldprs = RDF::LDP::RDFSource.new('http://example.org/moomin', repository)
ldprs.create(content, 'text/turtle')

altering changes before execution with block syntax

content = '<http://ex.org/1> <http://ex.org/p> "moomin" .'

ldprs.create(StringIO.new(content), 'text/turtle') do |tx|
  tx.insert([RDF::URI('s'), RDF::URI('p'), 'custom'])
  tx.insert([RDF::URI('s'), RDF::URI('p'), 'custom', RDF::URI('g')])
end

validating changes before execution with block syntax

content = '<http://ex.org/1> <http://ex.org/p> "moomin" .'

ldprs.create(StringIO.new(content), 'text/turtle') do |tx|
  raise "cannot delete triples on create!" unless tx.deletes.empty?
end

Parameters:

  • input (IO, File)

    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 an in-progress transaction (changeset) to collect changes to graph, metagraph and other resources’ (e.g. containers) graphs.

Yield Parameters:

  • tx (RDF::Transaction)

    a transaction targeting ‘#graph` as the default graph name

Returns:

Raises:



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

def create(input, content_type, &block)
  super do |transaction|
    transaction.insert(parse_graph(input, content_type))
    yield transaction if block_given?
  end
end

#destroy(&block) ⇒ Object

Clears the graph and marks as destroyed.



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

def destroy(&block)
  super do |tx|
    tx.delete(RDF::Statement(nil, nil, nil, graph_name: subject_uri))
  end
end

#graphRDF::Graph

Returns a graph representing the current persistent state of the resource.

Returns:

  • (RDF::Graph)

    a graph representing the current persistent state of the resource.



54
55
56
# File 'lib/rdf/ldp/rdf_source.rb', line 54

def graph
  @graph ||= RDF::Graph.new(graph_name: @subject_uri, data: @data)
end

#rdf_source?Boolean

Returns whether this is an ldp:RDFSource.

Returns:

  • (Boolean)

    whether this is an ldp:RDFSource



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

def rdf_source?
  true
end

#to_responseObject

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



162
163
164
# File 'lib/rdf/ldp/rdf_source.rb', line 162

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

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

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

Examples:

altering changes before execution with block syntax

content = '<http://ex.org/1> <http://ex.org/prop> "moomin" .'

ldprs.update(StringIO.new(content), 'text/turtle') do |tx|
  tx.insert([RDF::URI('s'), RDF::URI('p'), 'custom'])
  tx.insert([RDF::URI('s'), RDF::URI('p'), 'custom', RDF::URI('g')])
end

Parameters:

  • input (IO, File)

    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 an in-progress transaction (changeset) to collect changes to graph, metagraph and other resources’ (e.g. containers) graphs.

Yield Parameters:

  • tx (RDF::Transaction)

    a transaction targeting ‘#graph` as the default graph name

Returns:

Raises:



134
135
136
137
138
139
140
141
# File 'lib/rdf/ldp/rdf_source.rb', line 134

def update(input, content_type, &block)
  super do |transaction|
    transaction
      .delete(RDF::Statement(nil, nil, nil, graph_name: subject_uri))
    transaction.insert parse_graph(input, content_type)
    yield transaction if block_given?
  end
end