ardm-types 1.2.2

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 (77) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +36 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +51 -0
  5. data/LICENSE +20 -0
  6. data/README.rdoc +3 -0
  7. data/Rakefile +4 -0
  8. data/ardm-types.gemspec +29 -0
  9. data/lib/ardm-types.rb +1 -0
  10. data/lib/dm-types/api_key.rb +30 -0
  11. data/lib/dm-types/bcrypt_hash.rb +34 -0
  12. data/lib/dm-types/comma_separated_list.rb +29 -0
  13. data/lib/dm-types/csv.rb +38 -0
  14. data/lib/dm-types/enum.rb +51 -0
  15. data/lib/dm-types/epoch_time.rb +41 -0
  16. data/lib/dm-types/file_path.rb +32 -0
  17. data/lib/dm-types/flag.rb +63 -0
  18. data/lib/dm-types/ip_address.rb +42 -0
  19. data/lib/dm-types/json.rb +50 -0
  20. data/lib/dm-types/paranoid/base.rb +55 -0
  21. data/lib/dm-types/paranoid_boolean.rb +23 -0
  22. data/lib/dm-types/paranoid_datetime.rb +22 -0
  23. data/lib/dm-types/regexp.rb +21 -0
  24. data/lib/dm-types/slug.rb +29 -0
  25. data/lib/dm-types/support/dirty_minder.rb +166 -0
  26. data/lib/dm-types/support/flags.rb +41 -0
  27. data/lib/dm-types/uri.rb +38 -0
  28. data/lib/dm-types/uuid.rb +74 -0
  29. data/lib/dm-types/version.rb +5 -0
  30. data/lib/dm-types/yaml.rb +41 -0
  31. data/lib/dm-types.rb +23 -0
  32. data/spec/fixtures/api_user.rb +14 -0
  33. data/spec/fixtures/article.rb +35 -0
  34. data/spec/fixtures/bookmark.rb +23 -0
  35. data/spec/fixtures/invention.rb +7 -0
  36. data/spec/fixtures/network_node.rb +36 -0
  37. data/spec/fixtures/person.rb +25 -0
  38. data/spec/fixtures/software_package.rb +33 -0
  39. data/spec/fixtures/ticket.rb +21 -0
  40. data/spec/fixtures/tshirt.rb +24 -0
  41. data/spec/integration/api_key_spec.rb +27 -0
  42. data/spec/integration/bcrypt_hash_spec.rb +47 -0
  43. data/spec/integration/comma_separated_list_spec.rb +87 -0
  44. data/spec/integration/dirty_minder_spec.rb +197 -0
  45. data/spec/integration/enum_spec.rb +80 -0
  46. data/spec/integration/epoch_time_spec.rb +61 -0
  47. data/spec/integration/file_path_spec.rb +160 -0
  48. data/spec/integration/flag_spec.rb +72 -0
  49. data/spec/integration/ip_address_spec.rb +153 -0
  50. data/spec/integration/json_spec.rb +72 -0
  51. data/spec/integration/slug_spec.rb +67 -0
  52. data/spec/integration/uri_spec.rb +139 -0
  53. data/spec/integration/uuid_spec.rb +102 -0
  54. data/spec/integration/yaml_spec.rb +69 -0
  55. data/spec/rcov.opts +6 -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.opts +4 -0
  59. data/spec/spec_helper.rb +30 -0
  60. data/spec/unit/bcrypt_hash_spec.rb +155 -0
  61. data/spec/unit/csv_spec.rb +142 -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 +87 -0
  65. data/spec/unit/flag_spec.rb +114 -0
  66. data/spec/unit/ip_address_spec.rb +121 -0
  67. data/spec/unit/json_spec.rb +144 -0
  68. data/spec/unit/paranoid_boolean_spec.rb +150 -0
  69. data/spec/unit/paranoid_datetime_spec.rb +154 -0
  70. data/spec/unit/regexp_spec.rb +63 -0
  71. data/spec/unit/uri_spec.rb +64 -0
  72. data/spec/unit/uuid_spec.rb +25 -0
  73. data/spec/unit/yaml_spec.rb +111 -0
  74. data/tasks/spec.rake +38 -0
  75. data/tasks/yard.rake +9 -0
  76. data/tasks/yardstick.rake +19 -0
  77. metadata +236 -0
