2008-05-18 06:22:34 +02:00
|
|
|
class SetterTrap < ActiveSupport::BasicObject
|
2007-12-21 08:48:59 +01:00
|
|
|
class << self
|
|
|
|
def rollback_sets(obj)
|
2009-02-28 02:23:00 +01:00
|
|
|
trapped = new(obj)
|
|
|
|
yield(trapped).tap { trapped.rollback_sets }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(obj)
|
|
|
|
@cache = {}
|
|
|
|
@obj = obj
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(method)
|
|
|
|
@obj.respond_to?(method)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args, &proc)
|
|
|
|
@cache[method] ||= @obj.send($`) if method.to_s =~ /=$/
|
|
|
|
@obj.send method, *args, &proc
|
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_sets
|
|
|
|
@cache.each { |k, v| @obj.send k, v }
|
|
|
|
end
|
|
|
|
end
|