Module: RDF::Mutable
- Extended by:
- Util::Aliasing::LateBound
- Includes:
- Readable, Util::Coercions, Writable
- Included in:
- Graph, Repository, Transaction
- Defined in:
- lib/rdf/mixin/mutable.rb
Overview
Classes that include this module must implement the methods
#insert_statement
, #delete_statement
and #each_statement
.
Instance Method Summary collapse
-
#<<(data) ⇒ Mutable
Inserts RDF data into
self
. -
#apply_changeset(changeset) ⇒ Boolean
Applies the given changeset.
-
#clear ⇒ Mutable
(also: #clear!)
Deletes all RDF statements from
self
. -
#delete(*statements) ⇒ Object
(also: #delete!)
Deletes RDF statements from
self
. -
#delete_insert(deletes, inserts) ⇒ Mutable
(also: #delete_insert!)
Performs a set of deletes and inserts as a combined operation.
-
#delete_statement(statement)
protected
abstract
Deletes an RDF statement from the underlying storage.
-
#delete_statements(statements)
protected
Deletes the given RDF statements from the underlying storage.
-
#immutable? ⇒ Boolean
Returns
true
ifself
is immutable. -
#insert(*statements) ⇒ Object
Inserts RDF statements into
self
. -
#load(url, graph_name: nil, **options)
(also: #load!)
Loads RDF statements from the given file or URL into
self
. -
# ⇒ String
Implements #from_reader for each available instance of Reader, based on the reader symbol.
-
#mutable? ⇒ Boolean
Returns
true
ifself
is mutable. - #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#snapshot ⇒ Dataset
A readable & queryable snapshot of the repository for isolated reads.
-
#update(*statements) ⇒ Object
(also: #update!)
Updates RDF statements in
self
.
Methods included from Util::Aliasing::LateBound
Methods included from Util::Coercions
Methods included from Writable
#insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?
Methods included from Readable
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Method Details
#<<(data) ⇒ Mutable
Inserts RDF data into self
.
69 70 71 72 73 |
# File 'lib/rdf/mixin/mutable.rb', line 69 def <<(data) raise TypeError.new("#{self} is immutable") if immutable? super # RDF::Writable#<< end |
#apply_changeset(changeset) ⇒ Boolean
Applies the given changeset
If #supports?(:atomic_write)
is true
, this must apply the changeset
atomically. Otherwise, it should offer an efficient implementation of a
combined delete/insert of the changeset.
200 201 202 |
# File 'lib/rdf/mixin/mutable.rb', line 200 def apply_changeset(changeset) delete_insert(changeset.deletes, changeset.inserts) end |
#clear ⇒ Mutable Also known as: clear!
Deletes all RDF statements from self
.
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/rdf/mixin/mutable.rb', line 222 def clear raise TypeError.new("#{self} is immutable") if immutable? if respond_to?(:clear_statements, true) clear_statements else delete_statements(self) end return self end |
#delete(*statements) ⇒ self #delete(statements) ⇒ self Also known as: delete!
using splat argument syntax with excessive arguments provided
Deletes RDF statements from self
.
If any statement contains a Query::Variable, it is
considered to be a pattern, and used to query
self to find matching statements to delete.
significantly affects performance. Use Enumerator form for large numbers of statements.
155 156 157 158 159 160 161 162 163 |
# File 'lib/rdf/mixin/mutable.rb', line 155 def delete(*statements) raise TypeError.new("#{self} is immutable") if immutable? coerce_statements(statements, query: true, constant: true) do |value| delete_statements(value) end return self end |
#delete_insert(deletes, inserts) ⇒ Mutable Also known as: delete_insert!
in the base implementation, this is equivalent to calling #delete
and #insert
sequentially. This method is preferred to take advantage
of (e.g.) RDF::Repositories
that can execute the operation in a single
request.
Performs a set of deletes and inserts as a combined operation.
182 183 184 185 186 187 |
# File 'lib/rdf/mixin/mutable.rb', line 182 def delete_insert(deletes, inserts) deletes.respond_to?(:each_statement) ? delete(deletes) : delete(*deletes) inserts.respond_to?(:each_statement) ? insert(inserts) : insert(*inserts) self end |
#delete_statement(statement) (protected)
This method returns an undefined value.
Deletes an RDF statement from the underlying storage.
Subclasses of Repository must implement this method, except if they are immutable.
292 293 294 |
# File 'lib/rdf/mixin/mutable.rb', line 292 def delete_statement(statement) raise NotImplementedError.new("#{self.class}#delete_statement") end |
#delete_statements(statements) (protected)
This method returns an undefined value.
Deletes the given RDF statements from the underlying storage.
Defaults to invoking #delete_statement for each given statement.
Subclasses of Repository may wish to override this method if they are capable of more efficiently deleting multiple statements at once.
276 277 278 279 280 281 |
# File 'lib/rdf/mixin/mutable.rb', line 276 def delete_statements(statements) each = statements.respond_to?(:each_statement) ? :each_statement : :each statements.__send__(each) do |statement| delete_statement(statement) end end |
#immutable? ⇒ Boolean
Returns true
if self
is immutable.
28 29 30 |
# File 'lib/rdf/mixin/mutable.rb', line 28 def immutable? !mutable? end |
#insert(*statements) ⇒ self #insert(statements) ⇒ self
using splat argument syntax with excessive arguments provided
Inserts RDF statements into self
.
significantly affects performance. Use Enumerator form for large numbers of statements.
93 94 95 96 97 |
# File 'lib/rdf/mixin/mutable.rb', line 93 def insert(*statements) raise TypeError.new("#{self} is immutable") if immutable? super # RDF::Writable#insert end |
#load(url, graph_name: nil, **options) Also known as: load!
This method returns an undefined value.
Loads RDF statements from the given file or URL into self
.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rdf/mixin/mutable.rb', line 41 def load(url, graph_name: nil, **) raise TypeError.new("#{self} is immutable") if immutable? Reader.open(url, base_uri: url, **) do |reader| if graph_name statements = [] reader.each_statement do |statement| statement.graph_name = graph_name statements << statement end insert_statements(statements) statements.size else insert_statements(reader) nil end end end |
#mutable? ⇒ Boolean
Returns true
if self
is mutable.
19 20 21 |
# File 'lib/rdf/mixin/mutable.rb', line 19 def mutable? writable? end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
this instantiates an entire reader; it could probably be done more
efficiently by refactoring RDF::Reader
and/or RDF::Format
to expose
a list of valid format symbols.
258 259 260 261 |
# File 'lib/rdf/mixin/mutable.rb', line 258 def respond_to_missing?(name, include_private = false) return RDF::Reader.for(name.to_s[5..-1].to_sym) if name.to_s[0,5] == 'from_' super end |
#snapshot ⇒ Dataset
A readable & queryable snapshot of the repository for isolated reads.
This method must be implemented when #supports(:snapshots)
is true
.
213 214 215 |
# File 'lib/rdf/mixin/mutable.rb', line 213 def snapshot raise NotImplementedError, "#{self.class} does not implement snapshots" end |
#update(*statements) ⇒ self #update(statements) ⇒ self Also known as: update!
using splat argument syntax with excessive arguments provided
Updates RDF statements in self
.
#update([subject, predicate, object])
is equivalent to
#delete([subject, predicate, nil])
followed by
#insert([subject, predicate, object])
unless object
is nil
.
significantly affects performance. Use Enumerator form for large numbers of statements.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rdf/mixin/mutable.rb', line 119 def update(*statements) raise TypeError.new("#{self} is immutable") if immutable? statements = statements[0] if statements.length == 1 && statements[0].is_a?(Enumerable) statements.each do |statement| if (statement = Statement.from(statement)) if statement.object? delete_insert([[statement.subject, statement.predicate, nil]], [statement]) else delete([statement.subject, statement.predicate, nil]) end end end end |