Module: ActiveModelPersistence::Persistence::ClassMethods
- Defined in:
- lib/active_model_persistence/persistence.rb
Overview
When this module is included in another class, ActiveSupport::Concern will make these class methods on that class.
Instance Method Summary collapse
-
#all ⇒ Array<Object>
Return all model objects that have been saved to the object store.
-
#count ⇒ Integer
(also: #size)
The number of model objects saved in the object store.
-
#create(attributes = nil, &block) ⇒ Object+
Creates a new model object in to the object store and returns it.
-
#create!(attributes = nil, &block) ⇒ Object+
Creates a new model object in to the object store.
-
#delete_all ⇒ void
Removes all model objects from the object store.
-
#destroy_all ⇒ void
Removes all model objects from the object store.
-
#object_array ⇒ Array<Object>
private
All saved model objects are stored in this array (this is the object store).
Instance Method Details
#all ⇒ Array<Object>
Return all model objects that have been saved to the object store
160 161 162 |
# File 'lib/active_model_persistence/persistence.rb', line 160 def all object_array.each end |
#count ⇒ Integer Also known as: size
The number of model objects saved in the object store
176 177 178 |
# File 'lib/active_model_persistence/persistence.rb', line 176 def count object_array.size end |
#create(attributes = nil, &block) ⇒ Object+
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.
94 95 96 97 98 99 100 |
# 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) ⇒ Object+
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!`.
138 139 140 141 142 143 144 |
# 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 ⇒ void
This method returns an undefined value.
Removes all model objects from the object store
Each saved model object’s ‘#destroy` method is NOT called.
218 219 220 221 222 |
# File 'lib/active_model_persistence/persistence.rb', line 218 def delete_all @object_array = [] indexes.values.each(&:remove_all) nil end |
#destroy_all ⇒ void
This method returns an undefined value.
Removes all model objects from the object store
Each saved model object’s ‘#destroy` method is called.
198 199 200 |
# File 'lib/active_model_persistence/persistence.rb', line 198 def destroy_all object_array.first.destroy while object_array.size.positive? end |
#object_array ⇒ Array<Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All saved model objects are stored in this array (this is the object store)
232 233 234 |
# File 'lib/active_model_persistence/persistence.rb', line 232 def object_array @object_array ||= [] end |