forked from cardmagic/dm-more
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic support for native UUID type in postgres
- Loading branch information
Paul Sadauskas
committed
Sep 25, 2008
1 parent
4857fdc
commit 8f12b04
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'rubygems' | ||
require 'uuidtools' | ||
|
||
module DataMapper | ||
module Types | ||
# UUID Type | ||
# First run at this, because I need it. A few caveats: | ||
# * Only works on postgres, using the built-in native uuid type. | ||
# To make it work in mysql, you'll have to add a typemap entry to | ||
# the mysql_adapter. I think. I don't have mysql handy, so I'm | ||
# not going to try. For SQLite, this will have to inherit from the | ||
# String primitive | ||
# * Won't accept a random default, because of the namespace clash | ||
# between this and the UUIDtools gem. Also can't set the default | ||
# type to UUID() (postgres-contrib's native generator) and | ||
# automigrate, because auto_migrate! tries to make it a string "UUID()" | ||
# Feel free to enchance this, and delete these caveats when they're fixed. | ||
# | ||
# -- Rando Sept 25, 08 | ||
# | ||
class UUID < DataMapper::Type | ||
def self.load(value, property) | ||
return nil if value.nil? | ||
::UUID.parse(value) | ||
end | ||
|
||
def self.dump(value, property) | ||
return nil if value.nil? | ||
value.to_s | ||
end | ||
|
||
def self.typecast(value, property) | ||
value.kind_of?(::UUID) ? value : load(value, property) | ||
end | ||
|
||
::DataMapper::Property::TYPES << self | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'pathname' | ||
require 'uuidtools' | ||
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper' | ||
|
||
describe DataMapper::Types::UUID do | ||
|
||
before(:all) do | ||
class UUIDTest | ||
include DataMapper::Resource | ||
|
||
property :id, Serial | ||
property :uuid, DataMapper::Types::UUID | ||
end | ||
UUIDTest.auto_migrate! | ||
end | ||
|
||
it "should be settable as a uuid" do | ||
u = UUIDTest.create(:uuid => UUID.parse('b0fc632e-d744-4821-afe3-4ea0701859ee')) | ||
|
||
u.uuid.should be_kind_of(UUID) | ||
u.uuid.to_s.should == 'b0fc632e-d744-4821-afe3-4ea0701859ee' | ||
end | ||
|
||
it "should be settable as a string" do | ||
u = UUIDTest.create(:uuid => 'b0fc632e-d744-4821-afe3-4ea0701859ee') | ||
|
||
u.uuid.should be_kind_of(UUID) | ||
u.uuid.to_s.should == 'b0fc632e-d744-4821-afe3-4ea0701859ee' | ||
end | ||
|
||
it "should be allowed to be null" do | ||
u = UUIDTest.create() | ||
|
||
u.uuid.should be_nil | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters