Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
This is currently (very) broken. EpochTime behaves correctly, but dm-…
Browse files Browse the repository at this point in the history
…migrations sees it as a TIME, not an INT
  • Loading branch information
d11wtq committed Jul 11, 2011
1 parent 9746c36 commit d14e40c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,6 @@
## MAC OS ## MAC OS
.DS_Store .DS_Store
._*


## TEXTMATE ## TEXTMATE
*.tmproj *.tmproj
Expand Down
24 changes: 17 additions & 7 deletions lib/dm-types/epoch_time.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@


module DataMapper module DataMapper
class Property class Property
class EpochTime < Integer class EpochTime < Time

def typecast_to_primitive(value)
def load(value)
if value.kind_of?(::Numeric) if value.kind_of?(::Numeric)
::Time.at(value.to_i) ::Time.at(value.to_i)
else else
value super
end end
end end

def valid?(value)
value.nil? || value.kind_of?(::Time)
end


def dump(value) def typecast(value)
case value case value
when ::Numeric, ::Time then value.to_i when ::Time then value
when ::DateTime then datetime_to_time(value).to_i 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
end end


def dump(value)
value.to_i if value
end

private private


def datetime_to_time(datetime) def datetime_to_time(datetime)
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/person.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Person
property :name, String property :name, String
property :positions, Json property :positions, Json
property :inventions, Yaml property :inventions, Yaml
property :birthday, EpochTime


property :interests, CommaSeparatedList property :interests, CommaSeparatedList


Expand Down
33 changes: 33 additions & 0 deletions spec/integration/epoch_time_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
22 changes: 16 additions & 6 deletions spec/unit/epoch_time_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ class ::User
it { should == value.to_i } it { should == value.to_i }
end 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 describe 'with a DateTime instance' do
let(:value) { DateTime.now } let(:value) { DateTime.now }


it { should == Time.parse(value.to_s).to_i } it { should == Time.parse(value.to_s) }
end end


describe 'with a number' do describe 'with a number' do
let(:value) { Time.now.to_i } let(:value) { Time.now.to_i }


it { should == value } it { should == ::Time.at(value) }
end end

describe 'with a DateTime string' do
let(:value) { '2011-07-11 15:00:04 UTC' }


describe 'with nil' do it { should == ::Time.parse(value) }
let(:value) { nil }

it { should == value }
end end
end end


Expand Down

0 comments on commit d14e40c

Please sign in to comment.