module Flatter::Mapper::Persistence

Public Instance Methods

apply(params) click to toggle source
# File lib/flatter/mapper/persistence.rb, line 11
def apply(params)
  write(params)
  valid? && save
end
errors() click to toggle source
Calls superclass method
# File lib/flatter/mapper/persistence.rb, line 79
def errors
  trait? ? mounter.errors : super
end
run_save!() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 32
def run_save!
  with_callbacks(:save){ save_target }
end
run_validations!() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 22
def run_validations!
  errors.clear
  with_callbacks(:validate)
end
save() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 27
def save
  results = mappers_chain(:save).map(&:run_save!)
  results.all?
end
valid?(*) click to toggle source
# File lib/flatter/mapper/persistence.rb, line 16
def valid?(*)
  mappers_chain(:validate).each(&:run_validations!)
  consolidate_errors!
  errors.empty?
end
with_callbacks(type, chain = self_mountings) { || ... } click to toggle source
# File lib/flatter/mapper/persistence.rb, line 46
def with_callbacks(type, chain = self_mountings, &block)
  current = chain.shift
  current.run_callbacks(type) do
    chain.present? ? with_callbacks(type, chain, &block) : (yield if block_given?)
  end
end

Protected Instance Methods

prefix() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 74
def prefix
  nil
end
save_target() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 41
def save_target
  target.respond_to?(:save) ? target.save : true
end

Private Instance Methods

consolidate_errors!() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 63
def consolidate_errors!
  root_mountings.each do |mounting|
    prefix = mounting.prefix
    mounting.errors.to_hash.each do |name, errs|
      error_key = [prefix, name].compact.join('.')
      errors.messages.merge!(error_key.to_sym => errs){ |key, old, new| old + new }
    end
  end
end
mappers_chain(context) click to toggle source
# File lib/flatter/mapper/persistence.rb, line 36
def mappers_chain(context)
  root_mountings.dup.unshift(self)
end
root_mountings() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 53
def root_mountings
  inner_mountings.reject(&:trait?)
end
self_mountings() click to toggle source
# File lib/flatter/mapper/persistence.rb, line 58
def self_mountings
  local_mountings.select(&:trait?).unshift(self).reverse
end