Skip to content

Commit

Permalink
Improvements for ExtendedDocument initializer
Browse files Browse the repository at this point in the history
  * Tries to send arg= to the Document before setting the attribute

Signed-off-by: Matt Aimonetti <mattaimonetti@gmail.com>
  • Loading branch information
Mutwin Kraus authored and mattetti committed Apr 28, 2009
1 parent 6fca60e commit 75a5018
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/couchrest/mixins/properties.rb
Expand Up @@ -126,4 +126,4 @@ def #{meth}=(value)

end
end
end
end
7 changes: 6 additions & 1 deletion lib/couchrest/more/extended_document.rb
Expand Up @@ -33,6 +33,11 @@ def self.inherited(subklass)

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']
Expand Down Expand Up @@ -212,4 +217,4 @@ def destroy(bulk=false)
end

end
end
end
24 changes: 23 additions & 1 deletion spec/couchrest/more/extended_doc_spec.rb
Expand Up @@ -52,6 +52,19 @@ class WithTemplateAndUniqueID < CouchRest::ExtendedDocument
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
Expand Down Expand Up @@ -506,4 +519,13 @@ class WithTemplateAndUniqueID < CouchRest::ExtendedDocument

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

0 comments on commit 75a5018

Please sign in to comment.