Class: ActiveFedora::SparqlInsert

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/sparql_insert.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(changes, subject = ::RDF::URI.new(nil)) ⇒ SparqlInsert


6
7
8
9
# File 'lib/active_fedora/sparql_insert.rb', line 6

def initialize(changes, subject = ::RDF::URI.new(nil))
  @changes = changes
  @subject = subject
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes


4
5
6
# File 'lib/active_fedora/sparql_insert.rb', line 4

def changes
  @changes
end

#subjectObject (readonly)

Returns the value of attribute subject


4
5
6
# File 'lib/active_fedora/sparql_insert.rb', line 4

def subject
  @subject
end

Instance Method Details

#buildObject


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_fedora/sparql_insert.rb', line 17

def build
  query = deletes(subject).join
  query += "INSERT { \n"
  query +=
    changes.map do |_, result|
      result.map do |statement|
        ::RDF::Query::Pattern.new(subject: subject, predicate: statement.predicate, object: statement.object).to_s
      end.join("\n")
    end.join("\n")

  query += "\n}\n WHERE { }"
  query
end

#execute(uri) ⇒ Object


11
12
13
14
15
# File 'lib/active_fedora/sparql_insert.rb', line 11

def execute(uri)
  result = ActiveFedora.fedora.connection.patch(uri, build, "Content-Type" => "application/sparql-update")
  return true if result.status == 204
  raise "Problem updating #{result.status} #{result.body}"
end