module ActiveModelPersistence::Persistence::ClassMethods

When this module is included in another class, ActiveSupport::Concern will make these class methods on that class.

Public Instance Methods

all() click to toggle source

Return all model objects that have been saved to the object store

@example

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2
ModelExample.all.map(&:id) #=> [1, 2]
ModelExample.all.map(&:name) #=> ['James', 'Frank']

@return [Array<Object>] the model objects in the object store

# File lib/active_model_persistence/persistence.rb, line 160
def all
  object_array.each
end
count() click to toggle source

The number of model objects saved in the object store

@example

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2

@return [Integer] the number of model objects in the object store

# File lib/active_model_persistence/persistence.rb, line 176
def count
  object_array.size
end
Also aliased as: size
create(attributes = nil, &block) click to toggle source

Creates a new model object in to the object store and returns it

Create a new model object passing ‘attributes` and `block` to `.new` and then calls `#save`.

The new model object is returned even if it could not be saved to the object store.

@param attributes [Hash, Array<Hash>] attributes

The attributes to set on the model object. These are passed to the model's `.new` method.

Multiple model objects can be created by passing an array of attribute Hashes.

@param block [Proc] options

The block to pass to the model's `.new` method.

@example

m = ModelExample.new(id: 1, name: 'James')
m.id #=> 1
m.name #=> 'James'

@example Multiple model objects can be created

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
objects = ModelExample.create(array_of_attributes)
objects.class #=> Array
objects.size #=> 2
objects.first.id #=> 1
objects.map(&:name) #=> ['James', 'Frank']

@return [Object, Array<Object>] the model object or array of model objects created

# File lib/active_model_persistence/persistence.rb, line 94
def create(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    new(attributes, &block).tap(&:save)
  end
end
create!(attributes = nil, &block) click to toggle source

Creates a new model object in to the object store

Raises an error if the object could not be created.

Create a new model object passing ‘attributes` and `block` to `.new` and then calls `#save!`.

@param attributes [Hash, Array<Hash>] attributes

The attributes to set on the model object. These are passed to the model's `.new` method.

Multiple model objects can be created by passing an array of attribute Hashes.

@param block [Proc] options

The block to pass to the model's `.new` method.

@example

m = ModelExample.new(id: 1, name: 'James')
m.id #=> 1
m.name #=> 'James'

@example Multiple model objects can be created

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
objects = ModelExample.create(array_of_attributes)
objects.class #=> Array
objects.size #=> 2
objects.first.id #=> 1
objects.map(&:name) #=> ['James', 'Frank']

@return [Object, Array<Object>] the model object or array of model objects created

@raise [ModelError] if the model object could not be created

# File lib/active_model_persistence/persistence.rb, line 138
def create!(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create!(attr, &block) }
  else
    new(attributes, &block).tap(&:save!)
  end
end
delete_all() click to toggle source

Removes all model objects from the object store

Each saved model object’s ‘#destroy` method is NOT called.

@example

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2
ModelExample.destroy_all
ModelExample.all.count #=> 0

@return [void]

# File lib/active_model_persistence/persistence.rb, line 218
def delete_all
  @object_array = []
  indexes.values.each(&:remove_all)
  nil
end
destroy_all() click to toggle source

Removes all model objects from the object store

Each saved model object’s ‘#destroy` method is called.

@example

array_of_attributes = [
  { id: 1, name: 'James' },
  { id: 2, name: 'Frank' }
]
ModelExample.create(array_of_attributes)
ModelExample.all.count #=> 2
ModelExample.destroy_all
ModelExample.all.count #=> 0

@return [void]

# File lib/active_model_persistence/persistence.rb, line 198
def destroy_all
  object_array.first.destroy while object_array.size.positive?
end
object_array() click to toggle source

All saved model objects are stored in this array (this is the object store)

@return [Array<Object>] the model objects in the object store

@api private

# File lib/active_model_persistence/persistence.rb, line 232
def object_array
  @object_array ||= []
end
size()
Alias for: count