Module: Tripod::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/tripod/persistence.rb

Overview

This module defines behaviour for persisting to the database.

Defined Under Namespace

Modules: ClassMethods Classes: Transaction

Instance Method Summary collapse

Instance Method Details

#destroy(opts = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tripod/persistence.rb', line 128

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.

Examples:

Save the resource.

resource.save

Returns:

  • (true, false)

    True is success, false if not.



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

      query = "
        DELETE {GRAPH ?g {<#{@uri.to_s}> ?p ?o}} WHERE {GRAPH ?g {<#{@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.
      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.

Examples:

Save the resource.

resource.save

Returns:

  • (true)

    True is success.

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tripod/persistence.rb', line 111

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) ⇒ Object



151
152
153
154
# File 'lib/tripod/persistence.rb', line 151

def update_attribute(name, value)
  write_attribute(name, value)
  save
end

#update_attributes(attributes = {}) ⇒ Object



156
157
158
159
160
161
# File 'lib/tripod/persistence.rb', line 156

def update_attributes(attributes={})
  attributes.each_pair do |name, value|
    send "#{name}=", value
  end
  save
end