192f6ca97ddf67c17524dd407614802ce56b9ba7
[booh] / lib / booh / Synchronizator.rb
1 #                         *  BOOH  *
2 #
3 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
4 #
5 # The acronyn sucks, however this is a tribute to Dragon Ball by
6 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
7 # Ball is named "Boo". But there was already a free software project
8 # called Boo, so this one will be it "Booh". Or whatever.
9 #
10 #
11 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
12 #
13 # This software may be freely redistributed under the terms of the GNU
14 # public license version 2.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 #
21 # Any method call to the wrapped object will be synchronized on a unique
22 # monitor. Recursive calls are possible.
23 #
24
25 require 'monitor'
26
27 class ObjectWrapper
28     def initialize(wrapped_object)
29         @wrapped_object = wrapped_object
30     end
31     def method_missing(id, *args, &block)
32         wrap(id, *args, &block)
33     end
34     def wrap(id, *args, &block)
35         if block.nil?
36             return @wrapped_object.__send__(id, *args)
37         else
38             return @wrapped_object.__send__(id, *args) { |*args2| block.call(*args2) }
39         end
40     end
41 end
42
43 class Synchronizator < ObjectWrapper
44     def initialize(wrapped_object)
45         super
46         @wrapped_object.extend(MonitorMixin)
47     end
48     def wrap(id, *args, &block)
49         @wrapped_object.synchronize {
50             super(id, *args, &block)
51         }
52     end
53 end