Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…migrations sees it as a TIME, not an INT
  • Loading branch information...
commit d14e40cc42f3a26e0983b82c5758804a0110d00e 1 parent 9746c36
@d11wtq d11wtq authored
View
1  .gitignore
@@ -1,5 +1,6 @@
## MAC OS
.DS_Store
+._*
## TEXTMATE
*.tmproj
View
24 lib/dm-types/epoch_time.rb
@@ -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
1  spec/fixtures/person.rb
@@ -15,6 +15,7 @@ class Person
property :name, String
property :positions, Json
property :inventions, Yaml
+ property :birthday, EpochTime
property :interests, CommaSeparatedList
View
33 spec/integration/epoch_time_spec.rb
@@ -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
View
22 spec/unit/epoch_time_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.