public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Search Repo:
Added find or create support
myobie (author)
Thu May 08 06:49:30 -0700 2008
commit  d304b43f1bd73a3731ab355f4924d077a4cb5e75
tree    a9a17573dcc543ad32a1c3edbabe27ff3aba6367
parent  5aa6314b6c588b25d19445002655e2cc41538a35
...
387
388
389
 
 
 
 
390
391
392
...
387
388
389
390
391
392
393
394
395
396
0
@@ -387,6 +387,10 @@
0
         resource
0
       end
0
 
0
+ def find_or_create(search_attributes, create_attributes = {})
0
+ first(search_attributes) || create(search_attributes.merge(create_attributes))
0
+ end
0
+
0
       # TODO SPEC
0
       def copy(source, destination, options = {})
0
         repository(destination) do
...
286
287
288
 
 
 
 
 
 
 
289
290
291
...
286
287
288
289
290
291
292
293
294
295
296
297
298
0
@@ -286,6 +286,13 @@
0
       Planet.should respond_to(:first)
0
       Planet.should respond_to(:all)
0
       Planet.should respond_to(:[])
0
+ Planet.should respond_to(:find_or_create)
0
+ end
0
+
0
+ it 'should create if it cannot find (using find_or_create)' do
0
+ Planet.should_receive(:first).with({ :age => 1 }).and_return(nil)
0
+ Planet.should_receive(:create).with({ :age => 1, :name => "Fuzzy" }).and_return(true)
0
+ Planet.find_or_create({:age => 1}, { :name => "Fuzzy" })
0
     end
0
     
0
     it '.exists? should return whether or not the repository exists' do

Comments