public
Fork of chuyeow/activecouch
Description: ActiveCouch is a simple, convenient, Ruby-idiomatic wrapper for CouchDB
Homepage: http://github.com/arunthampi/activecouch
Clone URL: git://github.com/arunthampi/activecouch.git
- Working commit for callbacks
arunthampi (author)
Tue Jan 22 03:04:24 -0800 2008
commit  550809c9b45371a8636872f7c43c3404b0445b99
tree    fb31579c76a66e505500b9bd0ea152d35bd1450f
parent  8c932dea22371552f3b04f495ece656430f740ec
...
10
11
12
13
14
 
 
 
 
 
 
15
...
10
11
12
 
13
14
15
16
17
18
19
20
0
@@ -10,4 +10,9 @@ require 'active_couch/support'
0
 require 'active_couch/errors'
0
 require 'active_couch/base'
0
 require 'active_couch/connection'
0
-require 'active_couch/migrations'
0
\ No newline at end of file
0
+require 'active_couch/migrations'
0
+require 'active_couch/callbacks'
0
+
0
+#ActiveCouch::Base.class_eval do
0
+# include ActiveCouch::Callbacks
0
+#end
0
\ No newline at end of file
...
1
2
3
 
4
5
 
6
7
8
 
 
9
10
11
...
18
19
20
21
22
23
24
25
26
27
28
...
30
31
32
 
 
 
 
 
 
 
 
 
 
33
34
35
...
357
358
359
 
 
 
 
360
361
362
363
 
364
365
366
...
1
2
 
3
4
 
5
6
7
 
8
9
10
11
12
...
19
20
21
 
 
 
 
 
22
23
24
...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
0
@@ -1,11 +1,12 @@
0
 module ActiveCouch
0
   class Base
0
- SPECIAL_MEMBERS = %w(attributes associations connection)
0
+ SPECIAL_MEMBERS = %w(attributes associations connection callbacks)
0
     DEFAULT_ATTRIBUTES = %w(id rev)
0
-
0
+
0
     def initialize(params = {})
0
       # Object instance variable
0
- @attributes, @associations, @connection, klass_atts, klass_assocs = {}, {}, self.class.connection, self.class.attributes, self.class.associations
0
+ @attributes = {}; @associations = {}; @callbacks = Hash.new; @connection = self.class.connection
0
+ klass_atts = self.class.attributes; klass_assocs = self.class.associations; klass_callbacks = self.class.callbacks
0
       # ActiveCouch::Connection object will be readable in every
0
       # object instantiated from a subclass of ActiveCouch::Base
0
       SPECIAL_MEMBERS.each do |k|
0
@@ -18,11 +19,6 @@ module ActiveCouch
0
         self.instance_eval "def #{k}=(val); attributes[:#{k}].value = val; end"
0
       end
0
       
0
- DEFAULT_ATTRIBUTES.each do |x|
0
- self.instance_eval "def #{x}; _#{x}; end"
0
- self.instance_eval "def #{x}=(val); self._#{x}=(val); end"
0
- end
0
-
0
       klass_assocs.each_key do |k|
0
         @associations[k] = HasManyAssociation.new(klass_assocs[k].name, :class => klass_assocs[k].klass)
0
         self.instance_eval "def #{k}; associations[:#{k}].container; end"
0
@@ -30,6 +26,16 @@ module ActiveCouch
0
         # from the class
0
         self.instance_eval "def add_#{Inflector.singularize(k)}(val); associations[:#{k}].push(val); end"
0
       end
0
+
0
+ klass_callbacks.each_key do |k|
0
+ @callbacks[k] = klass_callbacks[k].dup
0
+ end
0
+
0
+ DEFAULT_ATTRIBUTES.each do |x|
0
+ self.instance_eval "def #{x}; _#{x}; end"
0
+ self.instance_eval "def #{x}=(val); self._#{x}=(val); end"
0
+ end
0
+
0
       # Set any instance variables if any, which are present in the params hash
0
       from_hash(params)
0
     end
0
@@ -357,10 +363,15 @@ module ActiveCouch
0
       end
0
 
0
       def inherited(subklass)
0
+ subklass.class_eval do
0
+ include ActiveCouch::Callbacks
0
+ end
0
+
0
         # TODO: Need a cleaner way to do this
0
         subklass.instance_variable_set "@attributes", { :_id => Attribute.new(:_id, :with_default_value => nil),
0
                                                         :_rev => Attribute.new(:_rev, :with_default_value => nil) }
0
         subklass.instance_variable_set "@associations", {}
0
+ subklass.instance_variable_set "@callbacks", Hash.new([])
0
         subklass.instance_variable_set "@connections", nil
0
                                                         
0
         SPECIAL_MEMBERS.each do |k|
...
46
47
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
50
51
52
...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
92
93
0
@@ -46,6 +46,47 @@ module ActiveCouch
0
       parent_name = name.split('::')[0..-2] * '::'
0
       parent_name.empty? ? Object : Inflector.constantize(parent_name)
0
     end
0
+
0
+ # Encapsulates the common pattern of:
0
+ #
0
+ # alias_method :foo_without_feature, :foo
0
+ # alias_method :foo, :foo_with_feature
0
+ #
0
+ # With this, you simply do:
0
+ #
0
+ # alias_method_chain :foo, :feature
0
+ #
0
+ # And both aliases are set up for you.
0
+ #
0
+ # Query and bang methods (foo?, foo!) keep the same punctuation:
0
+ #
0
+ # alias_method_chain :foo?, :feature
0
+ #
0
+ # is equivalent to
0
+ #
0
+ # alias_method :foo_without_feature?, :foo?
0
+ # alias_method :foo?, :foo_with_feature?
0
+ #
0
+ # so you can safely chain foo, foo?, and foo! with the same feature.
0
+ def alias_method_chain(target, feature)
0
+ # Strip out punctuation on predicates or bang methods since
0
+ # e.g. target?_without_feature is not a valid method name.
0
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
0
+ yield(aliased_target, punctuation) if block_given?
0
+
0
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
0
+
0
+ alias_method without_method, target
0
+ alias_method target, with_method
0
+
0
+ case
0
+ when public_method_defined?(without_method)
0
+ public target
0
+ when protected_method_defined?(without_method)
0
+ protected target
0
+ when private_method_defined?(without_method)
0
+ private target
0
+ end
0
+ end
0
   end
0
-
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.