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/spec/unit/csv_spec.rb
CHANGED
@@ -1,35 +1,119 @@
|
|
1
|
-
require '
|
2
|
-
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
try_spec do
|
4
|
+
describe DataMapper::Types::Csv do
|
5
|
+
describe '.load' do
|
6
|
+
describe 'when argument is a comma separated string' do
|
7
|
+
before :all do
|
8
|
+
@input = 'uno,due,tre'
|
9
|
+
@result = DataMapper::Types::Csv.load(@input, :property)
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
it 'parses the argument using CVS parser' do
|
13
|
+
@result.should == [ %w[ uno due tre ] ]
|
14
|
+
end
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
17
|
+
describe 'when argument is an empty array' do
|
18
|
+
before :all do
|
19
|
+
@input = []
|
20
|
+
@result = DataMapper::Types::Csv.load(@input, :property)
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
it 'does not change the input' do
|
24
|
+
@result.should == @input
|
25
|
+
end
|
26
|
+
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
describe 'when argument is an empty hash' do
|
29
|
+
before :all do
|
30
|
+
@input = {}
|
31
|
+
@result = DataMapper::Types::Csv.load(@input, :property)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns nil' do
|
35
|
+
@result.should be_nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'when argument is nil' do
|
40
|
+
before :all do
|
41
|
+
@input = nil
|
42
|
+
@result = DataMapper::Types::Csv.load(@input, :property)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns nil' do
|
46
|
+
@result.should be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'when argument is an integer' do
|
51
|
+
before :all do
|
52
|
+
@input = 7
|
53
|
+
@result = DataMapper::Types::Csv.load(@input, :property)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns nil' do
|
57
|
+
@result.should be_nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'when argument is a float' do
|
62
|
+
before :all do
|
63
|
+
@input = 7.0
|
64
|
+
@result = DataMapper::Types::Csv.load(@input, :property)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns nil' do
|
68
|
+
@result.should be_nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '.dump' do
|
74
|
+
describe 'when value is a list of lists' do
|
75
|
+
before :all do
|
76
|
+
@input = [ %w[ uno due tre ], %w[ uno dos tres ] ]
|
77
|
+
@result = DataMapper::Types::Csv.dump(@input, :property)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'dumps value to comma separated string' do
|
81
|
+
@result.should == "uno,due,tre\nuno,dos,tres\n"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'when value is a string' do
|
86
|
+
before :all do
|
87
|
+
@input = 'beauty hides in the deep'
|
88
|
+
@result = DataMapper::Types::Csv.dump(@input, :property)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'returns input as is' do
|
92
|
+
@result.should == @input
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe 'when value is nil' do
|
97
|
+
before :all do
|
98
|
+
@input = nil
|
99
|
+
@result = DataMapper::Types::Csv.dump(@input, :property)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'returns nil' do
|
103
|
+
@result.should be_nil
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe 'when value is a hash' do
|
108
|
+
before :all do
|
109
|
+
@input = { :library => 'DataMapper', :language => 'Ruby' }
|
110
|
+
@result = DataMapper::Types::Csv.dump(@input, :property)
|
111
|
+
end
|
30
112
|
|
31
|
-
|
32
|
-
|
33
|
-
|
113
|
+
it 'returns nil' do
|
114
|
+
@result.should be_nil
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
34
118
|
end
|
35
119
|
end
|
data/spec/unit/enum_spec.rb
CHANGED
@@ -1,96 +1,137 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
describe DataMapper::Types::Enum do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe ".[]" do
|
29
|
-
it "should be an alias for the new method" do
|
30
|
-
DataMapper::Types::Enum.should_receive(:new).with(:uno, :dos, :tres)
|
31
|
-
DataMapper::Types::Enum[:uno, :dos, :tres]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe ".dump" do
|
36
|
-
before(:each) do
|
37
|
-
@enum = DataMapper::Types::Enum[:first, :second, :third]
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should return the key of the value match from the flag map" do
|
41
|
-
@enum.dump(:first, :property).should == 1
|
42
|
-
@enum.dump(:second, :property).should == 2
|
43
|
-
@enum.dump(:third, :property).should == 3
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should return nil if there is no match" do
|
47
|
-
@enum.dump(:zero, :property).should be_nil
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe ".load" do
|
52
|
-
before(:each) do
|
53
|
-
@enum = DataMapper::Types::Enum[:uno, :dos, :tres]
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
try_spec do
|
4
|
+
describe DataMapper::Types::Enum do
|
5
|
+
describe '.new' do
|
6
|
+
it 'returns a Class' do
|
7
|
+
DataMapper::Types::Enum.new.should be_instance_of(Class)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns an unique Class each time' do
|
11
|
+
DataMapper::Types::Enum.new.should_not == DataMapper::Types::Enum.new
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'stores enumeration items' do
|
15
|
+
DataMapper::Types::Enum.new(:first, :second, :third).flag_map.values.should == [:first, :second, :third]
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'assigns incremental numeric keys to enumeration items' do
|
19
|
+
DataMapper::Types::Enum.new(:one, :two, :three, :four).flag_map.keys.should == (1..4).to_a
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'uses Integer as a primitive' do
|
23
|
+
DataMapper::Types::Enum.new.primitive.should == Integer
|
24
|
+
end
|
54
25
|
end
|
55
26
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
27
|
+
describe '.dump' do
|
28
|
+
before do
|
29
|
+
@enum = DataMapper::Types::Enum[:first, :second, :third]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return the key of the value match from the flag map' do
|
33
|
+
@enum.dump(:first, :property).should == 1
|
34
|
+
@enum.dump(:second, :property).should == 2
|
35
|
+
@enum.dump(:third, :property).should == 3
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'when there is no match' do
|
39
|
+
it 'should return nil' do
|
40
|
+
@enum.dump(:zero, :property).should be_nil
|
41
|
+
end
|
42
|
+
end
|
60
43
|
end
|
61
44
|
|
62
|
-
|
63
|
-
|
45
|
+
describe '.load' do
|
46
|
+
before do
|
47
|
+
@enum = DataMapper::Types::Enum[:uno, :dos, :tres]
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'returns the value of the key match from the flag map' do
|
51
|
+
@enum.load(1, :property).should == :uno
|
52
|
+
@enum.load(2, :property).should == :dos
|
53
|
+
@enum.load(3, :property).should == :tres
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'when there is no key' do
|
57
|
+
it 'returns nil' do
|
58
|
+
@enum.load(-1, :property).should be_nil
|
59
|
+
end
|
60
|
+
end
|
64
61
|
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe ".typecast" do
|
68
|
-
it 'should attempt to use the Enum type' do
|
69
|
-
# Symbol.
|
70
|
-
@sym_enum = DataMapper::Types::Enum[:uno]
|
71
|
-
@sym_enum.typecast(:uno, :property).should == :uno
|
72
|
-
@sym_enum.typecast("uno", :property).should == :uno
|
73
|
-
|
74
|
-
# String
|
75
|
-
@str_enum = DataMapper::Types::Enum["uno"]
|
76
|
-
@str_enum.typecast(:uno, :property).should == "uno"
|
77
|
-
@str_enum.typecast("uno", :property).should == "uno"
|
78
|
-
|
79
|
-
# Integer
|
80
|
-
@int_enum = DataMapper::Types::Enum[1, 2, 3]
|
81
|
-
@int_enum.typecast(1, :property).should == 1
|
82
|
-
@int_enum.typecast(1.1, :property).should == 1
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should not throw an error when value is nil" do
|
86
|
-
@sym_enum = DataMapper::Types::Enum[:uno]
|
87
|
-
@sym_enum.typecast( nil, :property).should == nil
|
88
|
-
|
89
|
-
@str_enum = DataMapper::Types::Enum["uno"]
|
90
|
-
@str_enum.typecast( nil, :property).should == nil
|
91
62
|
|
92
|
-
|
93
|
-
|
63
|
+
describe '.typecast' do
|
64
|
+
describe 'of Enum created from a symbol' do
|
65
|
+
before :all do
|
66
|
+
@enum = DataMapper::Types::Enum[:uno]
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'when given a symbol' do
|
70
|
+
it 'uses Enum type' do
|
71
|
+
@enum.typecast(:uno, :property).should == :uno
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'when given a string' do
|
76
|
+
it 'uses Enum type' do
|
77
|
+
@enum.typecast('uno', :property).should == :uno
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'when given nil' do
|
82
|
+
it 'returns nil' do
|
83
|
+
@enum.typecast( nil, :property).should == nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'of Enum created from integer list' do
|
89
|
+
before :all do
|
90
|
+
@enum = DataMapper::Types::Enum[1, 2, 3]
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'when given an integer' do
|
94
|
+
it 'uses Enum type' do
|
95
|
+
@enum.typecast(1, :property).should == 1
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe 'when given a float' do
|
100
|
+
it 'uses Enum type' do
|
101
|
+
@enum.typecast(1.1, :property).should == 1
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'when given nil' do
|
106
|
+
it 'returns nil' do
|
107
|
+
@enum.typecast( nil, :property).should == nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe 'of Enum created from a string' do
|
113
|
+
before :all do
|
114
|
+
@enum = DataMapper::Types::Enum['uno']
|
115
|
+
end
|
116
|
+
|
117
|
+
describe 'when given a symbol' do
|
118
|
+
it 'uses Enum type' do
|
119
|
+
@enum.typecast(:uno, :property).should == 'uno'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'when given a string' do
|
124
|
+
it 'uses Enum type' do
|
125
|
+
@enum.typecast('uno', :property).should == 'uno'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'when given nil' do
|
130
|
+
it 'returns nil' do
|
131
|
+
@enum.typecast( nil, :property).should == nil
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
94
135
|
end
|
95
136
|
end
|
96
137
|
end
|
@@ -1,37 +1,62 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
describe DataMapper::Types::EpochTime do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
try_spec do
|
4
|
+
describe DataMapper::Types::EpochTime do
|
5
|
+
describe '.dump' do
|
6
|
+
describe 'when given Time instance' do
|
7
|
+
before :all do
|
8
|
+
@input = Time.now
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns timestamp' do
|
12
|
+
DataMapper::Types::EpochTime.dump(@input, :property).should == @input.to_i
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'when given DateTime instance' do
|
17
|
+
before :all do
|
18
|
+
@input = DateTime.now
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns timestamp' do
|
22
|
+
DataMapper::Types::EpochTime.dump(@input, :property).should == Time.parse(@input.to_s).to_i
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'when given an integer' do
|
27
|
+
before :all do
|
28
|
+
@input = Time.now.to_i
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns value as is' do
|
32
|
+
DataMapper::Types::EpochTime.dump(@input, :property).should == @input
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'when given nil' do
|
37
|
+
before :all do
|
38
|
+
@input = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'returns value as is' do
|
42
|
+
DataMapper::Types::EpochTime.dump(@input, :property).should == @input
|
43
|
+
end
|
44
|
+
end
|
30
45
|
end
|
31
46
|
|
32
|
-
|
33
|
-
|
34
|
-
|
47
|
+
describe '.load' do
|
48
|
+
describe 'when value is nil' do
|
49
|
+
it 'returns nil' do
|
50
|
+
DataMapper::Types::EpochTime.load(nil, :property).should == nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'when value is an integer' do
|
55
|
+
it 'returns time object from timestamp' do
|
56
|
+
t = Time.now.to_i
|
57
|
+
DataMapper::Types::EpochTime.load(Time.now.to_i, :property).should == Time.at(t)
|
58
|
+
end
|
59
|
+
end
|
35
60
|
end
|
36
61
|
end
|
37
62
|
end
|