Adding support for defining cast_as on properties as a Class

This commit is contained in:
Sam Lown 2010-03-30 20:50:47 +00:00
parent 64d68ecc1a
commit dd3df8fb69
8 changed files with 34 additions and 25 deletions

View file

@ -1,13 +1,8 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.join(FIXTURE_PATH, 'more', 'cat')
require File.join(FIXTURE_PATH, 'more', 'person')
require File.join(FIXTURE_PATH, 'more', 'card')
class Car < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
property :name
property :driver, :cast_as => 'Driver'
end
class Driver < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
# You have to add a casted_by accessor if you want to reach a casted extended doc parent
@ -16,19 +11,30 @@ class Driver < CouchRest::ExtendedDocument
property :name
end
class Car < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
property :name
property :driver, :cast_as => 'Driver'
property :backseat_driver, :cast_as => Driver
end
describe "casting an extended document" do
before(:each) do
@driver = Driver.new(:name => 'Matt')
@car = Car.new(:name => 'Renault 306', :driver => @driver)
@car2 = Car.new(:name => 'Renault 306', :backseat_driver => @driver.dup)
end
it "should retain all properties of the casted attribute" do
@car.driver.should == @driver
@car2.backseat_driver.should == @driver
end
it "should let the casted document know who casted it" do
@car.driver.casted_by.should == @car
@car2.backseat_driver.casted_by.should == @car2
end
end
@ -70,4 +76,4 @@ describe "casting an extended document from parsed JSON" do
it "should retain all properties of the casted attribute" do
@new_car.driver.should == @driver
end
end
end

View file

@ -124,7 +124,7 @@ describe CouchRest::CastedModel do
@obj = DummyModel.new(:keywords => ['couch', 'sofa', 'relax', 'canapé'])
end
it "should cast the array propery" do
it "should cast the array properly" do
@obj.keywords.should be_an_instance_of(Array)
@obj.keywords.first.should == 'couch'
end