module Tripod::Persistence

This module defines behaviour for persisting to the database.

Public Instance Methods

destroy(opts={}) click to toggle source
# 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={}) click to toggle source

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.

@example Save the resource.

resource.save

@return [ true, false ] True is success, false if not.

# 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={}) click to toggle source

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.

@example Save the resource.

resource.save

@raise [Tripod::Errors::Validations] if invalid

@return [ true ] True is success.

# 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={}) click to toggle source
# File lib/tripod/persistence.rb, line 152
def update_attribute(name, value, opts={})
  write_attribute(name, value)
  save(opts)
end
update_attributes(attributes, opts={}) click to toggle source
# 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