Adding support to :cast_as => 'Date'.

This commit is contained in:
Tapajós 2009-09-02 23:54:25 -03:00
parent 273a174aae
commit bc6df2f5ca
3 changed files with 10 additions and 1 deletions

View file

@ -53,6 +53,9 @@ module CouchRest
# Auto parse Time objects # Auto parse Time objects
self[property.name] = if ((property.init_method == 'new') && target == 'Time') self[property.name] = if ((property.init_method == 'new') && target == 'Time')
self[key].is_a?(String) ? Time.parse(self[key].dup) : self[key] self[key].is_a?(String) ? Time.parse(self[key].dup) : self[key]
elsif
((property.init_method == 'new') && target == 'Date')
self[key].is_a?(String) ? Date.parse(self[key].dup) : self[key]
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)

View file

@ -122,7 +122,7 @@ describe "ExtendedDocument properties" do
describe "casting" do describe "casting" do
describe "cast keys to any type" do describe "cast keys to any type" do
before(:all) do before(:all) do
event_doc = { :subject => "Some event", :occurs_at => Time.now } event_doc = { :subject => "Some event", :occurs_at => Time.now, :end_date => Date.today }
e = Event.database.save_doc event_doc e = Event.database.save_doc event_doc
@event = Event.get e['id'] @event = Event.get e['id']
@ -130,6 +130,9 @@ describe "ExtendedDocument properties" do
it "should cast occurs_at to Time" do it "should cast occurs_at to Time" do
@event['occurs_at'].should be_an_instance_of(Time) @event['occurs_at'].should be_an_instance_of(Time)
end end
it "should cast end_date to Date" do
@event['end_date'].should be_an_instance_of(Date)
end
end end
end end

View file

@ -3,4 +3,7 @@ class Event < CouchRest::ExtendedDocument
property :subject property :subject
property :occurs_at, :cast_as => 'Time', :send => 'parse' property :occurs_at, :cast_as => 'Time', :send => 'parse'
property :end_date, :cast_as => 'Date'
end end