Merge branch 'casted-extendeddocument'

* casted-extendeddocument:
  fixed a bug where :include_docs on a view couldn't be overwritten
  spec'd casted extended document
This commit is contained in:
Matt Aimonetti 2009-03-02 21:22:15 -08:00
commit b1999ac8b7
7 changed files with 61 additions and 11 deletions

File diff suppressed because one or more lines are too long

View file

@ -27,7 +27,7 @@ require 'couchrest/monkeypatches'
# = CouchDB, close to the metal # = CouchDB, close to the metal
module CouchRest module CouchRest
VERSION = '0.16' unless self.const_defined?("VERSION") VERSION = '0.17' unless self.const_defined?("VERSION")
autoload :Server, 'couchrest/core/server' autoload :Server, 'couchrest/core/server'
autoload :Database, 'couchrest/core/database' autoload :Database, 'couchrest/core/database'

View file

@ -1,14 +1,15 @@
module CouchRest module CouchRest
class Response < Hash class Response < Hash
def initialize(keys = {}) def initialize(pkeys = {})
keys.each do |k,v| pkeys ||= {}
pkeys.each do |k,v|
self[k.to_s] = v self[k.to_s] = v
end end
end end
def []= key, value def []=(key, value)
super(key.to_s, value) super(key.to_s, value)
end end
def [] key def [](key)
super(key.to_s) super(key.to_s)
end end
end end

View file

@ -55,9 +55,14 @@ module CouchRest
else else
# Let people use :send as a Time parse arg # Let people use :send as a Time parse arg
klass = ::CouchRest.constantize(target) klass = ::CouchRest.constantize(target)
# I'm not convince we should or should not create a new instance if we are casting a doc/extended doc without default value and nothing was passed
# unless (property.casted &&
# (klass.superclass == CouchRest::ExtendedDocument || klass.superclass == CouchRest::Document) &&
# (self[key].nil? || property.default.nil?))
klass.send(property.init_method, self[key]) klass.send(property.init_method, self[key])
#end
end end
self[key].casted_by = self if self[key].respond_to?(:casted_by) self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
end end
end end
end end

View file

@ -133,7 +133,9 @@ module CouchRest
fetch_view(name, opts, &block) fetch_view(name, opts, &block)
else else
begin begin
view = fetch_view name, opts.merge({:include_docs => true}), &block # auto load mentioned documents unless asked differently
opts.merge({:include_docs => true}) unless opts.has_key?(:include_docs)
view = fetch_view name, opts, &block
view['rows'].collect{|r|new(r['doc'])} if view['rows'] view['rows'].collect{|r|new(r['doc'])} if view['rows']
rescue rescue
# fallback for old versions of couchdb that don't # fallback for old versions of couchdb that don't

View file

@ -24,15 +24,17 @@ module CouchRest
subklass.send(:include, CouchRest::Mixins::Properties) subklass.send(:include, CouchRest::Mixins::Properties)
end end
# Accessors
attr_accessor :casted_by
# Callbacks # Callbacks
define_callbacks :create define_callbacks :create
define_callbacks :save define_callbacks :save
define_callbacks :update define_callbacks :update
define_callbacks :destroy define_callbacks :destroy
def initialize(keys={}) def initialize(passed_keys={})
apply_defaults # defined in CouchRest::Mixins::Properties apply_defaults # defined in CouchRest::Mixins::Properties
keys ||= {}
super super
cast_keys # defined in CouchRest::Mixins::Properties cast_keys # defined in CouchRest::Mixins::Properties
unless self['_id'] && self['_rev'] unless self['_id'] && self['_rev']

View file

@ -0,0 +1,40 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
require File.join(FIXTURE_PATH, 'more', 'card')
class Car < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
property :name
property :driver, :cast_as => 'Driver'
end
class Driver < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
# You have to add a casted_by accessor if you want to reach a casted extended doc parent
attr_accessor :casted_by
property :name
end
describe "casting an extended document" do
before(:each) do
@car = Car.new(:name => 'Renault 306')
@driver = Driver.new(:name => 'Matt')
end
# it "should not create an empty casted object" do
# @car.driver.should be_nil
# end
it "should let you assign the casted attribute after instantializing an object" do
@car.driver = @driver
@car.driver.name.should == 'Matt'
end
it "should let the casted document who casted it" do
Car.new(:name => 'Renault 306', :driver => @driver)
@car.driver.casted_by.should == @car
end
end