public
Description: Plug-and-play data persistence created for small Ruby web applications.
Homepage: http://stone.rubyforge.org
Clone URL: git://github.com/ndemonner/stone.git
Search Repo:
Finished most associations and all callbacks.
ndemonner (author)
Fri Apr 11 15:14:30 -0700 2008
commit  1e8118823d58b5ac585857f49e2119c94075fdd0
tree    345e60fc29f901043fcd9d865155d08d3cf8040b
parent  f3e581e96e3702dc8a94b419b3c357849835b9a5
...
6
7
8
9
10
11
12
...
6
7
8
 
9
10
11
0
@@ -6,7 +6,6 @@ require 'english/inflect'
0
 require 'facets'
0
 require 'yaml'
0
 require 'fastercsv'
0
-require 'lib/stone/associations_map'
0
 require 'lib/stone/data_store'
0
 require 'lib/stone/callbacks'
0
 require 'lib/stone/resource'
...
19
20
21
22
23
24
25
...
19
20
21
 
22
23
24
0
@@ -19,7 +19,6 @@ module Stone
0
     # +klass+:: The class to be registered
0
     def register_klass(klass)
0
       self[klass.to_s.make_key] = {}
0
-
0
       CALLBACKS.each do |cb_sym|
0
         self[klass.to_s.make_key][cb_sym] = []
0
       end
...
28
29
30
31
32
33
34
...
43
44
45
46
 
 
 
 
 
 
 
 
47
48
49
 
 
 
50
51
 
 
 
52
53
54
...
72
73
74
75
76
77
78
79
80
81
 
82
83
84
...
111
112
113
 
 
 
 
114
 
 
 
 
 
115
116
117
 
118
119
 
 
 
120
 
 
 
 
 
 
121
122
 
123
124
125
...
141
142
143
 
 
 
144
145
146
...
153
154
155
156
157
158
159
...
28
29
30
 
31
32
33
...
42
43
44
 
45
46
47
48
49
50
51
52
53
54
 
55
56
57
58
 
59
60
61
62
63
64
...
82
83
84
 
 
 
 
85
86
 
87
88
89
90
...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
...
167
168
169
170
171
172
173
174
175
...
182
183
184
 
185
186
187
0
@@ -28,7 +28,6 @@ module Stone
0
         @@callbacks ||= Callbacks.new
0
         @@callbacks.register_klass(base)
0
         
0
- @@map ||= AssociationsMap.new
0
         @@store ||= DataStore.new
0
         unless @@store.resources.include? rsrc_sym
0
           @@store.resources[rsrc_sym] = DataStore.load_data(rsrc_sym)
0
@@ -43,12 +42,23 @@ module Stone
0
     # === Parameters
0
     # +name+<String>::
0
     #
0
- def field(name, klass, *args)
0
+ def field(name, klass, arg = nil)
0
+
0
+ if arg && arg[:unique] == true
0
+ unique = true
0
+ else
0
+ unique = false
0
+ end
0
+
0
       klass_sym = self.to_s.make_key
0
       unless @@fields[klass_sym]
0
- @@fields[klass_sym] = [{:name => name, :klass => klass}]
0
+ @@fields[klass_sym] = [{:name => name,
0
+ :klass => klass,
0
+ :unique => unique}]
0
       else
0
- @@fields[klass_sym] << {:name => name, :klass => klass}
0
+ @@fields[klass_sym] << {:name => name,
0
+ :klass => klass,
0
+ :unique => unique}
0
       end
0
       
0
       name = name.to_s
0
@@ -72,13 +82,9 @@ module Stone
0
       @id
0
     end
0
     
0
- # hmmmm, this is a tricky one...
0
- def validates_uniqueness_of(field)s
0
- end
0
-
0
     # Registers the given method with the current instance of Callbacks. Upon
0
     # activation (in this case, right before Resource.save is executed), the
0
- # +meth+ given is called against the object begin, in this case, saved.
0
+ # +meth+ given is called against the object being, in this case, saved.
0
     # === Parameters
0
     # +meth+:: The method to be registered
0
     def before_save(meth)
0
@@ -111,15 +117,35 @@ module Stone
0
       @@callbacks.register(:after_delete, meth, self)
0
     end
0
     
0
+ # Registers a has_many relationship for +resource+
0
+ # === Parameters
0
+ # +resource+::
0
+ # the resource of which this class has many
0
     def has_many(resource, *args)
0
+ self.class_eval <<-EOS, __FILE__, __LINE__
0
+ def #{resource.to_s}
0
+ #{resource.to_s.singularize.titlecase}.all("#{self.to_s.downcase}_id == "+self.id.to_s)
0
+ end
0
+ EOS
0
     end
0
     
0
     def has_one(resource, *args)
0
+ field "#{resource.to_s}_id".to_sym, Fixnum
0
     end
0
     
0
+ # Registers a belongs_to relationship for resource
0
+ # === Parameters
0
+ # +resource+ :: The resource to which this class belongs
0
     def belongs_to(resource, *args)
0
+ field "#{resource.to_s}_id".to_sym, Fixnum
0
+ self.class_eval <<-EOS, __FILE__, __LINE__
0
+ def #{resource.to_s}
0
+ #{resource.to_s.titlecase}[self.#{resource.to_s}_id]
0
+ end
0
+ EOS
0
     end
0
     
0
+ # TODO: implement this
0
     def has_and_belongs_to_many(resource, *args)
0
     end
0
     
0
@@ -141,6 +167,9 @@ module Stone
0
         unless self.send(field[:name]).class == field[:klass] || self.send(field[:name]) == nil
0
           return false
0
         end
0
+ if field[:unique] == true
0
+ return false if self.class.first("#{field[:name]} == '#{self.send(field[:name])}'")
0
+ end
0
       end
0
       true
0
     end
0
@@ -153,7 +182,6 @@ module Stone
0
     # === Example
0
     # <tt>Author.first("name ~= /Nick/i && email.include?('gmail.com')")</tt>
0
     def first(conditions = nil)
0
- objs = []
0
       unless conditions
0
         return @@store.resources[self.to_s.make_key].first[1]
0
       else
...
1
2
3
 
 
4
5
6
7
 
8
9
10
11
12
 
 
...
1
 
 
2
3
4
5
6
 
7
8
9
10
 
 
11
12
0
@@ -1,12 +1,12 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-10T13:54:55-07:00
0
-email: nick@bzzybee.com
0
+created_at: 2008-04-11T15:13:33-07:00
0
+email: nick@cladby.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 33
0
+favorite_number: 46
0
 id: 1
0
 name: Nick DeMonner
0
 times_validated_hash:
0
- Author/Validatable::ValidatesPresenceOf/email: 2
0
- Author/Validatable::ValidatesPresenceOf/name: 2
0
+ Author/Validatable::ValidatesPresenceOf/email: 1
0
+ Author/Validatable::ValidatesPresenceOf/name: 1
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-10T13:54:55-07:00
0
+created_at: 2008-04-11T15:13:33-07:00
0
 email: heyo@something.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 38
0
+favorite_number: 17
0
 id: 2
0
 name: Mike McMichaels
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-10T13:54:55-07:00
0
+created_at: 2008-04-11T15:13:33-07:00
0
 email: weyo@something.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 27
0
+favorite_number: 37
0
 id: 3
0
 name: Mary Poppins
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-10T13:54:55-07:00
0
+created_at: 2008-04-11T15:13:33-07:00
0
 email: bob@gmail.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 18
0
+favorite_number: 39
0
 id: 4
0
 name: Bob Hicklesby
0
 times_validated_hash:
...
1
2
 
3
4
5
6
7
 
8
9
10
...
1
 
2
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 --- !ruby/object:Author
0
-created_at: 2008-04-10T13:54:55-07:00
0
+created_at: 2008-04-11T15:13:33-07:00
0
 email: chariot_guy@hotmail.com
0
 errors: !ruby/object:Validatable::Errors
0
   errors: {}
0
 
0
-favorite_number: 31
0
+favorite_number: 6
0
 id: 5
0
 name: Ben Hurr
0
 times_validated_hash:
...
2
3
4
5
 
6
7
8
...
2
3
4
 
5
6
7
8
0
@@ -2,7 +2,7 @@ class Author #:nodoc:
0
   include Stone::Resource
0
   
0
   field :name, String
0
- field :email, String
0
+ field :email, String, :unique => true
0
   field :favorite_number, Fixnum
0
   field :created_at, DateTime
0
 
...
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,6 @@
0
+class Person
0
+ include Stone::Resource
0
+
0
+ field :name, String
0
+ has_many :comments
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
0
@@ -0,0 +1,9 @@
0
+class Post
0
+ include Stone::Resource
0
+
0
+ field :title, String
0
+ field :body, String
0
+
0
+ belongs_to :author
0
+ has_many :comments
0
+end
0
\ No newline at end of file
...
31
32
33
34
35
36
37
38
39
...
89
90
91
92
 
93
94
95
...
110
111
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
114
...
31
32
33
 
 
 
34
35
36
...
86
87
88
 
89
90
91
92
...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
0
@@ -31,9 +31,6 @@ describe Stone::Resource do
0
   end
0
   
0
   it "should be invalid when a field's class does not match its declaration" do
0
- # Field was declared in resource as:
0
- # field :author, String
0
-
0
     @author.name = 3
0
     @author.email = "nick@cladby.com"
0
     @author.save.should_not be_true
0
@@ -89,7 +86,7 @@ describe Stone::Resource do
0
     @author.email = "higglebear@higgly.com"
0
     @author.favorite_number = 3
0
     @author.save
0
- Author.first("favorite_number == 3").should be_instance_of Author
0
+ Author.first("favorite_number == 3").should be_instance_of(Author)
0
   end
0
   
0
   it "should perform a put if the object already exists on save" do
0
@@ -110,4 +107,30 @@ describe Stone::Resource do
0
     @author.save
0
     @author.name.should == "Ben Hurr"
0
   end
0
+
0
+ it "should not save if there is a duplicate and the field is unique" do
0
+ @author.name = "John Doe"
0
+ @author.email = "nick@bzzybee.com"
0
+ @author.save.should_not be_true
0
+ end
0
+
0
+ it "should retrieve a parent object if belongs_to has been set" do
0
+ @post = Post.new
0
+ @post.title = "Stone is Cool"
0
+ @post.body = "Stone is cool because..."
0
+ author = Author.first("name == 'Nick DeMonner'")
0
+ @post.author_id = author.id
0
+ @post.save
0
+ @post.author.should be_instance_of(Author)
0
+ end
0
+
0
+ it "should retrieve children objects id has_many has been set" do
0
+ @post = Post.new
0
+ @post.title = "Stone is Awesome"
0
+ @post.body = "Stone is awesome because..."
0
+ author = Author.first("name == 'Nick DeMonner'")
0
+ @post.author_id = author.id
0
+ @post.save
0
+ author.posts.size.should == 2
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.