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

Returns a new instance of SparqlInsert.



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

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

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



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

def changes
  @changes
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#buildObject



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

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

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

#execute(uri) ⇒ Object



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

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