Allowing save of default value for read-only property

This commit is contained in:
Eric Watson 2009-05-15 14:44:41 -05:00 committed by Matt Aimonetti
parent 9d4837993a
commit 406eaebfc9
2 changed files with 6 additions and 6 deletions

View file

@ -23,8 +23,8 @@ module CouchRest
# TODO: cache the default object # TODO: cache the default object
self.class.properties.each do |property| self.class.properties.each do |property|
key = property.name.to_s key = property.name.to_s
# let's make sure we have a default and we can assign the value # let's make sure we have a default
if !property.default.nil? && (self.respond_to?("#{key}=") || self.key?(key)) if property.default
if property.default.class == Proc if property.default.class == Proc
self[key] = property.default.call self[key] = property.default.call
else else

View file

@ -10,7 +10,7 @@ describe "ExtendedDocument" do
property :preset, :default => {:right => 10, :top_align => false} property :preset, :default => {:right => 10, :top_align => false}
property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time' property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
property :tags, :default => [] property :tags, :default => []
property :false_default, :default => false property :read_only_with_default, :default => 'generic', :read_only => true
property :name property :name
timestamps! timestamps!
end end
@ -158,10 +158,10 @@ describe "ExtendedDocument" do
obj = WithDefaultValues.new(:tags => ['spec']) obj = WithDefaultValues.new(:tags => ['spec'])
obj.tags.should == ['spec'] obj.tags.should == ['spec']
end end
it "should work with a default value of false" do it "should set default value of read-only property" do
obj = WithDefaultValues.new obj = WithDefaultValues.new
obj.false_default.should == false obj.read_only_with_default.should == 'generic'
end end
end end