sbf-dm-types 1.3.0.beta

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.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +45 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +468 -0
  5. data/.travis.yml +52 -0
  6. data/Gemfile +67 -0
  7. data/LICENSE +20 -0
  8. data/README.rdoc +3 -0
  9. data/Rakefile +4 -0
  10. data/dm-types.gemspec +26 -0
  11. data/lib/dm-types/api_key.rb +30 -0
  12. data/lib/dm-types/bcrypt_hash.rb +33 -0
  13. data/lib/dm-types/comma_separated_list.rb +28 -0
  14. data/lib/dm-types/csv.rb +34 -0
  15. data/lib/dm-types/enum.rb +55 -0
  16. data/lib/dm-types/epoch_time.rb +35 -0
  17. data/lib/dm-types/file_path.rb +24 -0
  18. data/lib/dm-types/flag.rb +64 -0
  19. data/lib/dm-types/ip_address.rb +34 -0
  20. data/lib/dm-types/json.rb +48 -0
  21. data/lib/dm-types/paranoid/base.rb +56 -0
  22. data/lib/dm-types/paranoid_boolean.rb +23 -0
  23. data/lib/dm-types/paranoid_datetime.rb +22 -0
  24. data/lib/dm-types/regexp.rb +21 -0
  25. data/lib/dm-types/slug.rb +28 -0
  26. data/lib/dm-types/support/dirty_minder.rb +168 -0
  27. data/lib/dm-types/support/flags.rb +41 -0
  28. data/lib/dm-types/uri.rb +27 -0
  29. data/lib/dm-types/uuid.rb +64 -0
  30. data/lib/dm-types/version.rb +5 -0
  31. data/lib/dm-types/yaml.rb +39 -0
  32. data/lib/dm-types.rb +23 -0
  33. data/spec/fixtures/api_user.rb +14 -0
  34. data/spec/fixtures/article.rb +35 -0
  35. data/spec/fixtures/bookmark.rb +23 -0
  36. data/spec/fixtures/invention.rb +7 -0
  37. data/spec/fixtures/network_node.rb +36 -0
  38. data/spec/fixtures/person.rb +25 -0
  39. data/spec/fixtures/software_package.rb +33 -0
  40. data/spec/fixtures/ticket.rb +21 -0
  41. data/spec/fixtures/tshirt.rb +24 -0
  42. data/spec/integration/api_key_spec.rb +27 -0
  43. data/spec/integration/bcrypt_hash_spec.rb +47 -0
  44. data/spec/integration/comma_separated_list_spec.rb +87 -0
  45. data/spec/integration/dirty_minder_spec.rb +197 -0
  46. data/spec/integration/enum_spec.rb +78 -0
  47. data/spec/integration/epoch_time_spec.rb +61 -0
  48. data/spec/integration/file_path_spec.rb +160 -0
  49. data/spec/integration/flag_spec.rb +72 -0
  50. data/spec/integration/ip_address_spec.rb +153 -0
  51. data/spec/integration/json_spec.rb +72 -0
  52. data/spec/integration/slug_spec.rb +67 -0
  53. data/spec/integration/uri_spec.rb +117 -0
  54. data/spec/integration/uuid_spec.rb +102 -0
  55. data/spec/integration/yaml_spec.rb +87 -0
  56. data/spec/shared/flags_shared_spec.rb +37 -0
  57. data/spec/shared/identity_function_group.rb +5 -0
  58. data/spec/spec_helper.rb +30 -0
  59. data/spec/unit/bcrypt_hash_spec.rb +155 -0
  60. data/spec/unit/csv_spec.rb +142 -0
  61. data/spec/unit/dirty_minder_spec.rb +65 -0
  62. data/spec/unit/enum_spec.rb +126 -0
  63. data/spec/unit/epoch_time_spec.rb +74 -0
  64. data/spec/unit/file_path_spec.rb +75 -0
  65. data/spec/unit/flag_spec.rb +114 -0
  66. data/spec/unit/ip_address_spec.rb +109 -0
  67. data/spec/unit/json_spec.rb +126 -0
  68. data/spec/unit/paranoid_boolean_spec.rb +149 -0
  69. data/spec/unit/paranoid_datetime_spec.rb +153 -0
  70. data/spec/unit/regexp_spec.rb +63 -0
  71. data/spec/unit/uri_spec.rb +83 -0
  72. data/spec/unit/yaml_spec.rb +111 -0
  73. data/tasks/spec.rake +21 -0
  74. data/tasks/yard.rake +9 -0
  75. data/tasks/yardstick.rake +19 -0
  76. metadata +229 -0
