diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 0fccb27..669d8a3 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -537,13 +537,14 @@ module CouchRest self.class.casts.each do |k,v| next unless self[k] target = v[:as] + v[:send] ||= 'new' if target.is_a?(Array) klass = ::Extlib::Inflection.constantize(target[0]) self[k] = self[k].collect do |value| - klass.new(value) + klass.send(v[:send], value) end else - self[k] = ::Extlib::Inflection.constantize(target).new(self[k]) + self[k] = ::Extlib::Inflection.constantize(target).send(v[:send], self[k]) end end end @@ -554,4 +555,4 @@ module CouchRest register_instance_hooks :save, :create, :update, :destroy end # class Model -end # module CouchRest \ No newline at end of file +end # module CouchRest diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 466b850..4b28921 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -88,6 +88,12 @@ class Player < CouchRest::Model timestamps! end +class Event < CouchRest::Model + key_accessor :subject, :occurs_at + + cast :occurs_at, :as => 'Time', :send => 'parse' +end + describe "save bug" do it "should fix" do @db = reset_test_db! @@ -314,6 +320,18 @@ describe CouchRest::Model do 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 before(:all) do @obj = Basic.new @@ -679,4 +697,4 @@ describe CouchRest::Model do lambda{Basic.get(@obj.id)}.should raise_error end end -end \ No newline at end of file +end