dm-types 0.10.2 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.gitignore +36 -0
  2. data/Gemfile +147 -0
  3. data/Rakefile +7 -8
  4. data/VERSION +1 -1
  5. data/dm-types.gemspec +61 -20
  6. data/lib/dm-types.rb +24 -19
  7. data/lib/dm-types/bcrypt_hash.rb +17 -13
  8. data/lib/dm-types/comma_separated_list.rb +11 -16
  9. data/lib/dm-types/csv.rb +11 -11
  10. data/lib/dm-types/enum.rb +33 -50
  11. data/lib/dm-types/epoch_time.rb +11 -11
  12. data/lib/dm-types/file_path.rb +13 -10
  13. data/lib/dm-types/flag.rb +17 -25
  14. data/lib/dm-types/ip_address.rb +15 -11
  15. data/lib/dm-types/json.rb +17 -14
  16. data/lib/dm-types/paranoid/base.rb +38 -0
  17. data/lib/dm-types/paranoid_boolean.rb +23 -0
  18. data/lib/dm-types/paranoid_datetime.rb +22 -0
  19. data/lib/dm-types/regexp.rb +8 -8
  20. data/lib/dm-types/slug.rb +7 -12
  21. data/lib/dm-types/uri.rb +21 -9
  22. data/lib/dm-types/uuid.rb +18 -11
  23. data/lib/dm-types/yaml.rb +12 -10
  24. data/spec/fixtures/article.rb +0 -2
  25. data/spec/fixtures/bookmark.rb +0 -2
  26. data/spec/fixtures/network_node.rb +0 -2
  27. data/spec/fixtures/person.rb +0 -2
  28. data/spec/fixtures/software_package.rb +0 -2
  29. data/spec/fixtures/ticket.rb +2 -4
  30. data/spec/fixtures/tshirt.rb +3 -5
  31. data/spec/integration/bcrypt_hash_spec.rb +33 -31
  32. data/spec/integration/comma_separated_list_spec.rb +55 -53
  33. data/spec/integration/enum_spec.rb +55 -53
  34. data/spec/integration/file_path_spec.rb +105 -103
  35. data/spec/integration/flag_spec.rb +42 -40
  36. data/spec/integration/ip_address_spec.rb +91 -89
  37. data/spec/integration/json_spec.rb +41 -39
  38. data/spec/integration/slug_spec.rb +36 -34
  39. data/spec/integration/uri_spec.rb +82 -79
  40. data/spec/integration/uuid_spec.rb +63 -61
  41. data/spec/integration/yaml_spec.rb +37 -35
  42. data/spec/spec_helper.rb +7 -36
  43. data/spec/unit/bcrypt_hash_spec.rb +18 -10
  44. data/spec/unit/csv_spec.rb +92 -80
  45. data/spec/unit/enum_spec.rb +27 -42
  46. data/spec/unit/epoch_time_spec.rb +18 -7
  47. data/spec/unit/file_path_spec.rb +15 -10
  48. data/spec/unit/flag_spec.rb +13 -36
  49. data/spec/unit/ip_address_spec.rb +13 -10
  50. data/spec/unit/json_spec.rb +21 -14
  51. data/spec/unit/paranoid_boolean_spec.rb +138 -0
  52. data/spec/unit/paranoid_datetime_spec.rb +143 -0
  53. data/spec/unit/regexp_spec.rb +15 -5
  54. data/spec/unit/uri_spec.rb +13 -9
  55. data/spec/unit/yaml_spec.rb +16 -9
  56. data/tasks/local_gemfile.rake +18 -0
  57. data/tasks/spec.rake +0 -3
  58. metadata +122 -52
@@ -2,24 +2,31 @@ require 'spec_helper'
2
2
  require 'shared/identity_function_group'
3
3
 
4
4
  try_spec do
5
- describe DataMapper::Types::Json do
5
+
6
+ require './spec/fixtures/person'
7
+
8
+ describe DataMapper::Property::Json do
9
+ before :all do
10
+ @property = DataMapper::Types::Fixtures::Person.properties[:positions]
11
+ end
12
+
6
13
  describe '.load' do
7
14
  describe 'when nil is provided' do
8
15
  it 'returns nil' do
9
- DataMapper::Types::Json.load(nil, :property).should be_nil
16
+ @property.load(nil).should be_nil
10
17
  end
