Module: Tripod::Persistence
Overview
This module defines behaviour for persisting to the database.
Defined Under Namespace
Modules: ClassMethods Classes: Transaction
Instance Method Summary collapse
- #destroy(opts = {}) ⇒ Object
-
#save(opts = {}) ⇒ true, false
Save the resource.
-
#save!(opts = {}) ⇒ true
Save the resource, and raise an exception if it fails.
- #update_attribute(name, value, opts = {}) ⇒ Object
- #update_attributes(attributes, opts = {}) ⇒ Object
Instance Method Details
#destroy(opts = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tripod/persistence.rb', line 129 def destroy(opts={}) run_callbacks :destroy do transaction = Tripod::Persistence::Transaction.get_transaction(opts[:transaction]) query = " # delete from default graph: DELETE {<#{@uri.to_s}> ?p ?o} WHERE {<#{@uri.to_s}> ?p ?o}; # delete from named graphs: DELETE {GRAPH ?g {<#{@uri.to_s}> ?p ?o}} WHERE {GRAPH ?g {<#{@uri.to_s}> ?p ?o}}; " if transaction transaction.query ||= "" transaction.query += query else Tripod::SparqlClient::Update::update(query) end @destroyed = true true end end |
#save(opts = {}) ⇒ true, false
Save the resource. Note: regardless of whether it’s a new_record or not, we always make the db match the contents of this resource’s statements.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/tripod/persistence.rb', line 69 def save(opts={}) run_callbacks :save do raise Tripod::Errors::GraphUriNotSet.new() unless @graph_uri transaction = Tripod::Persistence::Transaction.get_transaction(opts[:transaction]) if self.valid? graph_selector = self.graph_uri.present? ? "<#{graph_uri.to_s}>" : "?g" query = " DELETE {GRAPH #{graph_selector} {<#{@uri.to_s}> ?p ?o}} WHERE {GRAPH #{graph_selector} {<#{@uri.to_s}> ?p ?o}}; INSERT DATA { GRAPH <#{@graph_uri}> { #{ @repository.dump(:ntriples) } } }; " if transaction transaction.query ||= "" transaction.query += query else Tripod::SparqlClient::Update::update(query) end @new_record = false # if running in a trans, just assume it worked. If the query is dodgy, it will throw an exception later. post_persist true else false end end end |
#save!(opts = {}) ⇒ true
Save the resource, and raise an exception if it fails. Note: As with save(), regardless of whether it’s a new_record or not, we always make the db match the contents of this resource’s statements.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tripod/persistence.rb', line 112 def save!(opts={}) # try to save unless self.save(opts) # if we get in here, save failed. # abort the transaction transaction = Tripod::Persistence::Transaction.get_transaction(opts[:transaction]) transaction.abort() if transaction self.class.fail_validate!(self) # throw an exception # TODO: similar stuff for callbacks? end return true end |
#update_attribute(name, value, opts = {}) ⇒ Object
152 153 154 155 |
# File 'lib/tripod/persistence.rb', line 152 def update_attribute(name, value, opts={}) write_attribute(name, value) save(opts) end |
#update_attributes(attributes, opts = {}) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/tripod/persistence.rb', line 157 def update_attributes(attributes, opts={}) attributes.each_pair do |name, value| send "#{name}=", value end save(opts) end |