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

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

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

Direct Known Subclasses

Container

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.



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

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:



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

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
ldprs = RDF::LDP::RDFSource.new('http://example.org/moomin', repository)
ldprs.create('<http://ex.org/1> <http://ex.org/prop> "moomin" .', 'text/turtle')

altering changes before execution with block syntax

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

ldprs.create(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/prop> "moomin" .'

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

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



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

def create(input, content_type, &block)
  super do |transaction|
    transaction.graph_name = subject_uri
    statements = parse_graph(input, content_type)
    transaction << statements
    yield transaction if block_given?
  end
end

#destroy(&block) ⇒ Object

Clears the graph and marks as destroyed.



150
151
152
153
154
# File 'lib/rdf/ldp/rdf_source.rb', line 150

def destroy(&block)
  super do |_|
    graph.clear
  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.



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

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

#rdf_source?Boolean

Returns whether this is an ldp:RDFSource.

Returns:

  • (Boolean)

    whether this is an ldp:RDFSource



158
159
160
# File 'lib/rdf/ldp/rdf_source.rb', line 158

def rdf_source?
  true
end

#to_responseObject

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



165
166
167
# File 'lib/rdf/ldp/rdf_source.rb', line 165

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(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, #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 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:



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

def update(input, content_type, &block)
  super do |transaction|
    transaction.graph_name = subject_uri
    transaction << parse_graph(input, content_type)
    yield transaction if block_given?
    graph.clear
  end

  self
end