Module: RDF::Mutable

Extended by:
Util::Aliasing::LateBound
Includes:
Readable, 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.

See Also:

Instance Method Summary (collapse)

Methods included from Util::Aliasing::LateBound

alias_method

Methods included from Writable

#insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?

Methods included from Readable

#readable?

Instance Method Details

- (Mutable) <<(data)

Inserts RDF data into self.

Parameters:

Returns:

Raises:

  • (TypeError)

    if self is immutable

See Also:



65
66
67
68
69
# File 'lib/rdf/mixin/mutable.rb', line 65

def <<(data)
  raise TypeError.new("#{self} is immutable") if immutable?

  super # RDF::Writable#<<
end

- (Mutable) clear Also known as: clear!

Deletes all RDF statements from self.

Returns:

Raises:

  • (TypeError)

    if self is immutable



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rdf/mixin/mutable.rb', line 141

def clear
  raise TypeError.new("#{self} is immutable") if immutable?

  if respond_to?(:clear_statements)
    clear_statements
  else
    delete_statements(self)
  end

  return self
end

- (Mutable) delete(*statements) Also known as: delete!

Deletes RDF statements from self.

Parameters:

Returns:

Raises:

  • (TypeError)

    if self is immutable



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rdf/mixin/mutable.rb', line 113

def delete(*statements)
  raise TypeError.new("#{self} is immutable") if immutable?

  statements.map! do |value|
    case
      when value.respond_to?(:each_statement)
        delete_statements(value)
        nil
      when (statement = Statement.from(value)).valid?
        statement
      else
        delete_statements(query(value))
        nil
    end
  end
  statements.compact!
  delete_statements(statements) unless statements.empty?

  return self
end

- delete_statement(statement) (protected)

This method is abstract.

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.

Parameters:

Raises:

  • (NotImplementedError)


184
185
186
# File 'lib/rdf/mixin/mutable.rb', line 184

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.

Parameters:



168
169
170
171
172
173
# File 'lib/rdf/mixin/mutable.rb', line 168

def delete_statements(statements)
  each = statements.respond_to?(:each_statement) ? :each_statement : :each
  statements.__send__(each) do |statement|
    delete_statement(statement)
  end
end

- (Boolean) immutable?

Returns true if self is immutable.

Returns:

  • (Boolean)

See Also:



27
28
29
# File 'lib/rdf/mixin/mutable.rb', line 27

def immutable?
  !mutable?
end

- (Mutable) insert(*statements)

Inserts RDF statements into self.

Parameters:

Returns:

Raises:

  • (TypeError)

    if self is immutable

See Also:



78
79
80
81
82
# File 'lib/rdf/mixin/mutable.rb', line 78

def insert(*statements)
  raise TypeError.new("#{self} is immutable") if immutable?

  super # RDF::Writable#insert
end

- load(filename, options = {}) Also known as: load!

This method returns an undefined value.

Loads RDF statements from the given file or URL into self.

Parameters:

  • (String, #to_s) filename
  • (Hash{Symbol => Object}) options (defaults to: {})

Raises:

  • (TypeError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rdf/mixin/mutable.rb', line 37

def load(filename, options = {})
  raise TypeError.new("#{self} is immutable") if immutable?

  Reader.open(filename, {:base_uri => filename}.merge(options)) do |reader|
    if options[:context]
      statements = []
      reader.each_statement do |statement|
        statement.context = options[:context]
        statements << statement
      end
      insert_statements(statements)
      statements.size
    else
      insert_statements(reader)
      nil
    end
  end
end

- (Boolean) mutable?

Returns true if self is mutable.

Returns:

  • (Boolean)

See Also:



18
19
20
# File 'lib/rdf/mixin/mutable.rb', line 18

def mutable?
  writable?
end

- (Mutable) update(*statements) Also known as: update!

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.

Parameters:

Returns:

Raises:

  • (TypeError)

    if self is immutable



94
95
96
97
98
99
100
101
102
103
# File 'lib/rdf/mixin/mutable.rb', line 94

def update(*statements)
  raise TypeError.new("#{self} is immutable") if immutable?

  statements.each do |statement|
    if (statement = Statement.from(statement))
      delete([statement.subject, statement.predicate, nil])
      insert(statement) if statement.has_object?
    end
  end
end