Permalink
Browse files

Added to delete index at destroy and return [] when Model.all does no…

…t find any
  • Loading branch information...
1 parent eacbd75 commit 5da144342546f2daf8bcbf4cb7e24bef37ebe73f Makoto Inoue committed Nov 12, 2008
Showing with 80 additions and 28 deletions.
  1. +29 −6 lib/tc_adapter.rb
  2. +51 −22 spec/tc_adapater_spec.rb
View
35 lib/tc_adapter.rb
@@ -42,11 +42,17 @@ def create(resources)
def read_many(query)
results = parse_query(query)
- Collection.new(query) do |collection|
- results.each do |result|
- data = map_into_query_field(query, result)
- collection.load(data)
+ if results # to handle results == nil
+ Collection.new(query) do |collection|
+ results.each do |result|
+ data = map_into_query_field(query, result)
+ if data # to handle results == [nil]
+ collection.load(data)
+ end
+ end
end
+ else
+ []
end
end
@@ -81,9 +87,24 @@ def update(attributes, query)
def delete(query)
item_id = get_id(query)
+
+ attributes = get_items_from_id(query, item_id)
+ # Don't need id attribute and attribut with no data.
+ attributes.reject{|k,v| k == :id || v == nil}.each do | k, v|
+ access_data(query.model, k) do |item|
+ items = item.getlist(v)
+ items = items - [item_id]
+ item.out(v)
+ if items.size > 0
+ item.putlist(v, items)
+ end
+ end
+ end
+
access_data(query.model) do |item|
item.out(item_id)
end
+
# Seems required to return 1 to update @new_record instance variable at DataMapper::Resource.
# Not quite sure how it works.
1
@@ -166,8 +187,10 @@ def parse_query(query)
end
def map_into_query_field(query, data)
- query.fields.map do |property|
- data[property.field.to_sym]
+ if data
+ query.fields.map do |property|
+ data[property.field.to_sym]
+ end
end
end
end # class AbstractAdapter
View
73 spec/tc_adapater_spec.rb
@@ -46,36 +46,65 @@
end
end
- it "should update an item" do
- @user.name = 'peter'
- @user.age = 22
-
- @user.save
- user = User.get(@user.id)
- user.name.should == @user.name
- user.age.should == @user.age
+ describe "update" do
+ it "should update an item" do
+ @user.name = 'peter'
+ @user.age = 22
+
+ @user.save
+ user = User.get(@user.id)
+ user.name.should == @user.name
+ user.age.should == @user.age
+ end
+
+ # it "should reflect index" do
+ # @user.name = 'thomas'
+ # @user.save
+ # User.first(:name => 'thomas').should == @user
+ # end
end
- it "should destroy an item" do
- @user.destroy
- lambda{User.get!(@user.id)}.should raise_error(DataMapper::ObjectNotFoundError)
+
+ describe "destroy" do
+ it "should destroy an item" do
+ @user.destroy
+ lambda{User.get!(@user.id)}.should raise_error(DataMapper::ObjectNotFoundError)
+ User.first(:name => @user.name).should == nil
+ User.all(:name => @user.name).should == []
+ end
+
+ it "should reflect index" do
+ pending
+ end
end
end
describe 'Finder' do
- before(:each) do
- @tom = User.create(:name => 'tom')
- @peter = User.create(:name => 'peter')
- @post = Post.create
+ describe "when no data" do
+ it "first should return nil" do
+ User.first(:name => 'someone').should == nil
+ end
+
+ it "all should return []" do
+ User.all(:name => 'someone').should == []
+ end
end
+
+ describe "when data" do
+ before(:each) do
+ @tom = User.create(:name => 'tom')
+ @peter = User.create(:name => 'peter')
+ @post = Post.create
+ end
- it 'should get one record per model' do
- User.first.should == @tom
- Post.first.should == @post
- end
+ it 'should get one record per model' do
+ User.first.should == @tom
+ Post.first.should == @post
+ end
- it 'should return collection of all records per model' do
- Post.all.should have(1).post
- User.all.should have(2).users
+ it 'should return collection of all records per model' do
+ Post.all.should have(1).post
+ User.all.should have(2).users
+ end
end
end

0 comments on commit 5da1443

Please sign in to comment.