From cd53e16eba423dca59c7bf8cd47077c5daf92422 Mon Sep 17 00:00:00 2001 From: Matt Lyon Date: Fri, 19 Dec 2008 02:06:05 -0800 Subject: [PATCH 1/2] instantiate Time casts correctly --- lib/couchrest/core/model.rb | 8 ++++++-- spec/couchrest/core/model_spec.rb | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 0fccb27..11df20f 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -540,10 +540,14 @@ module CouchRest if target.is_a?(Array) klass = ::Extlib::Inflection.constantize(target[0]) self[k] = self[k].collect do |value| - klass.new(value) + klass == Time ? Time.parse(value) : klass.new(value) end else - self[k] = ::Extlib::Inflection.constantize(target).new(self[k]) + self[k] = if target == 'Time' + Time.parse(self[k]) + else + ::Extlib::Inflection.constantize(target).new(self[k]) + end end end end diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 466b850..8be5444 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -40,6 +40,7 @@ class Course < CouchRest::Model key_accessor :title cast :questions, :as => ['Question'] cast :professor, :as => 'Person' + cast :final_test_at, :as => 'Time' view_by :title view_by :dept, :ducktype => true end @@ -301,7 +302,8 @@ describe CouchRest::Model do "title" => "Metaphysics 410", "professor" => { "name" => ["Mark", "Hinchliff"] - } + }, + "final_test_at" => "2008/12/19 13:00:00 +0800" } r = Course.database.save course_doc @course = Course.get r['id'] @@ -312,6 +314,9 @@ describe CouchRest::Model do it "should instantiate the professor as a person" do @course['professor'].last_name.should == "Hinchliff" end + it "should instantiate the final_test_at as a Time" do + @course['final_test_at'].should == Time.parse("2008/12/19 13:00:00 +0800") + end end describe "saving a model" do From ce172cf77b2ad5a09fe0a07b3c52878b34a89544 Mon Sep 17 00:00:00 2001 From: Matt Lyon Date: Fri, 19 Dec 2008 02:09:20 -0800 Subject: [PATCH 2/2] run individual specs without rake --- spec/spec_helper.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c6ff9c4..4af958f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require "rubygems" +require "spec" # Satisfies Autotest and anyone else not using the Rake tasks + require File.dirname(__FILE__) + '/../lib/couchrest' FIXTURE_PATH = File.dirname(__FILE__) + '/fixtures'