aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/setter_trap.rb
blob: 437fbdad32e60f0b8a04d8df5a0007e2ccd5c58e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require 'abstract_unit'

class SetterTrap < ActiveSupport::BasicObject
  class << self
    def rollback_sets(obj)
      trapped = new(obj)
      yield(trapped).tap { trapped.rollback_sets }
    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