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

#writable?

Methods included from Readable

#readable?

Instance Method Details

#<<(data) ⇒ Mutable

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

#clearMutable 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

#delete(*statements) ⇒ Mutable 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

#immutable?Boolean

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

#insert(*statements) ⇒ Mutable

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 = {}) ⇒ void Also known as: load!

This method returns an undefined value.

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

Parameters:

  • filename (String, #to_s)
  • options (Hash{Symbol => Object}) (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

#mutable?Boolean

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

#update(*statements) ⇒ Mutable 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