Modified CastedModel to apply defaults first

(so the defaults don't overwrite modified values)
Included failing (and now passing) specs
This commit is contained in:
Eric Watson 2009-05-25 14:12:56 -05:00 committed by Matt Aimonetti
parent 0647307acd
commit dff005f082
2 changed files with 35 additions and 5 deletions

View file

@ -10,12 +10,12 @@ module CouchRest
def initialize(keys={}) def initialize(keys={})
raise StandardError unless self.is_a? Hash raise StandardError unless self.is_a? Hash
apply_defaults # defined in CouchRest::Mixins::Properties
super() super()
keys.each do |k,v| keys.each do |k,v|
self[k.to_s] = v self[k.to_s] = v
end if keys end if keys
apply_defaults # defined in CouchRest::Mixins::Properties cast_keys # defined in CouchRest::Mixins::Properties
# cast_keys # defined in CouchRest::Mixins::Properties
end end
def []= key, value def []= key, value

View file

@ -10,7 +10,8 @@ class WithCastedModelMixin < Hash
include CouchRest::CastedModel include CouchRest::CastedModel
property :name property :name
property :no_value property :no_value
property :hash, :default => {} property :details, :default => {}
property :casted_attribute, :cast_as => 'WithCastedModelMixin'
end end
class DummyModel < CouchRest::ExtendedDocument class DummyModel < CouchRest::ExtendedDocument
@ -42,11 +43,28 @@ describe CouchRest::CastedModel do
@obj.name = 'Matt' @obj.name = 'Matt'
@obj.name.should == 'Matt' @obj.name.should == 'Matt'
end end
it "should allow override of default" do
@obj = WithCastedModelMixin.new(:name => 'Eric', :details => {'color' => 'orange'})
@obj.name.should == 'Eric'
@obj.details['color'].should == 'orange'
end
end
describe "casted as an attribute, but without a value" do
before(:each) do
@obj = DummyModel.new
@casted_obj = @obj.casted_attribute
end
it "should be nil" do
@casted_obj.should == nil
end
end end
describe "casted as attribute" do describe "casted as attribute" do
before(:each) do before(:each) do
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever'}) casted = {:name => 'not whatever'}
@obj = DummyModel.new(:casted_attribute => {:name => 'whatever', :casted_attribute => casted})
@casted_obj = @obj.casted_attribute @casted_obj = @obj.casted_attribute
end end
@ -71,7 +89,11 @@ describe CouchRest::CastedModel do
end end
it "should return {} for the hash attribute" do it "should return {} for the hash attribute" do
@casted_obj.hash.should == {} @casted_obj.details.should == {}
end
it "should cast its own attributes" do
@casted_obj.casted_attribute.should be_instance_of(WithCastedModelMixin)
end end
end end
@ -112,6 +134,14 @@ describe CouchRest::CastedModel do
casted_obj.name.should == "test" casted_obj.name.should == "test"
end end
it "should retain an override of a casted model attribute's default" do
casted_obj = @obj.casted_attribute
casted_obj.details['color'] = 'orange'
@obj.save
casted_obj = DummyModel.get(@obj.id).casted_attribute
casted_obj.details['color'].should == 'orange'
end
end end
describe "saving document with array of casted models and validation" do describe "saving document with array of casted models and validation" do