@@ -0,0 +1,37 @@
1
+ share_examples_for "A property with flags" do
2
+ before :all do
3
+ %w[ @property_klass ].each do |ivar|
4
+ raise "+#{ivar}+ should be defined in before block" unless instance_variable_defined?(ivar)
5
+ end
6
+
7
+ @flags = [ :one, :two, :three ]
8
+
9
+ class ::User
10
+ include DataMapper::Resource
11
+ end
12
+
13
+ @property = User.property :item, @property_klass[@flags], :key => true
14
+ end
15
+
16
+ describe ".generated_classes" do
17
+ it "should cache the generated class" do
18
+ @property_klass.generated_classes[@flags].should_not be_nil
19
+ end
20
+ end
21
+
22
+ it "should include :flags in accepted_options" do
23
+ @property_klass.accepted_options.should include(:flags)
24
+ end
25
+
26
+ it "should respond to :generated_classes" do
27
+ @property_klass.should respond_to(:generated_classes)
28
+ end
29
+
30
+ it "should respond to :flag_map" do
31
+ @property.should respond_to(:flag_map)
32
+ end
33
+
34
+ it "should be custom" do
35
+ @property.custom?.should be(true)
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ describe "identity function", :shared => true do
2
+ it "returns value unchanged" do
3
+ @result.should == @input
4
+ end
5
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --loadby random
3
+ --format profile
4
+ --backtrace
@@ -0,0 +1,30 @@
1
+ require 'dm-core/spec/setup'
2
+ require 'dm-core/spec/lib/adapter_helpers'
3
+
4
+ require 'dm-types'
5
+ require 'dm-migrations'
6
+ require 'dm-validations'
7
+
8
+ Dir["#{Pathname(__FILE__).dirname.expand_path}/shared/*.rb"].each { |file| require file }
9
+
10
+ DataMapper::Spec.setup
11
+
12
+ Spec::Runner.configure do |config|
13
+ config.extend(DataMapper::Spec::Adapters::Helpers)
14
+ end
15
+
16
+ DEPENDENCIES = {
17
+ 'bcrypt' => 'bcrypt-ruby',
18
+ }
19
+
20
+ def try_spec
21
+ begin
22
+ yield
23
+ rescue LoadError => error
24
+ raise error unless lib = error.message.match(/\Ano such file to load -- (.+)\z/)[1]
25
+
26
+ gem_location = DEPENDENCIES[lib] || raise("Unknown lib #{lib}")
27
+
28
+ warn "[WARNING] Skipping specs using #{lib}, please do: gem install #{gem_location}"
29
+ end
30
+ end
@@ -0,0 +1,155 @@
1
+ require '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 BCrypt::Password' do
34
+ @result.should be_an_instance_of(BCrypt::Password)
35
+ end
36
+
37
+ it 'returns a string that is 60 characters long' do
38
+ @result.size.should == 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
+ @result.should 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
+ @result.should be_an_instance_of(BCrypt::Password)
63
+ end
64
+
65
+ it 'returns a string that matches original' do
66
+ @result.should == @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
+ @result.should 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
+ @result.should be_an_instance_of(BCrypt::Password)
91
+ end
92
+
93
+ it 'casts argument to value that matches input' do
94
+ @result.should == @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
+ @result.should be_an_instance_of(BCrypt::Password)
106
+ end
107
+
108
+ it 'casts argument to value that matches input' do
109
+ @result.should == @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
+ @result.should be_an_instance_of(BCrypt::Password)
121
+ end
122
+
123
+ it 'casts argument to value that matches input' do
124
+ @result.should == @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
+ @result.should be_an_instance_of(BCrypt::Password)
136
+ end
137
+
138
+ it 'casts argument to value that matches input' do
139
+ @result.should == @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
+ @result.should be_nil
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,142 @@
1
+ require '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
+ @result.should == [ %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
+ @result.should == @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
+ @result.should 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
+ @result.should 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
+ @result.should 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
+ @result.should 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
+ @result.should 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
+ @result.should == "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
+ @result.should == @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
+ @result.should 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
+ @result.should be_nil
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,126 @@
1
+ require '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_should_behave_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 'should return the key of the value match from the flag map' do
22
+ @enum.dump(:first).should == 1
23
+ @enum.dump(:second).should == 2
24
+ @enum.dump(:third).should == 3
25
+ end
26
+
27
+ describe 'when there is no match' do
28
+ it 'should return nil' do
29
+ @enum.dump(:zero).should 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
+ @enum.load(1).should == :uno
41
+ @enum.load(2).should == :dos
42
+ @enum.load(3).should == :tres
43
+ end
44
+
45
+ describe 'when there is no key' do
46
+ it 'returns nil' do
47
+ @enum.load(-1).should 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
+ @enum.typecast(:uno).should == :uno
61
+ end
62
+ end
63
+
64
+ describe 'when given a string' do
65
+ it 'uses Enum type' do
66
+ @enum.typecast('uno').should == :uno
67
+ end
68
+ end
69
+
70
+ describe 'when given nil' do
71
+ it 'returns nil' do
72
+ @enum.typecast( nil).should == 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
+ @enum.typecast(1).should == 1
85
+ end
86
+ end
87
+
88
+ describe 'when given a float' do
89
+ it 'uses Enum type' do
90
+ @enum.typecast(1.1).should == 1
91
+ end
92
+ end
93
+
94
+ describe 'when given nil' do
95
+ it 'returns nil' do
96
+ @enum.typecast( nil).should == 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
+ @enum.typecast(:uno).should == 'uno'
109
+ end
110
+ end
111
+
112
+ describe 'when given a string' do
113
+ it 'uses Enum type' do
114
+ @enum.typecast('uno').should == 'uno'
115
+ end
116
+ end
117
+
118
+ describe 'when given nil' do
119
+ it 'returns nil' do
120
+ @enum.typecast( nil).should == nil
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,74 @@
1
+ require '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 { should == value.to_i }
22
+ end
23
+
24
+ describe 'with nil' do
25
+ let(:value) { nil }
26
+
27
+ it { should == 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 { should == Time.parse(value.to_s) }
38
+ end
39
+
40
+ describe 'with a number' do
41
+ let(:value) { Time.now.to_i }
42
+
43
+ it { should == ::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 { should == ::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 { should == ::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 { should == Time.at(value) }
66
+ end
67
+
68
+ describe 'with nil' do
69
+ let(:value) { nil }
70
+
71
+ it { should == value }
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require './spec/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 '#valid?' do
16
+ describe "with a String" do
17
+ subject { @property.valid?(@input) }
18
+ it { subject.should be(true) }
19
+ end
20
+
21
+ describe "with a Pathname" do
22
+ subject { @property.valid?(@path) }
23
+ it { subject.should be(true) }
24
+ end
25
+ end
26
+
27
+ describe '.dump' do
28
+ describe 'when input is a string' do
29
+ it 'does not modify input' do
30
+ @property.dump(@input).should == @input
31
+ end
32
+ end
33
+
34
+ describe 'when input is nil' do
35
+ it 'returns nil' do
36
+ @property.dump(nil).should be_nil
37
+ end
38
+ end
39
+
40
+ describe 'when input is a blank string' do
41
+ it 'returns nil' do
42
+ @property.dump('').should be_nil
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '.load' do
48
+ describe 'when value is a non-blank file path' do
49
+ it 'returns Pathname for a path' do
50
+ @property.load(@input).should == @path
51
+ end
52
+ end
53
+
54
+ describe 'when value is nil' do
55
+ it 'return nil' do
56
+ @property.load(nil).should be_nil
57
+ end
58
+ end
59
+
60
+ describe 'when value is a blank string' do
61
+ it 'returns nil' do
62
+ @property.load('').should be_nil
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '.typecast' do
68
+ describe 'when a Pathname is given' do
69
+ it 'does not modify input' do
70
+ @property.typecast(@path).should == @path
71
+ end
72
+ end
73
+
74
+ describe 'when a nil is given' do
75
+ it 'does not modify input' do
76
+ @property.typecast(nil).should == nil
77
+ end
78
+ end
79
+
80
+ describe 'when a string is given' do
81
+ it 'returns Pathname for given path' do
82
+ @property.typecast(@input).should == @path
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end