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:

  • 0.3.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mutable

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

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #writable?

Constructor Details

#initialize(options = {}) {|tx| ... } ⇒ Transaction

Initializes this transaction.

Parameters:

  • options (Hash{Symbol => Object}) (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

#deletesRDF::Enumerable (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

#graphRDF::Resource (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

#insertsRDF::Enumerable (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

#optionsHash{Symbol => Object} (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| ... } ⇒ void

This method returns an undefined value.

Executes a transaction against the given RDF repository.

Parameters:

  • repository (RDF::Repository)
  • options (Hash{Symbol => Object}) (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

#execute(repository, options = {}) ⇒ void

This method returns an undefined value.

Executes this transaction against the given RDF repository.

Parameters:

  • repository (RDF::Repository)
  • options (Hash{Symbol => Object}) (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

#inspectString

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!void

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

#readable?Boolean

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