Class: RDF::Transaction

Inherits:
Object
  • Object
show all
Includes:
Mutable
Defined in:
lib/rdf/transaction.rb

Overview

An RDF transaction.

Transactions consist of a sequence of RDF statements to delete from and a sequence of RDF statements to insert into a given named graph.

Examples:

Executing a transaction against a repository

repository = ...
RDF::Transaction.execute(repository) do |tx|
  subject = RDF::URI("http://example.org/article")
  tx.delete [subject, RDF::DC.title, "Old title"]
  tx.insert [subject, RDF::DC.title, "New title"]
end

Since:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Mutable

#<<, #clear, #delete, #delete_statements, #immutable?, #insert, #load, #mutable?, #update

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #insert_statements, #writable?

Constructor Details

- (Transaction) initialize(options = {}) {|tx| ... }

Initializes this transaction.

Parameters:

  • (Hash{Symbol => Object}) options (defaults to: {})

Options Hash (options):

Yields:

  • (tx)

Yield Parameters:

Since:

  • 0.3.0



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rdf/transaction.rb', line 65

def initialize(options = {}, &block)
  @options = options.dup
  @graph   = @options.delete(:graph)  || @options.delete(:context)
  @inserts = @options.delete(:insert) || RDF::Graph.new
  @deletes = @options.delete(:delete) || RDF::Graph.new
  @inserts.extend(RDF::Enumerable) unless @inserts.kind_of?(RDF::Enumerable)
  @deletes.extend(RDF::Enumerable) unless @deletes.kind_of?(RDF::Enumerable)

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Instance Attribute Details

- (RDF::Enumerable) deletes (readonly)

RDF statements to delete when executed.

Returns:

Since:

  • 0.3.0



42
43
44
# File 'lib/rdf/transaction.rb', line 42

def deletes
  @deletes
end

- (RDF::Resource) graph (readonly)

RDF graph to modify when executed.

Returns:

Since:

  • 0.3.0



36
37
38
# File 'lib/rdf/transaction.rb', line 36

def graph
  @graph
end

- (RDF::Enumerable) inserts (readonly)

RDF statements to insert when executed.

Returns:

Since:

  • 0.3.0



48
49
50
# File 'lib/rdf/transaction.rb', line 48

def inserts
  @inserts
end

- (Hash{Symbol => Object}) options (readonly)

Any additional options for this transaction.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 0.3.0



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

def options
  @options
end

Class Method Details

+ execute(repository, options = {}) {|tx| ... }

This method returns an undefined value.

Executes a transaction against the given RDF repository.

Parameters:

  • (RDF::Repository) repository
  • (Hash{Symbol => Object}) options (defaults to: {})

Yields:

  • (tx)

Yield Parameters:

Since:

  • 0.3.0



28
29
30
# File 'lib/rdf/transaction.rb', line 28

def self.execute(repository, options = {}, &block)
  self.new(&block).execute(repository, options)
end

Instance Method Details

- delete_statement(statement) (protected)

This method returns an undefined value.

Appends an RDF statement to the sequence to delete when executed.

Parameters:

See Also:

Since:

  • 0.3.0



153
154
155
# File 'lib/rdf/transaction.rb', line 153

def delete_statement(statement)
  deletes << statement
end

- execute(repository, options = {})

This method returns an undefined value.

Executes this transaction against the given RDF repository.

Parameters:

  • (RDF::Repository) repository
  • (Hash{Symbol => Object}) options (defaults to: {})

Since:

  • 0.3.0



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rdf/transaction.rb', line 100

def execute(repository, options = {})
  before_execute(repository, options) if respond_to?(:before_execute)

  deletes.each_statement do |statement|
    statement = statement.dup
    statement.context = graph
    repository.delete(statement)
  end

  inserts.each_statement do |statement|
    statement = statement.dup
    statement.context = graph
    repository.insert(statement)
  end

  after_execute(repository, options) if respond_to?(:after_execute)
  self
end

- insert_statement(statement) (protected)

This method returns an undefined value.

Appends an RDF statement to the sequence to insert when executed.

Parameters:

See Also:

Since:

  • 0.3.0



143
144
145
# File 'lib/rdf/transaction.rb', line 143

def insert_statement(statement)
  inserts << statement
end

- (String) inspect

Returns a developer-friendly representation of this transaction.

Returns:

  • (String)

Since:

  • 0.3.0



123
124
125
126
# File 'lib/rdf/transaction.rb', line 123

def inspect
  sprintf("#<%s:%#0x(graph: %s, deletes: %d, inserts: %d)>", self.class.name, __id__,
    graph ? graph.to_s : 'nil', deletes.count, inserts.count)
end

- inspect!

This method returns an undefined value.

Outputs a developer-friendly representation of this transaction to stderr.

Since:

  • 0.3.0



133
134
135
# File 'lib/rdf/transaction.rb', line 133

def inspect!
  warn(inspect)
end

- (Boolean) readable?

Returns false to indicate that this transaction is append-only.

Transactions do not support the RDF::Enumerable protocol directly. To enumerate the RDF statements to be inserted or deleted, use the #inserts and #deletes accessors.

Returns:

  • (Boolean)

See Also:

Since:

  • 0.3.0



90
91
92
# File 'lib/rdf/transaction.rb', line 90

def readable?
  false
end