Module: Spira::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/spira/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#countObject

The number of RDF::Statements this projection has.


337
338
339
# File 'lib/spira/persistence.rb', line 337

def count
  each.count
end

#destroy(*args) ⇒ Object


293
294
295
296
297
# File 'lib/spira/persistence.rb', line 293

def destroy(*args)
  run_callbacks :destroy do
    destroy_model_data(*args)
  end
end

#destroy!(*args) ⇒ Object


299
300
301
# File 'lib/spira/persistence.rb', line 299

def destroy!(*args)
  destroy(*args) || raise(RecordNotSaved)
end

#destroyed?Boolean

Returns:

  • (Boolean)

274
275
276
# File 'lib/spira/persistence.rb', line 274

def destroyed?
  @destroyed
end

#eachObject

Enumerate each RDF statement that makes up this projection. This makes each instance an RDF::Enumerable, with all of the nifty benefits thereof. See http://rdf.rubyforge.org/RDF/Enumerable.html for information on arguments.


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/spira/persistence.rb', line 310

def each
  if block_given?
    self.class.properties.each do |name, property|
      if value = read_attribute(name)
        if self.class.reflect_on_association(name)
          value.each do |val|
            node = build_rdf_value(val, property[:type])
            yield RDF::Statement.new(subject, property[:predicate], node) if valid_object?(node)
          end
        else
          node = build_rdf_value(value, property[:type])
          yield RDF::Statement.new(subject, property[:predicate], node) if valid_object?(node)
        end
      end
    end
    self.class.types.each do |t|
      yield RDF::Statement.new(subject, RDF.type, t)
    end
  else
    enum_for(:each)
  end
end

#new_record?Boolean

A resource is considered to be new if the repository does not have statements where subject == resource type

Returns:

  • (Boolean)

270
271
272
# File 'lib/spira/persistence.rb', line 270

def new_record?
  !self.class.repository.has_subject?(subject)
end

#persisted?Boolean

Returns:

  • (Boolean)

278
279
280
281
282
283
# File 'lib/spira/persistence.rb', line 278

def persisted?
  # FIXME: an object should be considered persisted
  # when its attributes (and their exact values) are all available in the storage.
  # This should check for !(changed? || new_record? || destroyed?) actually.
  !(new_record? || destroyed?)
end

#reload(props = {}) ⇒ Object

Reload all attributes for this instance. This resource will block if the underlying repository blocks the next time it accesses attributes.

NB: "props" argument is ignored, it is handled in Base


368
369
370
371
372
373
374
375
376
377
# File 'lib/spira/persistence.rb', line 368

def reload(props = {})
  sts = self.class.repository.query(:subject => subject)
  self.class.properties.each do |name, options|
    name = name.to_s
    if sts
      objects = sts.select { |s| s.predicate == options[:predicate] }
      attributes[name] = retrieve_attribute(name, options, objects)
    end
  end
end

#saveObject


285
286
287
# File 'lib/spira/persistence.rb', line 285

def save(*)
  create_or_update
end

#save!Object


289
290
291
# File 'lib/spira/persistence.rb', line 289

def save!(*)
  create_or_update || raise(RecordNotSaved)
end

#update_attributes(properties, options = {}) ⇒ self

Update multiple attributes of this repository.

Examples:

Update multiple attributes

person.update_attributes(:name => 'test', :age => 10)
#=> person
person.name
#=> 'test'
person.age
#=> 10
person.dirty?
#=> true

Parameters:

  • properties (Hash{Symbol => Any})
  • options (Hash{Symbol => Any}) (defaults to: {})

Returns:

  • (self)

356
357
358
359
# File 'lib/spira/persistence.rb', line 356

def update_attributes(properties, options = {})
  assign_attributes properties
  save options
end