From 75a5018b1242ba2769121dfa0f86bea57f8ffbd3 Mon Sep 17 00:00:00 2001 From: Mutwin Kraus Date: Thu, 23 Apr 2009 12:24:38 +0800 Subject: [PATCH] Improvements for ExtendedDocument initializer * Tries to send arg= to the Document before setting the attribute Signed-off-by: Matt Aimonetti --- lib/couchrest/mixins/properties.rb | 2 +- lib/couchrest/more/extended_document.rb | 7 ++++++- spec/couchrest/more/extended_doc_spec.rb | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/couchrest/mixins/properties.rb b/lib/couchrest/mixins/properties.rb index b1dddd5..0049736 100644 --- a/lib/couchrest/mixins/properties.rb +++ b/lib/couchrest/mixins/properties.rb @@ -126,4 +126,4 @@ module CouchRest end end -end \ No newline at end of file +end diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb index c1590f1..4fe8182 100644 --- a/lib/couchrest/more/extended_document.rb +++ b/lib/couchrest/more/extended_document.rb @@ -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 \ No newline at end of file +end diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index 190baba..45eb0e3 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -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 \ No newline at end of file + + 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