lib/active_data/model/associations/base.rb in active_data-1.0.0 vs lib/active_data/model/associations/base.rb in active_data-1.1.0
- old
+ new
@@ -3,12 +3,13 @@
module Associations
class Base
attr_accessor :owner, :reflection
delegate :macro, :collection?, to: :reflection
- def initialize owner, reflection
- @owner, @reflection = owner, reflection
+ def initialize(owner, reflection)
+ @owner = owner
+ @reflection = reflection
@evar_loaded = owner.persisted?
reset
end
def reset
@@ -41,13 +42,28 @@
def apply_changes!
apply_changes or raise ActiveData::AssociationChangesNotApplied
end
- def transaction &block
- data = Marshal.load(Marshal.dump(read_source))
- block.call
+ def callback(name, object)
+ evaluator = reflection.options[name]
+ return true unless evaluator
+
+ if evaluator.is_a?(Proc)
+ if evaluator.arity == 1
+ owner.instance_exec(object, &evaluator)
+ else
+ evaluator.call(owner, object)
+ end
+ else
+ owner.send(evaluator, object)
+ end
+ end
+
+ def transaction
+ data = read_source.deep_dup
+ yield
rescue StandardError => e
write_source data
reload
raise e
end
@@ -60,10 +76,10 @@
def read_source
reflection.read_source owner
end
- def write_source value
+ def write_source(value)
reflection.write_source owner, value
end
def target_for_inspect
if value.length > 50