Module: Undoer::ClassMethods

Defined in:
lib/undoer.rb

Instance Method Summary collapse

Instance Method Details

#restore(options = {}, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/undoer.rb', line 24

def restore(options = {}, &block)
  target = options[:target]
  errors = options[:if_errors] || [StandardError].freeze
  cloned_target = target.respond_to?('new') ? target.clone : Marshal.load( Marshal.dump(target) )
  yield(target)
rescue *errors => e
  raise e, "not restoreable!" if NOT_SUPPORTED.select { |k| target.is_a?(k) }.any?
  target.deep_copy!(cloned_target)
end