11
18
  end
12
19
 
13
20
  describe 'when Json encoded primitive string is provided' do
14
21
  it 'returns decoded value as Ruby string' do
15
- DataMapper::Types::Json.load(JSON.dump(:value => 'JSON encoded string'), :property).should == { 'value' => 'JSON encoded string' }
22
+ @property.load(JSON.dump(:value => 'JSON encoded string')).should == { 'value' => 'JSON encoded string' }
16
23
  end
17
24
  end
18
25
 
19
26
  describe 'when something else is provided' do
20
27
  it 'raises ArgumentError with a meaningful message' do
21
28
  lambda {
22
- DataMapper::Types::Json.load(:sym, :property)
29
+ @property.load(:sym)
23
30
  }.should raise_error(ArgumentError, '+value+ of a property of JSON type must be nil or a String')
24
31
  end
25
32
  end
@@ -28,31 +35,31 @@ try_spec do
28
35
  describe '.dump' do
29
36
  describe 'when nil is provided' do
30
37
  it 'returns nil' do
31
- DataMapper::Types::Json.dump(nil, :property).should be_nil
38
+ @property.dump(nil).should be_nil
32
39
  end
33
40
  end
34
41
 
35
42
  describe 'when Json encoded primitive string is provided' do
36
43
  it 'does not do double encoding' do
37
- DataMapper::Types::Json.dump('Json encoded string', :property).should == 'Json encoded string'
44
+ @property.dump('Json encoded string').should == 'Json encoded string'
38
45
  end
39
46
  end
40
47
 
41
48
  describe 'when regular Ruby string is provided' do
42
49
  it 'dumps argument to Json' do
43
- DataMapper::Types::Json.dump('dump me (to JSON)', :property).should == 'dump me (to JSON)'
50
+ @property.dump('dump me (to JSON)').should == 'dump me (to JSON)'
44
51
  end
45
52
  end
46
53
 
47
54
  describe 'when Ruby array is provided' do
48
55
  it 'dumps argument to Json' do
49
- DataMapper::Types::Json.dump([1, 2, 3], :property).should == '[1,2,3]'
56
+ @property.dump([1, 2, 3]).should == '[1,2,3]'
50
57
  end
51
58
  end
52
59
 
53
60
  describe 'when Ruby hash is provided' do
54
61
  it 'dumps argument to Json' do
55
- DataMapper::Types::Json.dump({ :datamapper => 'Data access layer in Ruby' }, :property).
62
+ @property.dump({ :datamapper => 'Data access layer in Ruby' }).
56
63
  should == '{"datamapper":"Data access layer in Ruby"}'
57
64
  end
58
65
  end
@@ -67,7 +74,7 @@ try_spec do
67
74
  before :all do
68
75
  @input = { :library => 'DataMapper' }
69
76
 
70
- @result = DataMapper::Types::Json.typecast(@input, :property)
77
+ @result = @property.typecast(@input)
71
78
  end
72
79
 
73
80
  it_should_behave_like 'identity function'
@@ -77,7 +84,7 @@ try_spec do
77
84
  before :all do
78
85
  @input = %w[ dm-core dm-more ]
79
86
 
80
- @result = DataMapper::Types::Json.typecast(@input, :property)
87
+ @result = @property.typecast(@input)
81
88
  end
82
89
 
83
90
  it_should_behave_like 'identity function'
@@ -87,7 +94,7 @@ try_spec do
87
94
  before :all do
88
95
  @input = nil
89
96
 
90
- @result = DataMapper::Types::Json.typecast(@input, :property)
97
+ @result = @property.typecast(@input)
91
98
  end
92
99
 
93
100
  it_should_behave_like 'identity function'
@@ -97,7 +104,7 @@ try_spec do
97
104
  before :all do
98
105
  @input = '{ "value": 11 }'
99
106
 
100
- @result = DataMapper::Types::Json.typecast(@input, :property)
107
+ @result = @property.typecast(@input)
101
108
  end
102
109
 
103
110
  it 'decodes value from JSON' do
@@ -110,7 +117,7 @@ try_spec do
110
117
  @input = SerializeMe.new
111
118
  @input.name = 'Hello!'
112
119
 
113
- # @result = DataMapper::Types::Json.typecast(@input, :property)
120
+ # @result = @property.typecast(@input)
114
121
  end
