Module: FlatMap::ModelMapper::Persistence

Extended by:
ActiveSupport::Concern
Included in:
FlatMap::ModelMapper
Defined in:
lib/flat_map/model_mapper/persistence.rb

Overview

This module enhances and modifies original FlatMap::OpenMapper::Persistence functionality for ActiveRecord models as targets.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

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.



69
70
71
72
73
74
75
76
77
# File 'lib/flat_map/model_mapper/persistence.rb', line 69

def apply(params)
  write(params)
  res = if valid?
    ActiveRecord::Base.transaction do
      save
    end
  end
  !!res
end

#idFixnum?

Delegate #id to target, if possible.



97
98
99
# File 'lib/flat_map/model_mapper/persistence.rb', line 97

def id
  target.id if target.respond_to?(:id)
end

#persisted?Boolean

Delegate persistence to target.



90
91
92
# File 'lib/flat_map/model_mapper/persistence.rb', line 90

def persisted?
  target.respond_to?(:persisted?) ? target.persisted? : false
end

#save_targetBoolean

Save target



82
83
84
85
# File 'lib/flat_map/model_mapper/persistence.rb', line 82

def save_target
  return true if owned?
  target.respond_to?(:save) ? target.save(:validate => false) : true
end

#to_keyString

Delegate to the target’s #to_key method.



60
61
62
# File 'lib/flat_map/model_mapper/persistence.rb', line 60

def to_key
  target.to_key
end