Class: RDFbus::Transaction

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

Overview

An RDFbus transaction.

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

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Constructor Details

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

Initializes this transaction.

Parameters:

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

Options Hash (options):

  • (RDF::Resource) :graph — default: nil
  • (RDF::Enumerable) :insert — default: []
  • (RDF::Enumerable) :delete — default: []

Yields:

  • (transaction)

Yield Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rdfbus/transaction.rb', line 55

def initialize(options = {}, &block)
  @graph   = options.delete(:graph)
  @inserts = options.delete(:insert) || RDF::Graph.new
  @deletes = options.delete(:delete) || RDF::Graph.new
  @options = options

  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:

  • (RDF::Enumerable)


32
33
34
# File 'lib/rdfbus/transaction.rb', line 32

def deletes
  @deletes
end

- (RDF::Resource) graph (readonly)

RDF graph to modify when executed.

Returns:

  • (RDF::Resource)


26
27
28
# File 'lib/rdfbus/transaction.rb', line 26

def graph
  @graph
end

- (RDF::Enumerable) inserts (readonly)

RDF statements to insert when executed.

Returns:

  • (RDF::Enumerable)


38
39
40
# File 'lib/rdfbus/transaction.rb', line 38

def inserts
  @inserts
end

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

Any additional options for this transaction.

Returns:

  • (Hash{Symbol => Object})


44
45
46
# File 'lib/rdfbus/transaction.rb', line 44

def options
  @options
end

Class Method Details

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

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:

  • (transaction)

Yield Parameters:



18
19
20
# File 'lib/rdfbus/transaction.rb', line 18

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:

  • (RDF::Statement) statement


154
155
156
# File 'lib/rdfbus/transaction.rb', line 154

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: {})


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rdfbus/transaction.rb', line 88

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)
  nil
end

- insert_statement(statement) (protected)

This method returns an undefined value.

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

Parameters:

  • (RDF::Statement) statement


145
146
147
# File 'lib/rdfbus/transaction.rb', line 145

def insert_statement(statement)
  inserts << statement
end

- (String) inspect

Returns a developer-friendly representation of this transaction.

Returns:

  • (String)


126
127
128
129
# File 'lib/rdfbus/transaction.rb', line 126

def inspect
  sprintf("#<%s:%#0x(graph: %s, deletes: %d, inserts: %d)>", self.class.name, object_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.



136
137
138
# File 'lib/rdfbus/transaction.rb', line 136

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

Returns:

  • (Boolean)

See Also:

  • RDF::Readable


78
79
80
# File 'lib/rdfbus/transaction.rb', line 78

def readable?
  false
end

- (Hash) to_json

Returns the JSON representation of this transaction.

Returns:

  • (Hash)


111
112
113
114
115
116
117
118
119
120
# File 'lib/rdfbus/transaction.rb', line 111

def to_json
  require 'rdf/json' unless defined?(RDF::JSON)
  json = options.dup.to_hash rescue {}
  json.merge!({
    :graph  => graph ? graph.to_uri.to_s : nil,
    :delete => deletes.to_rdf_json,
    :insert => inserts.to_rdf_json,
  })
  json.to_json
end