Module: FlatMap::BaseMapper::Persistence
- Included in:
- FlatMap::BaseMapper
- Defined in:
- lib/flat_map/base_mapper/persistence.rb
Overview
This module provides persistence functionality for mappers. Note that term of persistence here does not imply storing information in database or other place. This module provides methods for saving operation as a work flow of applying parameters to mapper and all of its mounted mappers in a right way, running callbacks, etc.
See Mapper::Targeting for a place where mapper targets are actually get persisted / updated.
In particular, validation and save methods are defined here. And the save method itself is defined as a callback. Also, Rails multiparam attributes extraction is defined within this module.
Instance Method Summary collapse
-
#apply(params) ⇒ Boolean
Write a passed set of
params. -
#errors ⇒ FlatMap::Errors
Overridden to use Errors instead of ActiveModel ones.
-
#save ⇒ Boolean
Try to save the target and send a
savemethod to all mounted mappers. -
#shallow_save ⇒ Boolean
Perform target save with callbacks call.
-
#valid? ⇒ Boolean
Return
trueif the mapper is valid, i.e. -
#write(params) ⇒ Hash
Extract the multiparam values from the passed
params.
Instance Method Details
#apply(params) ⇒ Boolean
Write a passed set of params. Then try to save the model if self passes validation. Saving is performed in a transaction.
21 22 23 24 |
# File 'lib/flat_map/base_mapper/persistence.rb', line 21 def apply(params) write(params) valid? && save end |
#errors ⇒ FlatMap::Errors
Overridden to use Errors instead of ActiveModel ones.
111 112 113 |
# File 'lib/flat_map/base_mapper/persistence.rb', line 111 def errors @errors ||= FlatMap::Errors.new(self) end |
#save ⇒ Boolean
Try to save the target and send a save method to all mounted mappers.
The order in which mappings are saved is important, since we save records with :validate => false option. Since Rails will perform auto-saving on associations (and it in its turn will try to save associated record with :validate => true option. To be more precise, with :validate => !autosave option, where autosave corresponds to that option of reflection, which is usually not specified, i.e. nil), we might come to a situation of saving a record with nil foreign key for belongs_to association, which will raise exception. Thus, we want to explicitly save records in order which will allow them to be saved. Return false if that chain of save calls returns true on any of its elements. Return true otherwise.
Saving is performed as a callback.
58 59 60 61 62 63 64 |
# File 'lib/flat_map/base_mapper/persistence.rb', line 58 def save before_res = save_mountings(before_save_mountings) target_res = self_mountings.map{ |mount| mount.shallow_save }.all? after_res = save_mountings(after_save_mountings) before_res && target_res && after_res end |
#shallow_save ⇒ Boolean
Perform target save with callbacks call
69 70 71 |
# File 'lib/flat_map/base_mapper/persistence.rb', line 69 def shallow_save run_callbacks(:save){ save_target } end |
#valid? ⇒ Boolean
Return true if the mapper is valid, i.e. if it is valid itself, and if all mounted mappers (traits and other mappers) are also valid.
Accepts any parameters, but doesn’t use them to be compatible with ActiveRecord calls.
90 91 92 93 94 95 96 |
# File 'lib/flat_map/base_mapper/persistence.rb', line 90 def valid?(*) res = trait_mountings.map(&:valid?).all? res = super && res # we do want to call super mounting_res = mapper_mountings.map(&:valid?).all? consolidate_errors! res && mounting_res end |
#write(params) ⇒ Hash
Extract the multiparam values from the passed params. Then use the resulting hash to assign values to the target. Assignment is performed by sending writer methods to self that correspond to keys in the resulting params hash.
33 34 35 36 37 38 39 |
# File 'lib/flat_map/base_mapper/persistence.rb', line 33 def write(params) extract_multiparams!(params) params.each do |name, value| self.send("#{name}=", value) end end |