115
122
 
116
123
  it 'attempts to load value from JSON string'
@@ -0,0 +1,138 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe DataMapper::Property::ParanoidBoolean do
4
+ before :all do
5
+ Object.send(:remove_const, :Blog) if defined?(Blog)
6
+ module ::Blog
7
+ class Article
8
+ include DataMapper::Resource
9
+
10
+ property :id, Serial
11
+ property :deleted, ParanoidBoolean
12
+
13
+ before :destroy, :before_destroy
14
+
15
+ def before_destroy; end
16
+ end
17
+ end
18
+
19
+ @model = Blog::Article
20
+ end
21
+
22
+ supported_by :all do
23
+ describe 'Resource#destroy' do
24
+ subject { @resource.destroy }
25
+
26
+ describe 'with a new resource' do
27
+ before do
28
+ @resource = @model.new
29
+ end
30
+
31
+ it { should be_false }
32
+
33
+ it 'should not delete the resource from the datastore' do
34
+ method(:subject).should_not change { @model.with_deleted.size }.from(0)
35
+ end
36
+
37
+ it 'should not set the paranoid column' do
38
+ method(:subject).should_not change { @resource.deleted }.from(false)
39
+ end
40
+
41
+ it 'should run the destroy hook' do
42
+ @resource.should_receive(:before_destroy).with(no_args)
43
+ subject
44
+ end
45
+ end
46
+
47
+ describe 'with a saved resource' do
48
+ before do
49
+ @resource = @model.create
50
+ end
51
+
52
+ it { should be_true }
53
+
54
+ it 'should not delete the resource from the datastore' do
55
+ method(:subject).should_not change { @model.with_deleted.size }.from(1)
56
+ end
57
+
58
+ it 'should set the paranoid column' do
59
+ method(:subject).should change { @resource.deleted }.from(false).to(true)
60
+ end
61
+
62
+ it 'should run the destroy hook' do
63
+ @resource.should_receive(:before_destroy).with(no_args)
64
+ subject
65
+ end
66
+ end
67
+ end
68
+
69
+ describe 'Resource#destroy!' do
70
+ subject { @resource.destroy! }
71
+
72
+ describe 'with a new resource' do
73
+ before do
74
+ @resource = @model.new
75
+ end
76
+
77
+ it { should be_false }
78
+
79
+ it 'should not delete the resource from the datastore' do
80
+ method(:subject).should_not change { @model.with_deleted.size }.from(0)
81
+ end
82
+
83
+ it 'should not set the paranoid column' do
84
+ method(:subject).should_not change { @resource.deleted }.from(false)
85
+ end
86
+
87
+ it 'should not run the destroy hook' do
88
+ @resource.should_not_receive(:before_destroy).with(no_args)
89
+ subject
90
+ end
91
+ end
92
+
93
+ describe 'with a saved resource' do
94
+ before do
95
+ @resource = @model.create
96
+ end
97
+
98
+ it { should be_true }
99
+
100
+ it 'should delete the resource from the datastore' do
101
+ method(:subject).should change { @model.with_deleted.size }.from(1).to(0)
102
+ end
103
+
104
+ it 'should not set the paranoid column' do
105
+ method(:subject).should_not change { @resource.deleted }.from(false)
106
+ end
107
+
108
+ it 'should not run the destroy hook' do
109
+ @resource.should_not_receive(:before_destroy).with(no_args)
110
+ subject
111
+ end
112
+ end
113
+ end
114
+
115
+ describe 'Model#with_deleted' do
116
+ before do
117
+ @resource = @model.create
118
+ @resource.destroy
119
+ end
120
+
121
+ describe 'with a block' do
122
+ subject { @model.with_deleted { @model.all } }
123
+
124
+ it 'should scope the block to return all resources' do
125
+ subject.map { |resource| resource.key }.should == [ @resource.key ]
126
+ end
127
+ end
128
+
129
+ describe 'without a block' do
130
+ subject { @model.with_deleted }
131
+
132
+ it 'should return a collection scoped to return all resources' do
133
+ subject.map { |resource| resource.key }.should == [ @resource.key ]
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,143 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe DataMapper::Property::ParanoidDateTime do
4
+ before :all do
5
+ Object.send(:remove_const, :Blog) if defined?(Blog)
6
+ module ::Blog
7
+ class Article
8
+ include DataMapper::Resource
9
+
10
+ property :id, Serial
11
+ property :deleted_at, ParanoidDateTime
12
+
13
+ before :destroy, :before_destroy
14
+
15
+ def before_destroy; end
16
+ end
17
+ end
18
+
19
+ @model = Blog::Article
20
+ end
21
+
22
+ supported_by :all do
23
+ describe 'Resource#destroy' do
24
+ before do
25
+ pending 'Does not work with < 1.8.7, see if backports fixes it' if RUBY_VERSION < '1.8.7'
26
+ end
27
+
28
+ subject { @resource.destroy }
29
+
30
+ describe 'with a new resource' do
31
+ before do
32
+ @resource = @model.new
33
+ end
34
+
35
+ it { should be_false }
36
+
37
+ it 'should not delete the resource from the datastore' do
38
+ method(:subject).should_not change { @model.with_deleted.size }.from(0)
39
+ end
40
+
41
+ it 'should not set the paranoid column' do
42
+ method(:subject).should_not change { @resource.deleted_at }.from(nil)
43
+ end
44
+
45
+ it 'should run the destroy hook' do
46
+ @resource.should_receive(:before_destroy).with(no_args)
47
+ subject
48
+ end
49
+ end
50
+
51
+ describe 'with a saved resource' do
52
+ before do
53
+ @resource = @model.create
54
+ end
55
+
56
+ it { should be_true }
57
+
58
+ it 'should not delete the resource from the datastore' do
59
+ method(:subject).should_not change { @model.with_deleted.size }.from(1)
60
+ end
61
+
62
+ it 'should set the paranoid column' do
63
+ method(:subject).should change { @resource.deleted_at }.from(nil)
64
+ end
65
+
66
+ it 'should run the destroy hook' do
67
+ @resource.should_receive(:before_destroy).with(no_args)
68
+ subject
69
+ end
70
+ end
71
+ end
72
+
73
+ describe 'Resource#destroy!' do
74
+ subject { @resource.destroy! }
75
+
76
+ describe 'with a new resource' do
77
+ before do
78
+ @resource = @model.new
79
+ end
80
+
81
+ it { should be_false }
82
+
83
+ it 'should not delete the resource from the datastore' do
84
+ method(:subject).should_not change { @model.with_deleted.size }.from(0)
85
+ end
86
+
87
+ it 'should not set the paranoid column' do
88
+ method(:subject).should_not change { @resource.deleted_at }.from(nil)
89
+ end
90
+
91
+ it 'should not run the destroy hook' do
92
+ @resource.should_not_receive(:before_destroy).with(no_args)
93
+ subject
94
+ end
95
+ end
96
+
97
+ describe 'with a saved resource' do
98
+ before do
99
+ @resource = @model.create
100
+ end
101
+
102
+ it { should be_true }
103
+
104
+ it 'should delete the resource from the datastore' do
105
+ method(:subject).should change { @model.with_deleted.size }.from(1).to(0)
106
+ end
107
+
108
+ it 'should not set the paranoid column' do
109
+ method(:subject).should_not change { @resource.deleted_at }.from(nil)
110
+ end
111
+
112
+ it 'should not run the destroy hook' do
113
+ @resource.should_not_receive(:before_destroy).with(no_args)
114
+ subject
115
+ end
116
+ end
117
+ end
118
+
119
+ describe 'Model#with_deleted' do
120
+ before do
121
+ pending 'Does not work with < 1.8.7, see if backports fixes it' if RUBY_VERSION < '1.8.7'
122
+ @resource = @model.create
123
+ @resource.destroy
124
+ end
125
+
126
+ describe 'with a block' do
127
+ subject { @model.with_deleted { @model.all } }
128
+
129
+ it 'should scope the block to return all resources' do
130
+ subject.map { |resource| resource.key }.should == [ @resource.key ]
131
+ end
132
+ end
133
+
134
+ describe 'without a block' do
135
+ subject { @model.with_deleted }
136
+
137
+ it 'should return a collection scoped to return all resources' do
138
+ subject.map { |resource| resource.key }.should == [ @resource.key ]
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -1,12 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  try_spec do
4
- describe DataMapper::Types::Regexp do
4
+ describe DataMapper::Property::Regexp do
5
+ before :all do
6
+ class User
7
+ include DataMapper::Resource
8
+ property :id, Serial
9
+ property :regexp, Regexp
10
+ end
11
+
12
+ @property = User.properties[:regexp]
13
+ end
14
+
5
15
  describe '.load' do
