class RDF::Changeset

An RDF changeset that can be applied to an {RDF::Mutable}.

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

@example 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

@example 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)

@note When applying a Changeset, deletes are resolved before inserts.

@since 2.0.0

Attributes

deletes[R]

RDF statements to delete when applied.

@return [RDF::Enumerable]

inserts[R]

RDF statements to insert when applied.

@return [RDF::Enumerable]

options[R]

Any additional options for this changeset.

@return [Hash{Symbol => Object}]

Public Class Methods

apply(mutable, **options, &block) click to toggle source

Applies a changeset to the given {RDF::Mutable} object.

@param [RDF::Mutable] mutable @param [Hash{Symbol => Object}] options @yield [changes] @yieldparam [RDF::Changeset] changes @return [void]

# File lib/rdf/changeset.rb, line 45
def self.apply(mutable, **options, &block)
  self.new(&block).apply(mutable, **options)
end
new(insert: [], delete: [], &block) click to toggle source

Initializes this changeset.

@param [RDF::Enumerable] insert (RDF::Graph.new) @param [RDF::Enumerable] delete (RDF::Graph.new) @yield [changes] @yieldparam [RDF::Changeset] changes

# 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

Public Instance Methods

<<(*statements)
Alias for: insert
>>(*statements)
Alias for: delete
apply(mutable, **options) click to toggle source

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

This operation executes as a single write transaction.

@param [RDF::Mutable] mutable @param [Hash{Symbol => Object}] options @return [void]

# File lib/rdf/changeset.rb, line 128
def apply(mutable, **options)
  mutable.apply_changeset(self)
end
count() click to toggle source

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

@return [Integer]

# File lib/rdf/changeset.rb, line 160
def count
  inserts.count + deletes.count
end
delete(*statements) click to toggle source

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

@param statements [Enumerable, RDF::Statement] Some statements @return [self]

# File lib/rdf/changeset.rb, line 186
def delete(*statements)
  coerce_statements(statements) do |stmts|
    append_statements :deletes, stmts
  end

  self
end
Also aliased as: delete!, >>
delete!(*statements)
Alias for: delete
empty?() click to toggle source

@return [Boolean] ‘true` iff inserts and deletes are both empty

# File lib/rdf/changeset.rb, line 134
def empty?
  deletes.empty? && inserts.empty?
end
insert(*statements) click to toggle source

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

@param statements [Enumerable, RDF::Statement] Some statements @return [self]

# File lib/rdf/changeset.rb, line 170
def insert(*statements)
  coerce_statements(statements) do |stmts|
    append_statements :inserts, stmts
  end

  self
end
Also aliased as: insert!, <<
insert!(*statements)
Alias for: insert
inspect() click to toggle source

Returns a developer-friendly representation of this changeset.

@return [String]

# 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!() click to toggle source

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

@return [void]

# File lib/rdf/changeset.rb, line 152
def inspect!
  $stderr.puts(self.inspect)
end
mutable?() click to toggle source

Returns ‘false` as changesets are not {RDF::Mutable}.

@return [Boolean] @see RDF::Mutable#mutable?

# File lib/rdf/changeset.rb, line 116
def mutable?
  false
end
readable?() click to toggle source

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 {RDF::Changeset#inserts} and {RDF::Changeset#deletes} accessors.

@return [Boolean] @see RDF::Readable#readable?

# File lib/rdf/changeset.rb, line 98
def readable?
  false
end
writable?() click to toggle source

Returns ‘false` as changesets are not {RDF::Writable}.

@return [Boolean] @see RDF::Writable#writable?

# File lib/rdf/changeset.rb, line 107
def writable?
  false
end

Private Instance Methods

append_statements(target, arg) click to toggle source

Append statements to the appropriate target. This is a little shim to go in between the other shim and the target.

@param target [Symbol] the method to send @param arg [Enumerable, RDF::Statement]

# File lib/rdf/changeset.rb, line 205
def append_statements(target, arg)
  # coerce to an enumerator
  stmts = case
          when arg.is_a?(RDF::Statement)
            [arg]
          when arg.respond_to?(:each_statement)
            arg.each_statement
          when arg.respond_to?(:each)
            arg
          else
            raise ArgumentError, "Invalid statement: #{arg.class}"
          end

  stmts.each { |s| send(target) << s }
end
query(stmt) click to toggle source

This simply returns its argument as a query in order to trick {RDF::Mutable#delete} into working.

# File lib/rdf/changeset.rb, line 223
def query(stmt)
  RDF::Query.new RDF::Query::Pattern.from(stmt)
end