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

Commit

Permalink
[dm-types] Refactored types
Browse files Browse the repository at this point in the history
* Changed usage of size() to length()
* Minor formatting change
* Removed Serial type which is included in dm-core
  • Loading branch information
dkubb committed Jun 23, 2009
1 parent 36db690 commit 4aad0d7
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 44 deletions.
1 change: 0 additions & 1 deletion Manifest.txt
Expand Up @@ -15,7 +15,6 @@ lib/dm-types/flag.rb
lib/dm-types/ip_address.rb
lib/dm-types/json.rb
lib/dm-types/regexp.rb
lib/dm-types/serial.rb
lib/dm-types/slug.rb
lib/dm-types/uri.rb
lib/dm-types/uuid.rb
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-types/bcrypt_hash.rb
Expand Up @@ -4,7 +4,7 @@ module DataMapper
module Types
class BCryptHash < DataMapper::Type
primitive String
size 60
length 60

def self.load(value, property)
typecast(value, property)
Expand Down
19 changes: 9 additions & 10 deletions lib/dm-types/csv.rb
Expand Up @@ -14,24 +14,23 @@ module DataMapper
module Types
class Csv < DataMapper::Type
primitive Text
lazy true
lazy true

def self.load(value, property)
case value
when String then CSV.parse(value)
when Array then value
else nil
when String then CSV.parse(value)
when Array then value
else
nil
end
end

def self.dump(value, property)
case value
when Array then
CSV.generate do |csv|
value.each { |row| csv << row }
end
when String then value
else nil
when Array then CSV.generate { |csv| value.each { |row| csv << row } }
when String then value
else
nil
end
end
end # class Csv
Expand Down
11 changes: 3 additions & 8 deletions lib/dm-types/epoch_time.rb
Expand Up @@ -4,8 +4,7 @@ class EpochTime < DataMapper::Type
primitive Integer

def self.load(value, property)
case value
when Integer
if value.kind_of?(Integer)
Time.at(value)
else
value
Expand All @@ -14,12 +13,8 @@ def self.load(value, property)

def self.dump(value, property)
case value
when Integer
value
when Time
value.to_i
when DateTime
Time.parse(value.to_s).to_i
when Integer, Time then value.to_i
when DateTime then value.to_time.to_i
end
end
end # class EpochTime
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-types/file_path.rb
Expand Up @@ -5,7 +5,7 @@ module DataMapper
module Types
class FilePath < DataMapper::Type
primitive String
size 255
length 255

def self.load(value, property)
if value.blank?
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-types/ip_address.rb
Expand Up @@ -4,7 +4,7 @@ module DataMapper
module Types
class IPAddress < DataMapper::Type
primitive String
size 16
length 16

def self.load(value, property)
if value.nil?
Expand Down
4 changes: 2 additions & 2 deletions lib/dm-types/json.rb
Expand Up @@ -11,8 +11,8 @@ module DataMapper
module Types
class Json < DataMapper::Type
primitive String
size 65535
lazy true
length 65535
lazy true

def self.load(value, property)
if value.nil?
Expand Down
5 changes: 2 additions & 3 deletions lib/dm-types/regexp.rb
Expand Up @@ -8,12 +8,11 @@ def self.load(value, property)
end

def self.dump(value, property)
return nil if value.nil?
value.source
value.source unless value.nil?
end

def self.typecast(value, property)
value.kind_of?(::Regexp) ? value : load(value, property)
load(value, property)
end
end
end
Expand Down
8 changes: 0 additions & 8 deletions lib/dm-types/serial.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/dm-types/slug.rb
Expand Up @@ -4,7 +4,7 @@ module DataMapper
module Types
class Slug < DataMapper::Type
primitive String
size 65535
length 65535

def self.load(value, property)
value
Expand Down
14 changes: 8 additions & 6 deletions lib/dm-types/uuid.rb
Expand Up @@ -39,20 +39,22 @@ module Types
#
class UUID < DataMapper::Type
primitive String
size 36
length 36

def self.load(value, property)
return nil if value.nil?
UUIDTools::UUID.parse(value)
UUIDTools::UUID.parse(value) unless value.nil?
end

def self.dump(value, property)
return nil if value.nil?
value.to_s
value.to_s unless value.nil?
end

def self.typecast(value, property)
value.kind_of?(UUIDTools::UUID) ? value : load(value, property)
if value.kind_of?(UUIDTools::UUID)
value
else
load(value, property)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dm-types/yaml.rb
Expand Up @@ -4,8 +4,8 @@ module DataMapper
module Types
class Yaml < DataMapper::Type
primitive String
size 65535
lazy true
length 65535
lazy true

def self.load(value, property)
if value.nil?
Expand Down

0 comments on commit 4aad0d7

Please sign in to comment.