Class: RDF::Changeset

Inherits:
Object
  • Object
show all
Includes:
Util::Coercions
Defined in:
lib/rdf/changeset.rb

Overview

Note:

When applying a Changeset, deletes are resolved before inserts.

An RDF changeset that can be applied to an Mutable.

Changesets consist of a sequence of RDF statements to delete from and a sequence of RDF statements to insert into a target dataset.

Examples:

Applying a Changeset with block syntax

graph = RDF::Graph.new
graph << [RDF::URI('s_del'), RDF::URI('p_del'), RDF::URI('o_del')]

RDF::Changeset.apply(graph) do |c|
  c.insert [RDF::URI('s1'), RDF::URI('p1'), RDF::URI('o1')]
  c.insert [RDF::URI('s2'), RDF::URI('p2'), RDF::URI('o2')]
  c.delete [RDF::URI('s_del'), RDF::URI('p_del'), RDF::URI('o_del')]
end

Defining a changeset for later application to a Mutable

changes = RDF::Changeset.new do |c|
  c.insert [RDF::URI('s1'), RDF::URI('p1'), RDF::URI('o1')]
  c.insert [RDF::URI('s2'), RDF::URI('p2'), RDF::URI('o2')]
  c.delete [RDF::URI('s_del'), RDF::URI('p_del'), RDF::URI('o_del')]
end

graph = RDF::Graph.new
graph << [RDF::URI('s_del'), RDF::URI('p_del'), RDF::URI('o_del')]

changes.apply(graph) # or graph.apply_changeset(changes)

Since:

  • 2.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(insert: [], delete: []) {|changes| ... } ⇒ Changeset

Initializes this changeset.

Parameters:

Yields:

  • (changes)

Yield Parameters:

Since:

  • 2.0.0



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rdf/changeset.rb', line 74

def initialize(insert: [], delete: [], &block)
  @inserts = insert
  @deletes = delete

  @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 self.instance_eval(&block)
    end
  end
end

Instance Attribute Details

#deletesRDF::Enumerable (readonly)

RDF statements to delete when applied.

Returns:

Since:

  • 2.0.0



53
54
55
# File 'lib/rdf/changeset.rb', line 53

def deletes
  @deletes
end

#insertsRDF::Enumerable (readonly)

RDF statements to insert when applied.

Returns:

Since:

  • 2.0.0



59
60
61
# File 'lib/rdf/changeset.rb', line 59

def inserts
  @inserts
end

#optionsHash{Symbol => Object} (readonly)

Any additional options for this changeset.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 2.0.0



65
66
67
# File 'lib/rdf/changeset.rb', line 65

def options
  @options
end

Class Method Details

.apply(mutable, **options) {|changes| ... } ⇒ void

This method returns an undefined value.

Applies a changeset to the given Mutable object.

Parameters:

Yields:

  • (changes)

Yield Parameters:

Since:

  • 2.0.0



45
46
47
# File 'lib/rdf/changeset.rb', line 45

def self.apply(mutable, **options, &block)
  self.new(&block).apply(mutable, **options)
end

Instance Method Details

#apply(mutable, **options) ⇒ void

This method returns an undefined value.

Applies this changeset to the given mutable RDF::Enumerable.

This operation executes as a single write transaction.

Parameters:

Since:

  • 2.0.0



128
129
130
# File 'lib/rdf/changeset.rb', line 128

def apply(mutable, **options)
  mutable.apply_changeset(self)
end

#countInteger

Returns the sum of both the ‘inserts` and `deletes` counts.

Returns:

  • (Integer)

Since:

  • 2.0.0



160
161
162
# File 'lib/rdf/changeset.rb', line 160

def count
  inserts.count + deletes.count
end

#delete(*statements) ⇒ self Also known as: delete!, >>

Append statements to ‘deletes`. Statements may contain variables, although support will depend on the Mutable target.

Parameters:

Returns:

  • (self)

Since:

  • 2.0.0



186
187
188
189
190
191
192
# File 'lib/rdf/changeset.rb', line 186

def delete(*statements)
  coerce_statements(statements) do |stmts|
    append_statements :deletes, stmts
  end

  self
end

#empty?Boolean

Returns ‘true` iff inserts and deletes are both empty.

Returns:

  • (Boolean)

    ‘true` iff inserts and deletes are both empty

Since:

  • 2.0.0



134
135
136
# File 'lib/rdf/changeset.rb', line 134

def empty?
  deletes.empty? && inserts.empty?
end

#insert(*statements) ⇒ self Also known as: insert!, <<

Append statements to ‘inserts`. Statements should be constant as variable statements will at best be ignored or at worst raise an error when applied.

Parameters:

Returns:

  • (self)

Since:

  • 2.0.0



170
171
172
173
174
175
176
# File 'lib/rdf/changeset.rb', line 170

def insert(*statements)
  coerce_statements(statements) do |stmts|
    append_statements :inserts, stmts
  end

  self
end

#inspectString

Returns a developer-friendly representation of this changeset.

Returns:

  • (String)

Since:

  • 2.0.0



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

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

#inspect!void

This method returns an undefined value.

Outputs a developer-friendly representation of this changeset to ‘$stderr`.

Since:

  • 2.0.0



152
153
154
# File 'lib/rdf/changeset.rb', line 152

def inspect!
  $stderr.puts(self.inspect)
end

#mutable?Boolean

Returns ‘false` as changesets are not Mutable.

Returns:

  • (Boolean)

See Also:

Since:

  • 2.0.0



116
117
118
# File 'lib/rdf/changeset.rb', line 116

def mutable?
  false
end

#readable?Boolean

Returns ‘false` to indicate that this changeset is append-only.

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

  • 2.0.0



98
99
100
# File 'lib/rdf/changeset.rb', line 98

def readable?
  false
end

#writable?Boolean

Returns ‘false` as changesets are not Writable.

Returns:

  • (Boolean)

See Also:

Since:

  • 2.0.0



107
108
109
# File 'lib/rdf/changeset.rb', line 107

def writable?
  false
end