merge rafaelss :as patch

This commit is contained in:
Chris Anderson 2008-12-31 15:40:17 -08:00
commit c180a317e8
2 changed files with 21 additions and 6 deletions

View file

@ -519,17 +519,14 @@ module CouchRest
self.class.casts.each do |k,v| self.class.casts.each do |k,v|
next unless self[k] next unless self[k]
target = v[:as] target = v[:as]
v[:send] ||= 'new'
if target.is_a?(Array) if target.is_a?(Array)
klass = ::Extlib::Inflection.constantize(target[0]) klass = ::Extlib::Inflection.constantize(target[0])
self[k] = self[k].collect do |value| self[k] = self[k].collect do |value|
klass == Time ? Time.parse(value) : klass.new(value) klass.send(v[:send], value)
end end
else else
self[k] = if target == 'Time' self[k] = ::Extlib::Inflection.constantize(target).send(v[:send], self[k])
Time.parse(self[k])
else
::Extlib::Inflection.constantize(target).new(self[k])
end
end end
end end
end end

View file

@ -89,6 +89,12 @@ class Player < CouchRest::Model
timestamps! timestamps!
end end
class Event < CouchRest::Model
key_accessor :subject, :occurs_at
cast :occurs_at, :as => 'Time', :send => 'parse'
end
describe "save bug" do describe "save bug" do
before(:each) do before(:each) do
CouchRest::Model.default_database = reset_test_db! CouchRest::Model.default_database = reset_test_db!
@ -322,6 +328,18 @@ describe CouchRest::Model do
end end
end end
describe "cast keys to any type" do
before(:all) do
event_doc = { :subject => "Some event", :occurs_at => Time.now }
e = Event.database.save event_doc
@event = Event.get e['id']
end
it "should cast created_at to Time" do
@event['occurs_at'].should be_an_instance_of(Time)
end
end
describe "saving a model" do describe "saving a model" do
before(:all) do before(:all) do
@obj = Basic.new @obj = Basic.new