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
Some more cleanup. Repeat after me, no code is better than no code
arunthampi (author)
Sun Jun 08 16:33:35 -0700 2008
commit  46ef7403b558e2cbbce891ccadb73d2d9d64558b
tree    02e5c05d62d7c335e760cf221e8ba2193557ae4b
parent  41e7e198db3befc189636bb0afc3e0397455fde0
...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
 
 
 
 
 
 
 
 
 
 
39
40
 
41
42
43
...
45
46
47
48
 
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
64
65
66
...
25
26
27
 
28
29
30
31
32
 
 
 
 
 
33
34
35
36
37
38
39
40
41
42
43
44
 
45
46
47
48
...
50
51
52
 
53
54
55
56
 
 
 
 
 
 
57
58
 
 
 
 
59
60
61
62
0
@@ -25,19 +25,24 @@ module ActiveCouch
0
       klass_atts = self.class.attributes
0
       klass_assocs = self.class.associations
0
       klass_callbacks = self.class.callbacks
0
-
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
         self.instance_eval "def #{k}; @#{k}; end"
0
       end
0
-
0
- klass_atts.each_key do |k|
0
- @attributes[k] = klass_atts[k]
0
- self.instance_eval "def #{k}; attributes[:#{k}]; end"
0
- self.instance_eval "def #{k}=(val); attributes[:#{k}] = val; end"
0
+ # First, initialize all the attributes
0
+ klass_atts.each_key do |property|
0
+ @attributes[property] = klass_atts[property]
0
+ self.instance_eval "def #{property}; attributes[:#{property}]; end"
0
+ self.instance_eval "def #{property}=(val); attributes[:#{property}] = val; end"
0
+ # These are special attributes which need aliases (for now, it's _id and _rev)
0
+ if property.to_s[0,1] == '_'
0
+ aliased_prop = property.to_s.slice(1, property.to_s.size)
0
+ self.instance_eval "def #{aliased_prop}; self.#{property}; end"
0
+ self.instance_eval "def #{aliased_prop}=(val); self.#{property}=(val); end"
0
+ end
0
       end
0
-
0
+ # Then, initialize all the associations
0
       klass_assocs.each_key do |k|
0
         @associations[k] = klass_assocs[k]
0
         self.instance_eval "def #{k}; @#{k} ||= []; end"
0
@@ -45,22 +50,13 @@ module ActiveCouch
0
         # to the object instantiated from the class
0
         self.instance_eval "def add_#{k.singularize}(val); @#{k} = #{k} << val; end"
0
       end
0
-
0
+ # Finally, all the calbacks
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}; self._#{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
- # Now you can do stuff like
0
- # Person.new do |p|
0
- # p.name = 'McLovin'
0
- # end
0
+ # Handle the block, which can also be used to initialize the object
0
       yield self if block_given?
0
     end
0
 

Comments

    No one has commented yet.