@@ -0,0 +1,155 @@
1
+ require_relative '../spec_helper'
2
+
3
+ try_spec do
4
+ describe DataMapper::Property::BCryptHash do
5
+ before :all do
6
+ @clear_password = 'DataMapper R0cks!'
7
+ @crypted_password = BCrypt::Password.create(@clear_password)
8
+
9
+ @nonstandard_type = 1
10
+
11
+ class ::TestType
12
+ @a = 1
13
+ @b = 'Hi There'
14
+ end
15
+ @nonstandard_type2 = TestType.new
16
+
17
+ class ::User
18
+ include DataMapper::Resource
19
+ property :id, Serial
20
+ property :password, BCryptHash
21
+ end
22
+
23
+ @bcrypt_hash = User.properties[:password]
24
+ end
25
+
26
+ describe '.dump' do
27
+ describe 'when argument is a string' do
28
+ before :all do
29
+ @input = 'DataMapper'
30
+ @result = @bcrypt_hash.dump(@input)
31
+ end
32
+
33
+ it 'returns instance of String' do
34
+ expect(@result).to be_an_instance_of(String)
35
+ end
36
+
37
+ it 'returns a string that is 60 characters long' do
38
+ expect(@result.size).to eq 60
39
+ end
40
+ end
41
+
42
+ describe 'when argument is nil' do
43
+ before :all do
44
+ @input = nil
45
+ @result = @bcrypt_hash.dump(@input)
46
+ end
47
+
48
+ it 'returns nil' do
49
+ expect(@result).to be_nil
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '.load' do
55
+ describe 'when argument is a string' do
56
+ before :all do
57
+ @input = 'DataMapper'
58
+ @result = @bcrypt_hash.load(@crypted_password)
59
+ end
60
+
61
+ it 'returns instance of BCrypt::Password' do
62
+ expect(@result).to be_an_instance_of(BCrypt::Password)
63
+ end
64
+
65
+ it 'returns a string that matches original' do
66
+ expect(@result).to eq @clear_password
67
+ end
68
+ end
69
+
70
+ describe 'when argument is nil' do
71
+ before :all do
72
+ @input = nil
73
+ @result = @bcrypt_hash.load(@input)
74
+ end
75
+
76
+ it 'returns nil' do
77
+ expect(@result).to be_nil
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '.typecast' do
83
+ describe 'when argument is a string' do
84
+ before :all do
85
+ @input = 'bcrypt hash'
86
+ @result = @bcrypt_hash.typecast(@input)
87
+ end
88
+
89
+ it 'casts argument to BCrypt::Password' do
90
+ expect(@result).to be_an_instance_of(BCrypt::Password)
91
+ end
92
+
93
+ it 'casts argument to value that matches input' do
94
+ expect(@result).to eq @input
95
+ end
96
+ end
97
+
98
+ describe 'when argument is a blank string' do
99
+ before :all do
100
+ @input = ''
101
+ @result = @bcrypt_hash.typecast(@input)
102
+ end
103
+
104
+ it 'casts argument to BCrypt::Password' do
105
+ expect(@result).to be_an_instance_of(BCrypt::Password)
106
+ end
107
+
108
+ it 'casts argument to value that matches input' do
109
+ expect(@result).to eq @input
110
+ end
111
+ end
112
+
113
+ describe 'when argument is integer' do
114
+ before :all do
115
+ @input = 2249
116
+ @result = @bcrypt_hash.typecast(@input)
117
+ end
118
+
119
+ it 'casts argument to BCrypt::Password' do
120
+ expect(@result).to be_an_instance_of(BCrypt::Password)
121
+ end
122
+
123
+ it 'casts argument to value that matches input' do
124
+ expect(@result).to eq @input
125
+ end
126
+ end
127
+
128
+ describe 'when argument is hash' do
129
+ before :all do
130
+ @input = { :cryptic => 'obscure' }
131
+ @result = @bcrypt_hash.typecast(@input)
132
+ end
133
+
134
+ it 'casts argument to BCrypt::Password' do
135
+ expect(@result).to be_an_instance_of(BCrypt::Password)
136
+ end
137
+
138
+ it 'casts argument to value that matches input' do
139
+ expect(@result).to eq @input
140
+ end
141
+ end
142
+
143
+ describe 'when argument is nil' do
144
+ before :all do
145
+ @input = nil
146
+ @result = @bcrypt_hash.typecast(@input)
147
+ end
148
+
149
+ it 'returns nil' do
150
+ expect(@result).to be_nil
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,142 @@
1
+ require_relative '../spec_helper'
2
+
3
+ try_spec do
4
+ describe DataMapper::Property::Csv do
5
+ supported_by :all do
6
+ before :all do
7
+ class ::User
8
+ include DataMapper::Resource
9
+ property :id, Serial
10
+ property :things, Csv
11
+ end
12
+
13
+ @property = User.properties[:things]
14
+ end
15
+
16
+ describe '.load' do
17
+ describe 'when argument is a comma separated string' do
18
+ before :all do
19
+ @input = 'uno,due,tre'
20
+ @result = @property.load(@input)
21
+ end
22
+
23
+ it 'parses the argument using CVS parser' do
24
+ expect(@result).to eq [ %w[ uno due tre ] ]
25
+ end
26
+ end
27
+
28
+ describe 'when argument is an empty array' do
29
+ before :all do
30
+ @input = []
31
+ @result = @property.load(@input)
32
+ end
33
+
34
+ it 'does not change the input' do
35
+ expect(@result).to eq @input
36
+ end
37
+ end
38
+
39
+ describe 'when argument is an empty hash' do
40
+ before :all do
41
+ @input = {}
42
+ @result = @property.load(@input)
43
+ end
44
+
45
+ it 'returns nil' do
46
+ expect(@result).to be_nil
47
+ end
48
+ end
49
+
50
+ describe 'when argument is nil' do
51
+ before :all do
52
+ @input = nil
53
+ @result = @property.load(@input)
54
+ end
55
+
56
+ it 'returns nil' do
57
+ expect(@result).to be_nil
58
+ end
59
+ end
60
+
61
+ describe 'when argument is an integer' do
62
+ before :all do
63
+ @input = 7
64
+ @result = @property.load(@input)
65
+ end
66
+
67
+ it 'returns nil' do
68
+ expect(@result).to be_nil
69
+ end
70
+ end
71
+
72
+ describe 'when argument is a float' do
73
+ before :all do
74
+ @input = 7.0
75
+ @result = @property.load(@input)
76
+ end
77
+
78
+ it 'returns nil' do
79
+ expect(@result).to be_nil
80
+ end
81
+ end
82
+
83
+ describe 'when argument is an array' do
84
+ before :all do
85
+ @input = [ 1, 2, 3 ]
86
+ @result = @property.load(@input)
87
+ end
88
+
89
+ it 'returns input as is' do
90
+ expect(@result).to eql(@input)
91
+ end
92
+ end
93
+ end
94
+
95
+ describe '.dump' do
96
+ describe 'when value is a list of lists' do
97
+ before :all do
98
+ @input = [ %w[ uno due tre ], %w[ uno dos tres ] ]
99
+ @result = @property.dump(@input)
100
+ end
101
+
102
+ it 'dumps value to comma separated string' do
103
+ expect(@result).to eq "uno,due,tre\nuno,dos,tres\n"
104
+ end
105
+ end
106
+
107
+ describe 'when value is a string' do
108
+ before :all do
109
+ @input = 'beauty hides in the deep'
110
+ @result = @property.dump(@input)
111
+ end
112
+
113
+ it 'returns input as is' do
114
+ expect(@result).to eq @input
115
+ end
116
+ end
117
+
118
+ describe 'when value is nil' do
119
+ before :all do
120
+ @input = nil
121
+ @result = @property.dump(@input)
122
+ end
123
+
124
+ it 'returns nil' do
125
+ expect(@result).to be_nil
126
+ end
127
+ end
128
+
129
+ describe 'when value is a hash' do
130
+ before :all do
131
+ @input = { :library => 'DataMapper', :language => 'Ruby' }
132
+ @result = @property.dump(@input)
133
+ end
134
+
135
+ it 'returns nil' do
136
+ expect(@result).to be_nil
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,65 @@
1
+ require_relative '../spec_helper'
2
+ require 'dm-types/support/dirty_minder'
3
+
4
+ describe DataMapper::Property::DirtyMinder, 'set!' do
5
+ let(:property_class) do
6
+ Class.new(DataMapper::Property::Object) do
7
+ include DataMapper::Property::DirtyMinder
8
+ end
9
+ end
10
+
11
+ let(:model) do
12
+ property_class = self.property_class
13
+ Class.new do
14
+ include DataMapper::Resource
15
+ property :id, DataMapper::Property::Serial
16
+ property :name, property_class
17
+
18
+ def self.name
19
+ 'FredsClass'
20
+ end
21
+ end
22
+ end
23
+
24
+ let(:resource) { model.new }
25
+
26
+ let(:object) { model.properties[:name] }
27
+
28
+ subject { object.set!(resource, value) }
29
+
30
+ shared_examples 'a non hooked value' do
31
+ it 'does not extend value with hook' do
32
+ expect(value).not_to be_kind_of(DataMapper::Property::DirtyMinder::Hooker)
33
+ end
34
+ end
35
+
36
+ shared_examples 'a hooked value' do
37
+ it 'extends value with hook' do
38
+ expect(value).to be_kind_of(DataMapper::Property::DirtyMinder::Hooker)
39
+ end
40
+ end
41
+
42
+ before do
43
+ subject
44
+ end
45
+
46
+ context 'when setting nil' do
47
+ let(:value) { nil }
48
+ it_behaves_like 'a non hooked value'
49
+ end
50
+
51
+ context 'when setting a String' do
52
+ let(:value) { 'The fred' }
53
+ it_behaves_like 'a non hooked value'
54
+ end
55
+
56
+ context 'when setting an Array' do
57
+ let(:value) { ['The fred'] }
58
+ it_behaves_like 'a hooked value'
59
+ end
60
+
61
+ context 'when setting a Hash' do
62
+ let(:value) { {'The' => 'fred'} }
63
+ it_behaves_like 'a hooked value'
64
+ end
65
+ end
@@ -0,0 +1,126 @@
1
+ require_relative '../spec_helper'
2
+
3
+ try_spec do
4
+ describe DataMapper::Property::Enum do
5
+ before :all do
6
+ class ::User
7
+ include DataMapper::Resource
8
+ property :id, Serial
9
+ end
10
+
11
+ @property_klass = DataMapper::Property::Enum
12
+ end
13
+
14
+ it_behaves_like 'A property with flags'
15
+
16
+ describe '.dump' do
17
+ before do
18
+ @enum = User.property(:enum, DataMapper::Property::Enum[:first, :second, :third])
19
+ end
20
+
21
+ it 'returns the key of the value match from the flag map' do
22
+ expect(@enum.dump(:first)).to eq 1
23
+ expect(@enum.dump(:second)).to eq 2
24
+ expect(@enum.dump(:third)).to eq 3
25
+ end
26
+
27
+ describe 'when there is no match' do
28
+ it 'returns nil' do
29
+ expect(@enum.dump(:zero)).to be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '.load' do
35
+ before do
36
+ @enum = User.property(:enum, DataMapper::Property::Enum, :flags => [:uno, :dos, :tres])
37
+ end
38
+
39
+ it 'returns the value of the key match from the flag map' do
40
+ expect(@enum.load(1)).to eq :uno
41
+ expect(@enum.load(2)).to eq :dos
42
+ expect(@enum.load(3)).to eq :tres
43
+ end
44
+
45
+ describe 'when there is no key' do
46
+ it 'returns nil' do
47
+ expect(@enum.load(-1)).to be_nil
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '.typecast' do
53
+ describe 'of Enum created from a symbol' do
54
+ before :all do
55
+ @enum = User.property(:enum, DataMapper::Property::Enum, :flags => [:uno])
56
+ end
57
+
58
+ describe 'when given a symbol' do
59
+ it 'uses Enum type' do
60
+ expect(@enum.typecast(:uno)).to eq :uno
61
+ end
62
+ end
63
+
64
+ describe 'when given a string' do
65
+ it 'uses Enum type' do
66
+ expect(@enum.typecast('uno')).to eq :uno
67
+ end
68
+ end
69
+
70
+ describe 'when given nil' do
71
+ it 'returns nil' do
72
+ expect(@enum.typecast( nil)).to eq nil
73
+ end
74
+ end
75
+ end
76
+
77
+ describe 'of Enum created from integer list' do
78
+ before :all do
79
+ @enum = User.property(:enum, DataMapper::Property::Enum, :flags => [1, 2, 3])
80
+ end
81
+
82
+ describe 'when given an integer' do
83
+ it 'uses Enum type' do
84
+ expect(@enum.typecast(1)).to eq 1
85
+ end
86
+ end
87
+
88
+ describe 'when given a float' do
89
+ it 'uses Enum type' do
90
+ expect(@enum.typecast(1.1)).to eq 1
91
+ end
92
+ end
93
+
94
+ describe 'when given nil' do
95
+ it 'returns nil' do
96
+ expect(@enum.typecast( nil)).to eq nil
97
+ end
98
+ end
99
+ end
100
+
101
+ describe 'of Enum created from a string' do
102
+ before :all do
103
+ @enum = User.property(:enum, DataMapper::Property::Enum, :flags => ['uno'])
104
+ end
105
+
106
+ describe 'when given a symbol' do
107
+ it 'uses Enum type' do
108
+ expect(@enum.typecast(:uno)).to eq 'uno'
109
+ end
110
+ end
111
+
112
+ describe 'when given a string' do
113
+ it 'uses Enum type' do
114
+ expect(@enum.typecast('uno')).to eq 'uno'
115
+ end
116
+ end
117
+
118
+ describe 'when given nil' do
119
+ it 'returns nil' do
120
+ expect(@enum.typecast( nil)).to eq nil
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,74 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe DataMapper::Property::EpochTime do
4
+ before :all do
5
+ class ::User
6
+ include DataMapper::Resource
7
+
8
+ property :id, Serial
9
+ property :bday, EpochTime
10
+ end
11
+
12
+ @property = User.properties[:bday]
13
+ end
14
+
15
+ describe '#dump' do
16
+ subject { @property.dump(value) }
17
+
18
+ describe 'with a Time instance' do
19
+ let(:value) { Time.now }
20
+
21
+ it { is_expected.to eq value.to_i }
22
+ end
23
+
24
+ describe 'with nil' do
25
+ let(:value) { nil }
26
+
27
+ it { is_expected.to eq value }
28
+ end
29
+ end
30
+
31
+ describe '#typecast' do
32
+ subject { @property.typecast(value) }
33
+
34
+ describe 'with a DateTime instance' do
35
+ let(:value) { DateTime.now }
36
+
37
+ it { is_expected.to eq Time.parse(value.to_s) }
38
+ end
39
+
40
+ describe 'with a number' do
41
+ let(:value) { Time.now.to_i }
42
+
43
+ it { is_expected.to eq ::Time.at(value) }
44
+ end
45
+
46
+ describe 'with a numeric string' do
47
+ let(:value) { Time.now.to_i.to_s }
48
+
49
+ it { is_expected.to eq ::Time.at(value.to_i) }
50
+ end
51
+
52
+ describe 'with a DateTime string' do
53
+ let(:value) { '2011-07-11 15:00:04 UTC' }
54
+
55
+ it { is_expected.to eq ::Time.parse(value) }
56
+ end
57
+ end
58
+
59
+ describe '#load' do
60
+ subject { @property.load(value) }
61
+
62
+ describe 'with a number' do
63
+ let(:value) { Time.now.to_i }
64
+
65
+ it { is_expected.to eq Time.at(value) }
66
+ end
67
+
68
+ describe 'with nil' do
69
+ let(:value) { nil }
70
+
71
+ it { is_expected.to eq value }
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,75 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/software_package'
3
+
4
+ try_spec do
5
+ describe DataMapper::Property::FilePath do
6
+ before :all do
7
+ @property = DataMapper::TypesFixtures::SoftwarePackage.properties[:source_path]
8
+ end
9
+
10
+ before do
11
+ @input = '/usr/bin/ruby'
12
+ @path = Pathname.new(@input)
13
+ end
14
+
15
+ describe '.dump' do
16
+ describe 'when input is a string' do
17
+ it 'does not modify input' do
18
+ expect(@property.dump(@input)).to eq @input
19
+ end
20
+ end
21
+
22
+ describe 'when input is nil' do
23
+ it 'returns nil' do
24
+ expect(@property.dump(nil)).to be_nil
25
+ end
26
+ end
27
+
28
+ describe 'when input is a blank string' do
29
+ it 'returns nil' do
30
+ expect(@property.dump('')).to be_nil
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '.load' do
36
+ describe 'when value is a non-blank file path' do
37
+ it 'returns Pathname for a path' do
38
+ expect(@property.load(@input)).to eq @path
39
+ end
40
+ end
41
+
42
+ describe 'when value is nil' do
43
+ it 'return nil' do
44
+ expect(@property.load(nil)).to be_nil
45
+ end
46
+ end
47
+
48
+ describe 'when value is a blank string' do
49
+ it 'returns nil' do
50
+ expect(@property.load('')).to be_nil
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '.typecast' do
56
+ describe 'when a Pathname is given' do
57
+ it 'does not modify input' do
58
+ expect(@property.typecast(@path)).to eq @path
59
+ end
60
+ end
61
+
62
+ describe 'when a nil is given' do
63
+ it 'does not modify input' do
64
+ expect(@property.typecast(nil)).to eq nil
65
+ end
66
+ end
67
+
68
+ describe 'when a string is given' do
69
+ it 'returns Pathname for given path' do
70
+ expect(@property.typecast(@input)).to eq @path
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end