6
16
  describe 'when argument is a string' do
7
17
  before :all do
8
18
  @input = '[a-z]\d+'
9
- @result = DataMapper::Types::Regexp.load(@input, :property)
19
+ @result = @property.load(@input)
10
20
  end
11
21
 
12
22
  it 'create a regexp instance from argument' do
@@ -17,7 +27,7 @@ try_spec do
17
27
  describe 'when argument is nil' do
18
28
  before :all do
19
29
  @input = nil
20
- @result = DataMapper::Types::Regexp.load(@input, :property)
30
+ @result = @property.load(@input)
21
31
  end
22
32
 
23
33
  it 'returns nil' do
@@ -30,7 +40,7 @@ try_spec do
30
40
  describe 'when argument is a regular expression' do
31
41
  before :all do
32
42
  @input = /\d+/
33
- @result = DataMapper::Types::Regexp.dump(@input, :property)
43
+ @result = @property.dump(@input)
34
44
  end
35
45
 
36
46
  it 'escapes the argument' do
@@ -41,7 +51,7 @@ try_spec do
41
51
  describe 'when argument is nil' do
42
52
  before :all do
43
53
  @input = nil
44
- @result = DataMapper::Types::Regexp.dump(@input, :property)
54
+ @result = @property.dump(@input)
45
55
  end
46
56
 
47
57
  it 'returns nil' do
@@ -1,44 +1,48 @@
1
1
  require 'spec_helper'
2
2
 
3
+ require './spec/fixtures/bookmark'
4
+
3
5
  try_spec do
4
- describe DataMapper::Types::URI do
6
+ describe DataMapper::Property::URI do
5
7
  before do
6
8
  @uri_str = 'http://example.com/path/to/resource/'
7
9
  @uri = Addressable::URI.parse(@uri_str)
10
+
11
+ @property = DataMapper::Types::Fixtures::Bookmark.properties[:uri]
8
12
  end
9
13
 
10
14
  describe '.dump' do
11
15
  it 'returns the URI as a String' do
12
- DataMapper::Types::URI.dump(@uri, :property).should == @uri_str
16
+ @property.dump(@uri).should == @uri_str
13
17
  end
14
18
 
15
19
  describe 'when given nil' do
16
20
  it 'returns nil' do
17
- DataMapper::Types::URI.dump(nil, :property).should be_nil
21
+ @property.dump(nil).should be_nil
18
22
  end
19
23
  end
20
24
 
21
25
  describe 'when given an empty string' do
22
26
  it 'returns an empty URI' do
23
- DataMapper::Types::URI.dump('', :property).should == ''
27
+ @property.dump('').should == ''
24
28
  end
25
29
  end
26
30
  end
27
31
 
28
32
  describe '.load' do
29
33
  it 'returns the URI as Addressable' do
30
- DataMapper::Types::URI.load(@uri_str, :property).should == @uri
34
+ @property.load(@uri_str).should == @uri
31
35
  end
32
36
 
33
37
  describe 'when given nil' do
34
38
  it 'returns nil' do
35
- DataMapper::Types::URI.load(nil, :property).should be_nil
39
+ @property.load(nil).should be_nil
36
40
  end
37
41
  end
38
42
 
39
43
  describe 'if given an empty String' do
40
44
  it 'returns an empty URI' do
41
- DataMapper::Types::URI.load('', :property).should == Addressable::URI.parse('')
45
+ @property.load('').should == Addressable::URI.parse('')
42
46
  end
43
47
  end
44
48
  end
@@ -46,13 +50,13 @@ try_spec do
46
50
  describe '.typecast' do
47
51
  describe 'given instance of Addressable::URI' do
48
52
  it 'does nothing' do
49
- DataMapper::Types::URI.typecast(@uri, :property).should == @uri
53
+ @property.typecast(@uri).should == @uri
50
54
  end
51
55
  end
52
56
 
53
57
  describe 'when given a string' do
54
58
  it 'delegates to .load' do
55
- DataMapper::Types::URI.typecast(@uri_str, :property).should == @uri
59
+ @property.typecast(@uri_str).should == @uri
56
60
  end
57
61
  end
58
62
  end