spec'd casted extended document
This commit is contained in:
parent
80317f31a5
commit
55ecda2c90
File diff suppressed because one or more lines are too long
|
@ -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'
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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']
|
||||||
|
|
40
spec/couchrest/more/casted_extended_doc_spec.rb
Normal file
40
spec/couchrest/more/casted_extended_doc_spec.rb
Normal 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
|
Loading…
Reference in a new issue