[patch] When an attribute is an empty string, do not try and parse (and get current time)

* Spec to support handling empty strings when attribute is set to time.
This commit is contained in:
Paul Thornthwaite 2010-09-08 16:13:53 +01:00 committed by geemus
parent d299ae1579
commit 524a30918d
2 changed files with 25 additions and 1 deletions

View File

@ -51,7 +51,7 @@ module Fog
when :time
class_eval <<-EOS, __FILE__, __LINE__
def #{name}=(new_#{name})
if new_#{name}.nil? || new_#{name}.is_a?(Time)
if new_#{name}.nil? || new_#{name} == "" || new_#{name}.is_a?(Time)
@#{name} = new_#{name}
else
@#{name} = Time.parse(new_#{name})

View File

@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
class FogAttributeTestModel < Fog::Model
attribute :key_id, :aliases => "key", :squash => "id"
attribute :time, :type => :time
end
describe 'Fog::Attributes' do
@ -23,6 +24,29 @@ describe 'Fog::Attributes' do
end
end
describe "when merging a time field" do
it "should accept nil as a suitable setting" do
data = {"time" => nil}
model = FogAttributeTestModel.new
model.merge_attributes(data)
model.time.should be_nil
end
it "should accept empty string as a suitable setting" do
data = {"time" => ""}
model = FogAttributeTestModel.new
model.merge_attributes(data)
model.time.should == ""
end
it "should parse strings to get a Datetime" do
test_time = Time.parse("2010-11-12T13:14:15")
data = {"time" => test_time.to_s}
model = FogAttributeTestModel.new
model.merge_attributes(data)
model.time.should == test_time
end
end
end
end