fixed a bug with the casting of time objects
This commit is contained in:
parent
e07e5b468f
commit
bc47e72ae0
5 changed files with 51 additions and 14 deletions
|
@ -35,7 +35,6 @@ module CouchRest
|
|||
|
||||
def cast_keys
|
||||
return unless self.class.properties
|
||||
# TODO move the argument checking to the cast method for early crashes
|
||||
self.class.properties.each do |property|
|
||||
next unless property.casted
|
||||
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
||||
|
@ -44,17 +43,19 @@ module CouchRest
|
|||
klass = ::CouchRest.constantize(target[0])
|
||||
|
||||
self[property.name] = self[key].collect do |value|
|
||||
obj = ( (property.init_method == 'send') && klass == Time) ? Time.parse(value) : klass.send(property.init_method, value)
|
||||
# Auto parse Time objects
|
||||
obj = ( (property.init_method == 'new') && klass == Time) ? Time.parse(value) : klass.send(property.init_method, value)
|
||||
obj.casted_by = self if obj.respond_to?(:casted_by)
|
||||
obj
|
||||
end
|
||||
else
|
||||
# Let people use :send as a Time parse arg
|
||||
self[property.name] = if ((property.init_method != 'send') && target == 'Time')
|
||||
Time.parse(self[property.init_method])
|
||||
# Auto parse Time objects
|
||||
self[property.name] = if ((property.init_method == 'new') && target == 'Time')
|
||||
self[key].is_a?(String) ? Time.parse(self[key].dup) : self[key]
|
||||
else
|
||||
# Let people use :send as a Time parse arg
|
||||
klass = ::CouchRest.constantize(target)
|
||||
klass.send(property.init_method, self[property.name])
|
||||
klass.send(property.init_method, self[key])
|
||||
end
|
||||
self[key].casted_by = self if self[key].respond_to?(:casted_by)
|
||||
end
|
||||
|
@ -73,7 +74,7 @@ module CouchRest
|
|||
# make sure to use a mutex.
|
||||
def define_property(name, options={})
|
||||
# check if this property is going to casted
|
||||
options[:casted] = true if options[:cast_as]
|
||||
options[:casted] = options[:cast_as] ? options[:cast_as] : false
|
||||
property = CouchRest::Property.new(name, (options.delete(:cast_as) || options.delete(:type)), options)
|
||||
create_property_getter(property)
|
||||
create_property_setter(property) unless property.read_only == true
|
||||
|
|
|
@ -38,8 +38,8 @@ module CouchRest
|
|||
# decent time format by default. See Time#to_json
|
||||
def self.timestamps!
|
||||
class_eval <<-EOS, __FILE__, __LINE__
|
||||
property(:updated_at, :read_only => true)
|
||||
property(:created_at, :read_only => true)
|
||||
property(:updated_at, :read_only => true, :cast_as => 'Time')
|
||||
property(:created_at, :read_only => true, :cast_as => 'Time')
|
||||
|
||||
save_callback :before do |object|
|
||||
object['updated_at'] = Time.now
|
||||
|
|
|
@ -21,7 +21,7 @@ module CouchRest
|
|||
@alias = options.delete(:alias) if options[:alias]
|
||||
@default = options.delete(:default) if options[:default]
|
||||
@casted = options[:casted] ? true : false
|
||||
@init_method = options[:send] ? options.delete[:send] : 'new'
|
||||
@init_method = options[:send] ? options.delete(:send) : 'new'
|
||||
@options = options
|
||||
end
|
||||
|
||||
|
|
|
@ -852,4 +852,5 @@ describe CouchRest::Model do
|
|||
@obj.attachment_url(@attachment_name).should == "#{Basic.database}/#{@obj.id}/#{@attachment_name}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -3,15 +3,18 @@ require File.dirname(__FILE__) + '/../../spec_helper'
|
|||
class WithDefaultValues < CouchRest::ExtendedDocument
|
||||
use_database TEST_SERVER.default_database
|
||||
property :preset, :default => {:right => 10, :top_align => false}
|
||||
property :set_by_proc, :default => Proc.new{Time.now}, :type => 'Time'
|
||||
property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
|
||||
property :name
|
||||
timestamps!
|
||||
end
|
||||
|
||||
describe "ExtendedDocument" do
|
||||
|
||||
before(:each) do
|
||||
@obj = WithDefaultValues.new
|
||||
end
|
||||
|
||||
describe "with default" do
|
||||
before(:each) do
|
||||
@obj = WithDefaultValues.new
|
||||
end
|
||||
|
||||
it "should have the default value set at initalization" do
|
||||
@obj.preset.should == {:right => 10, :top_align => false}
|
||||
|
@ -24,4 +27,36 @@ describe "ExtendedDocument" do
|
|||
end
|
||||
end
|
||||
|
||||
describe "timestamping" do
|
||||
|
||||
it "should define the updated_at and created_at getters and set the values" do
|
||||
@obj.save
|
||||
obj = WithDefaultValues.get(@obj.id)
|
||||
obj.created_at.should be_an_instance_of(Time)
|
||||
obj.updated_at.should be_an_instance_of(Time)
|
||||
obj.created_at.to_s.should == @obj.updated_at.to_s
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "saving and retrieving" do
|
||||
|
||||
it "should work fine" do
|
||||
@obj.name = "should be easily saved and retrieved"
|
||||
@obj.save
|
||||
saved_obj = WithDefaultValues.get(@obj.id)
|
||||
saved_obj.should_not be_nil
|
||||
end
|
||||
|
||||
it "should parse the Time attributes automatically" do
|
||||
@obj.name = "should parse the Time attributes automatically"
|
||||
@obj.set_by_proc.should be_an_instance_of(Time)
|
||||
@obj.save
|
||||
@obj.set_by_proc.should be_an_instance_of(Time)
|
||||
saved_obj = WithDefaultValues.get(@obj.id)
|
||||
saved_obj.set_by_proc.should be_an_instance_of(Time)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue