From dff005f08229d46f72462e8df95332f0a3ac1198 Mon Sep 17 00:00:00 2001 From: Eric Watson Date: Mon, 25 May 2009 14:12:56 -0500 Subject: [PATCH] Modified CastedModel to apply defaults first (so the defaults don't overwrite modified values) Included failing (and now passing) specs --- lib/couchrest/more/casted_model.rb | 4 +-- spec/couchrest/more/casted_model_spec.rb | 36 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/couchrest/more/casted_model.rb b/lib/couchrest/more/casted_model.rb index 8ab794e..6f442c8 100644 --- a/lib/couchrest/more/casted_model.rb +++ b/lib/couchrest/more/casted_model.rb @@ -10,12 +10,12 @@ module CouchRest def initialize(keys={}) raise StandardError unless self.is_a? Hash + apply_defaults # defined in CouchRest::Mixins::Properties super() keys.each do |k,v| self[k.to_s] = v 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 def []= key, value diff --git a/spec/couchrest/more/casted_model_spec.rb b/spec/couchrest/more/casted_model_spec.rb index 269f03e..75b0c57 100644 --- a/spec/couchrest/more/casted_model_spec.rb +++ b/spec/couchrest/more/casted_model_spec.rb @@ -10,7 +10,8 @@ class WithCastedModelMixin < Hash include CouchRest::CastedModel property :name property :no_value - property :hash, :default => {} + property :details, :default => {} + property :casted_attribute, :cast_as => 'WithCastedModelMixin' end class DummyModel < CouchRest::ExtendedDocument @@ -42,11 +43,28 @@ describe CouchRest::CastedModel do @obj.name = 'Matt' @obj.name.should == 'Matt' 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 describe "casted as attribute" 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 end @@ -71,7 +89,11 @@ describe CouchRest::CastedModel do end 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 @@ -112,6 +134,14 @@ describe CouchRest::CastedModel do casted_obj.name.should == "test" 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 describe "saving document with array of casted models and validation" do