Permalink
Browse files

This is currently (very) broken. EpochTime behaves correctly, but dm-…

…migrations sees it as a TIME, not an INT
  • Loading branch information...
1 parent 9746c36 commit d14e40cc42f3a26e0983b82c5758804a0110d00e @d11wtq d11wtq committed Jul 11, 2011
Showing with 68 additions and 13 deletions.
  1. +1 −0 .gitignore
  2. +17 −7 lib/dm-types/epoch_time.rb
  3. +1 −0 spec/fixtures/person.rb
  4. +33 −0 spec/integration/epoch_time_spec.rb
  5. +16 −6 spec/unit/epoch_time_spec.rb
View
@@ -1,5 +1,6 @@
## MAC OS
.DS_Store
+._*
## TEXTMATE
*.tmproj
View
@@ -2,23 +2,33 @@
module DataMapper
class Property
- class EpochTime < Integer
-
- def load(value)
+ class EpochTime < Time
+ def typecast_to_primitive(value)
if value.kind_of?(::Numeric)
::Time.at(value.to_i)
else
- value
+ super
end
end
+
+ def valid?(value)
+ value.nil? || value.kind_of?(::Time)
+ end
- def dump(value)
+ def typecast(value)
case value
- when ::Numeric, ::Time then value.to_i
- when ::DateTime then datetime_to_time(value).to_i
+ when ::Time then value
+ when ::Numeric then ::Time.at(value.to_i)
+ when ::DateTime then datetime_to_time(value)
+ when ::String then ::Time.parse(value)
+ else super
end
end
+ def dump(value)
+ value.to_i if value
+ end
+
private
def datetime_to_time(datetime)
View
@@ -15,6 +15,7 @@ class Person
property :name, String
property :positions, Json
property :inventions, Yaml
+ property :birthday, EpochTime
property :interests, CommaSeparatedList
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+try_spec do
+
+ require './spec/fixtures/person'
+
+ describe DataMapper::TypesFixtures::Person do
+ supported_by :all do
+ before :all do
+ @resource = DataMapper::TypesFixtures::Person.new(:name => '')
+ end
+
+ describe 'with a birthday' do
+ before :all do
+ @resource.birthday = '1983-05-03'
+ end
+
+ describe 'when dumped and loaded again' do
+ before :all do
+ #puts DataMapper::TypesFixtures::Person.properties[:birthday].valid?(Time.now)
+ @resource.save.should be(true)
+ @resource.reload
+ end
+
+ it 'has a valid birthday' do
+ @resource.birthday.should == ::Time.parse('1983-05-03')
+ end
+ end
+ end
+
+ end
+ end
+end
@@ -21,22 +21,32 @@ class ::User
it { should == value.to_i }
end
+ describe 'with nil' do
+ let(:value) { nil }
+
+ it { should == value }
+ end
+ end
+
+ describe '#typecast' do
+ subject { @property.typecast(value) }
+
describe 'with a DateTime instance' do
let(:value) { DateTime.now }
- it { should == Time.parse(value.to_s).to_i }
+ it { should == Time.parse(value.to_s) }
end
describe 'with a number' do
let(:value) { Time.now.to_i }
- it { should == value }
+ it { should == ::Time.at(value) }
end
+
+ describe 'with a DateTime string' do
+ let(:value) { '2011-07-11 15:00:04 UTC' }
- describe 'with nil' do
- let(:value) { nil }
-
- it { should == value }
+ it { should == ::Time.parse(value) }
end
end

0 comments on commit d14e40c

Please sign in to comment.