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:
parent
0647307acd
commit
dff005f082
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue