dm-types 0.9.11 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{History.txt → History.rdoc} +5 -4
- data/LICENSE +16 -14
- data/Manifest.txt +13 -3
- data/{README.txt → README.rdoc} +0 -0
- data/Rakefile +3 -4
- data/TODO +74 -0
- data/lib/dm-types.rb +15 -18
- data/lib/dm-types/bcrypt_hash.rb +1 -4
- data/lib/dm-types/comma_separated_list.rb +31 -0
- data/lib/dm-types/csv.rb +12 -18
- data/lib/dm-types/enum.rb +18 -15
- data/lib/dm-types/epoch_time.rb +6 -11
- data/lib/dm-types/file_path.rb +5 -4
- data/lib/dm-types/flag.rb +14 -13
- data/lib/dm-types/ip_address.rb +1 -0
- data/lib/dm-types/json.rb +5 -11
- data/lib/dm-types/regexp.rb +2 -3
- data/lib/dm-types/slug.rb +8 -17
- data/lib/dm-types/uri.rb +8 -14
- data/lib/dm-types/uuid.rb +9 -15
- data/lib/dm-types/version.rb +1 -1
- data/lib/dm-types/yaml.rb +4 -3
- data/spec/fixtures/article.rb +39 -0
- data/spec/fixtures/bookmark.rb +27 -0
- data/spec/fixtures/invention.rb +9 -0
- data/spec/fixtures/network_node.rb +40 -0
- data/spec/fixtures/person.rb +29 -0
- data/spec/fixtures/software_package.rb +37 -0
- data/spec/fixtures/ticket.rb +25 -0
- data/spec/fixtures/tshirt.rb +28 -0
- data/spec/integration/bcrypt_hash_spec.rb +37 -55
- data/spec/integration/comma_separated_list_spec.rb +89 -0
- data/spec/integration/enum_spec.rb +62 -42
- data/spec/integration/file_path_spec.rb +149 -17
- data/spec/integration/flag_spec.rb +56 -25
- data/spec/integration/ip_address_spec.rb +142 -17
- data/spec/integration/json_spec.rb +60 -17
- data/spec/integration/slug_spec.rb +39 -40
- data/spec/integration/uri_spec.rb +126 -21
- data/spec/integration/uuid_spec.rb +84 -30
- data/spec/integration/yaml_spec.rb +55 -25
- data/spec/shared/identity_function_group.rb +5 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +49 -17
- data/spec/unit/bcrypt_hash_spec.rb +107 -64
- data/spec/unit/csv_spec.rb +111 -27
- data/spec/unit/enum_spec.rb +128 -87
- data/spec/unit/epoch_time_spec.rb +57 -32
- data/spec/unit/file_path_spec.rb +55 -34
- data/spec/unit/flag_spec.rb +102 -65
- data/spec/unit/ip_address_spec.rb +90 -40
- data/spec/unit/json_spec.rb +108 -41
- data/spec/unit/regexp_spec.rb +47 -17
- data/spec/unit/uri_spec.rb +57 -46
- data/spec/unit/yaml_spec.rb +91 -45
- data/tasks/install.rb +1 -1
- data/tasks/spec.rb +4 -4
- metadata +25 -32
- data/lib/dm-types/serial.rb +0 -8
data/lib/dm-types/flag.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
module DataMapper
|
2
2
|
module Types
|
3
|
-
class Flag <
|
3
|
+
class Flag < Type
|
4
|
+
primitive Integer
|
5
|
+
|
4
6
|
def self.inherited(target)
|
5
|
-
target.instance_variable_set(
|
7
|
+
target.instance_variable_set('@primitive', self.primitive)
|
6
8
|
end
|
7
9
|
|
8
10
|
def self.flag_map
|
@@ -18,7 +20,7 @@ module DataMapper
|
|
18
20
|
type.flag_map = {}
|
19
21
|
|
20
22
|
flags.each_with_index do |flag, i|
|
21
|
-
type.flag_map[
|
23
|
+
type.flag_map[i] = flag
|
22
24
|
end
|
23
25
|
|
24
26
|
type
|
@@ -29,13 +31,13 @@ module DataMapper
|
|
29
31
|
end
|
30
32
|
|
31
33
|
def self.load(value, property)
|
34
|
+
return [] if value.nil? || value <= 0
|
35
|
+
|
32
36
|
begin
|
33
37
|
matches = []
|
34
38
|
|
35
|
-
|
36
|
-
|
37
|
-
pow = 2 ** i
|
38
|
-
matches << flag_map[pow] if value & pow == pow
|
39
|
+
0.upto(flag_map.size - 1) do |i|
|
40
|
+
matches << flag_map[i] if value[i] == 1
|
39
41
|
end
|
40
42
|
|
41
43
|
matches.compact
|
@@ -46,16 +48,15 @@ module DataMapper
|
|
46
48
|
|
47
49
|
def self.dump(value, property)
|
48
50
|
return if value.nil?
|
49
|
-
flags = value.
|
50
|
-
flags.
|
51
|
-
flag_map.invert.values_at(*flags.flatten).compact.inject(0) { |sum, i| sum + i }
|
51
|
+
flags = Array(value).map { |flag| flag.to_sym }.flatten
|
52
|
+
flag_map.invert.values_at(*flags).compact.inject(0) { |sum, i| sum += 1 << i }
|
52
53
|
end
|
53
54
|
|
54
55
|
def self.typecast(value, property)
|
55
56
|
case value
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
when nil then nil
|
58
|
+
when Array then value.map {|v| v.to_sym}
|
59
|
+
else value.to_sym
|
59
60
|
end
|
60
61
|
end
|
61
62
|
end # class Flag
|
data/lib/dm-types/ip_address.rb
CHANGED
data/lib/dm-types/json.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
|
-
|
2
|
-
gem "json"
|
3
|
-
require "json/ext"
|
4
|
-
rescue LoadError
|
5
|
-
gem "json_pure"
|
6
|
-
require "json/pure"
|
7
|
-
end
|
8
|
-
|
1
|
+
require 'json'
|
9
2
|
|
10
3
|
module DataMapper
|
11
4
|
module Types
|
12
5
|
class Json < DataMapper::Type
|
13
|
-
primitive
|
14
|
-
|
6
|
+
primitive String
|
7
|
+
length 65535
|
8
|
+
lazy true
|
15
9
|
|
16
10
|
def self.load(value, property)
|
17
11
|
if value.nil?
|
@@ -19,7 +13,7 @@ module DataMapper
|
|
19
13
|
elsif value.is_a?(String)
|
20
14
|
::JSON.load(value)
|
21
15
|
else
|
22
|
-
raise ArgumentError
|
16
|
+
raise ArgumentError.new("+value+ of a property of JSON type must be nil or a String")
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
data/lib/dm-types/regexp.rb
CHANGED
@@ -8,12 +8,11 @@ module DataMapper
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.dump(value, property)
|
11
|
-
|
12
|
-
value.source
|
11
|
+
value.source unless value.nil?
|
13
12
|
end
|
14
13
|
|
15
14
|
def self.typecast(value, property)
|
16
|
-
|
15
|
+
load(value, property)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|
data/lib/dm-types/slug.rb
CHANGED
@@ -1,37 +1,28 @@
|
|
1
|
-
require '
|
1
|
+
require 'stringex'
|
2
2
|
|
3
3
|
module DataMapper
|
4
4
|
module Types
|
5
5
|
class Slug < DataMapper::Type
|
6
6
|
primitive String
|
7
|
-
|
7
|
+
length 65535
|
8
8
|
|
9
9
|
def self.load(value, property)
|
10
10
|
value
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.dump(value, property)
|
14
|
-
if value.nil?
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
escape(value)
|
14
|
+
return if value.nil?
|
15
|
+
|
16
|
+
if value.respond_to?(:to_str)
|
17
|
+
escape(value.to_str)
|
19
18
|
else
|
20
|
-
raise ArgumentError
|
19
|
+
raise ArgumentError, '+value+ must be nil or respond to #to_str'
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
24
|
-
# Hugs and kisses to Rick Olsons permalink_fu for the escape method
|
25
23
|
def self.escape(string)
|
26
|
-
|
27
|
-
result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
|
28
|
-
result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
|
29
|
-
result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
|
30
|
-
result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
|
31
|
-
result.downcase!
|
32
|
-
result
|
24
|
+
string.to_url
|
33
25
|
end
|
34
|
-
|
35
26
|
end # class Slug
|
36
27
|
end # module Types
|
37
28
|
end # module DataMapper
|
data/lib/dm-types/uri.rb
CHANGED
@@ -1,31 +1,25 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
|
3
|
-
gem 'addressable', '~>2.0.2'
|
4
1
|
require 'addressable/uri'
|
5
2
|
|
6
3
|
module DataMapper
|
7
4
|
module Types
|
8
5
|
class URI < DataMapper::Type
|
9
6
|
primitive String
|
7
|
+
length 2000
|
8
|
+
|
9
|
+
# Maximum length chosen based on recommendation:
|
10
|
+
# http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-an-url
|
10
11
|
|
11
12
|
def self.load(value, property)
|
12
13
|
Addressable::URI.parse(value)
|
13
14
|
end
|
14
15
|
|
15
16
|
def self.dump(value, property)
|
16
|
-
|
17
|
-
value.to_s
|
17
|
+
value.to_s unless value.nil?
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.typecast(value, property)
|
21
|
-
|
22
|
-
value
|
23
|
-
elsif value.nil?
|
24
|
-
load(nil, property)
|
25
|
-
else
|
26
|
-
load(value.to_s, property)
|
27
|
-
end
|
21
|
+
load(value, property)
|
28
22
|
end
|
29
23
|
end # class URI
|
30
|
-
end #
|
31
|
-
end #
|
24
|
+
end # module Types
|
25
|
+
end # module DataMapper
|
data/lib/dm-types/uuid.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
gem 'uuidtools', '~>1.0.7'
|
4
|
-
require 'uuidtools'
|
1
|
+
require 'uuidtools' # must be ~>2.0
|
5
2
|
|
6
3
|
module DataMapper
|
7
4
|
module Types
|
@@ -42,26 +39,23 @@ module DataMapper
|
|
42
39
|
#
|
43
40
|
class UUID < DataMapper::Type
|
44
41
|
primitive String
|
45
|
-
|
42
|
+
length 36
|
46
43
|
|
47
44
|
def self.load(value, property)
|
48
|
-
|
49
|
-
::UUID.parse(value)
|
45
|
+
UUIDTools::UUID.parse(value) unless value.nil?
|
50
46
|
end
|
51
47
|
|
52
48
|
def self.dump(value, property)
|
53
|
-
|
54
|
-
value.to_s
|
49
|
+
value.to_s unless value.nil?
|
55
50
|
end
|
56
51
|
|
57
52
|
def self.typecast(value, property)
|
58
|
-
value.kind_of?(::UUID)
|
53
|
+
if value.kind_of?(UUIDTools::UUID)
|
54
|
+
value
|
55
|
+
else
|
56
|
+
load(value, property)
|
57
|
+
end
|
59
58
|
end
|
60
|
-
|
61
|
-
#::DataMapper::Property::TYPES << self
|
62
|
-
#if defined? DataMapper::Adapters::PostgresAdapter
|
63
|
-
# DataMapper::Adapters::PostgresAdapter.type_map.map(self).to('UUID').with(:size => nil)
|
64
|
-
#end
|
65
59
|
end
|
66
60
|
end
|
67
61
|
end
|
data/lib/dm-types/version.rb
CHANGED
data/lib/dm-types/yaml.rb
CHANGED
@@ -3,8 +3,9 @@ require 'yaml'
|
|
3
3
|
module DataMapper
|
4
4
|
module Types
|
5
5
|
class Yaml < DataMapper::Type
|
6
|
-
primitive
|
7
|
-
|
6
|
+
primitive String
|
7
|
+
length 65535
|
8
|
+
lazy true
|
8
9
|
|
9
10
|
def self.load(value, property)
|
10
11
|
if value.nil?
|
@@ -12,7 +13,7 @@ module DataMapper
|
|
12
13
|
elsif value.is_a?(String)
|
13
14
|
::YAML.load(value)
|
14
15
|
else
|
15
|
-
raise ArgumentError.new("+value+ must be nil or a String")
|
16
|
+
raise ArgumentError.new("+value+ of a property of YAML type must be nil or a String")
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Types
|
3
|
+
module Fixtures
|
4
|
+
|
5
|
+
class Article
|
6
|
+
#
|
7
|
+
# Behaviors
|
8
|
+
#
|
9
|
+
|
10
|
+
include ::DataMapper::Resource
|
11
|
+
|
12
|
+
#
|
13
|
+
# Properties
|
14
|
+
#
|
15
|
+
|
16
|
+
property :id, Serial
|
17
|
+
|
18
|
+
property :title, String, :length => 255
|
19
|
+
property :body, Text
|
20
|
+
|
21
|
+
property :created_at, DateTime
|
22
|
+
property :updated_at, DateTime
|
23
|
+
property :published_at, DateTime
|
24
|
+
|
25
|
+
property :slug, Slug
|
26
|
+
|
27
|
+
#
|
28
|
+
# Hooks
|
29
|
+
#
|
30
|
+
|
31
|
+
before :valid? do
|
32
|
+
self.slug = self.title
|
33
|
+
end
|
34
|
+
|
35
|
+
auto_migrate!
|
36
|
+
end # Article
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Types
|
3
|
+
module Fixtures
|
4
|
+
|
5
|
+
class Bookmark
|
6
|
+
#
|
7
|
+
# Behaviors
|
8
|
+
#
|
9
|
+
|
10
|
+
include ::DataMapper::Resource
|
11
|
+
|
12
|
+
#
|
13
|
+
# Properties
|
14
|
+
#
|
15
|
+
|
16
|
+
property :id, Serial
|
17
|
+
|
18
|
+
property :title, String, :length => 255
|
19
|
+
property :shared, Boolean
|
20
|
+
property :uri, URI
|
21
|
+
property :tags, Yaml
|
22
|
+
|
23
|
+
auto_migrate!
|
24
|
+
end # Bookmark
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Types
|
3
|
+
module Fixtures
|
4
|
+
|
5
|
+
class NetworkNode
|
6
|
+
#
|
7
|
+
# Behaviors
|
8
|
+
#
|
9
|
+
|
10
|
+
include ::DataMapper::Resource
|
11
|
+
|
12
|
+
#
|
13
|
+
# Properties
|
14
|
+
#
|
15
|
+
|
16
|
+
property :id, Serial
|
17
|
+
property :ip_address, IPAddress
|
18
|
+
property :cidr_subnet_bits, Integer
|
19
|
+
property :node_uuid, UUID
|
20
|
+
|
21
|
+
#
|
22
|
+
# API
|
23
|
+
#
|
24
|
+
|
25
|
+
alias uuid node_uuid
|
26
|
+
alias uuid= node_uuid=
|
27
|
+
|
28
|
+
def runs_ipv6?
|
29
|
+
self.ip_address.ipv6?
|
30
|
+
end
|
31
|
+
|
32
|
+
def runs_ipv4?
|
33
|
+
self.ip_address.ipv4?
|
34
|
+
end
|
35
|
+
|
36
|
+
auto_migrate!
|
37
|
+
end # NetworkNode
|
38
|
+
end # Fixtures
|
39
|
+
end # Types
|
40
|
+
end # DataMapper
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Types
|
3
|
+
module Fixtures
|
4
|
+
|
5
|
+
class Person
|
6
|
+
#
|
7
|
+
# Behaviors
|
8
|
+
#
|
9
|
+
|
10
|
+
include DataMapper::Resource
|
11
|
+
|
12
|
+
#
|
13
|
+
# Properties
|
14
|
+
#
|
15
|
+
|
16
|
+
property :id, Serial
|
17
|
+
property :name, String
|
18
|
+
property :positions, Json
|
19
|
+
property :inventions, Yaml
|
20
|
+
|
21
|
+
property :interests, CommaSeparatedList
|
22
|
+
|
23
|
+
property :password, BCryptHash
|
24
|
+
|
25
|
+
auto_migrate!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Types
|
3
|
+
module Fixtures
|
4
|
+
|
5
|
+
class SoftwarePackage
|
6
|
+
#
|
7
|
+
# Behaviors
|
8
|
+
#
|
9
|
+
|
10
|
+
include ::DataMapper::Resource
|
11
|
+
|
12
|
+
#
|
13
|
+
# Properties
|
14
|
+
#
|
15
|
+
|
16
|
+
property :id, Serial
|
17
|
+
without_auto_validations do
|
18
|
+
property :node_number, Integer, :index => true
|
19
|
+
|
20
|
+
property :source_path, FilePath
|
21
|
+
property :destination_path, FilePath
|
22
|
+
|
23
|
+
property :product, String
|
24
|
+
property :version, String
|
25
|
+
property :released_at, DateTime
|
26
|
+
|
27
|
+
property :security_update, Boolean
|
28
|
+
|
29
|
+
property :installed_at, DateTime
|
30
|
+
property :installed_by, String
|
31
|
+
end
|
32
|
+
|
33
|
+
auto_migrate!
|
34
|
+
end # SoftwarePackage
|
35
|
+
end # Fixtures
|
36
|
+
end # Types
|
37
|
+
end # DataMapper
|