Improvements for ExtendedDocument initializer

* Tries to send arg= to the Document before setting the attribute

Signed-off-by: Matt Aimonetti <mattaimonetti@gmail.com>
This commit is contained in:
Mutwin Kraus 2009-04-23 12:24:38 +08:00 committed by Matt Aimonetti
parent 6fca60ebe4
commit 75a5018b12
3 changed files with 30 additions and 3 deletions

View file

@ -126,4 +126,4 @@ module CouchRest
end
end
end
end

View file

@ -33,6 +33,11 @@ module CouchRest
def initialize(passed_keys={})
apply_defaults # defined in CouchRest::Mixins::Properties
passed_keys.each do |k,v|
if self.respond_to?("#{k}=")
self.send("#{k}=", passed_keys.delete(k))
end
end if passed_keys
super
cast_keys # defined in CouchRest::Mixins::Properties
unless self['_id'] && self['_rev']
@ -212,4 +217,4 @@ module CouchRest
end
end
end
end

View file

@ -52,6 +52,19 @@ describe "ExtendedDocument" do
property :preset, :default => 'value'
property :has_no_default
end
class WithGetterAndSetterMethods < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
property :other_arg
def arg
other_arg
end
def arg=(value)
self.other_arg = "foo-#{value}"
end
end
before(:each) do
@obj = WithDefaultValues.new
@ -506,4 +519,13 @@ describe "ExtendedDocument" do
end
end
end
describe "getter and setter methods" do
it "should try to call the arg= method before setting :arg in the hash" do
@doc = WithGetterAndSetterMethods.new(:arg => "foo")
@doc['arg'].should be_nil
@doc[:arg].should be_nil
@doc.other_arg.should == "foo-foo"
end
end
end