activeuuid 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -2
- data/.travis.yml +2 -5
- data/README.mkd +24 -0
- data/activeuuid.gemspec +2 -1
- data/gemfiles/Gemfile.rails-4-0 +5 -0
- data/gemfiles/Gemfile.rails-4-1 +5 -0
- data/lib/activeuuid/patches.rb +9 -3
- data/lib/activeuuid/uuid.rb +16 -6
- data/lib/activeuuid/version.rb +1 -1
- data/spec/fabricators/uuid_article_with_namespace_fabricator.rb +4 -0
- data/spec/fabricators/uuid_article_with_natural_key_fabricator.rb +4 -0
- data/spec/lib/activerecord_spec.rb +29 -7
- data/spec/lib/uuid_mokeypatch_spec.rb +1 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/models/uuid_article_with_namespace.rb +6 -0
- data/spec/support/models/uuid_article_with_natural_key.rb +5 -0
- metadata +29 -9
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -3,15 +3,15 @@ before_script:
|
|
3
3
|
- psql -c 'create database activeuuid_test;' -U postgres
|
4
4
|
|
5
5
|
rvm:
|
6
|
-
- 1.9.2
|
7
6
|
- 1.9.3
|
8
7
|
- 2.0.0
|
9
|
-
- rbx-
|
8
|
+
- rbx-2
|
10
9
|
|
11
10
|
gemfile:
|
12
11
|
- Gemfile
|
13
12
|
- gemfiles/Gemfile.rails-3-1
|
14
13
|
- gemfiles/Gemfile.rails-3-2
|
14
|
+
- gemfiles/Gemfile.rails-4-0
|
15
15
|
- gemfiles/Gemfile.rails-head
|
16
16
|
|
17
17
|
env:
|
@@ -22,6 +22,3 @@ env:
|
|
22
22
|
matrix:
|
23
23
|
allow_failures:
|
24
24
|
- gemfile: gemfiles/Gemfile.rails-head
|
25
|
-
exclude:
|
26
|
-
- rvm: 1.9.2
|
27
|
-
gemfile: gemfiles/Gemfile.rails-head
|
data/README.mkd
CHANGED
@@ -41,6 +41,29 @@ class Email < ActiveRecord::Base
|
|
41
41
|
end
|
42
42
|
```
|
43
43
|
|
44
|
+
### specify a natural key for generating the UUID based on one or more columns
|
45
|
+
```ruby
|
46
|
+
class Email < ActiveRecord::Base
|
47
|
+
include ActiveUUID::UUID
|
48
|
+
natural_key :sender_id, :received_at
|
49
|
+
belongs_to :sender
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
`natural_key` generates a SHA1-based UUID in the ISO OID namespace by default. [7]
|
54
|
+
|
55
|
+
### specify a custom UUID namespace for the natural key
|
56
|
+
```ruby
|
57
|
+
class Email < ActiveRecord::Base
|
58
|
+
include ActiveUUID::UUID
|
59
|
+
uuid_namespace "1dd74dd0-d116-11e0-99c7-5ac5d975667e"
|
60
|
+
natural_key :sender_id, :received_at
|
61
|
+
belongs_to :sender
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
`uuid_namespace` can either be a UUID in string format, or a UUIDTools::UUID object.
|
66
|
+
|
44
67
|
### use it:
|
45
68
|
Here are some example specs:
|
46
69
|
|
@@ -118,6 +141,7 @@ Or get the code here: https://github.com/jashmenn/activeuuid
|
|
118
141
|
* [4] http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html
|
119
142
|
* [5] http://krow.livejournal.com/497839.html
|
120
143
|
* [6] https://github.com/jamesgolick/friendly
|
144
|
+
* [7] http://tools.ietf.org/html/rfc4122.html#appendix-C
|
121
145
|
|
122
146
|
## Dependencies
|
123
147
|
Rails ~> 3.1.0
|
data/activeuuid.gemspec
CHANGED
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
|
19
19
|
s.add_development_dependency "rake"
|
20
|
-
s.add_development_dependency "rspec"
|
20
|
+
s.add_development_dependency "rspec", "~> 2.99.0"
|
21
|
+
s.add_development_dependency "rspec-its"
|
21
22
|
s.add_development_dependency "activesupport"
|
22
23
|
s.add_development_dependency "database_cleaner"
|
23
24
|
s.add_development_dependency "forgery"
|
data/lib/activeuuid/patches.rb
CHANGED
@@ -8,7 +8,7 @@ module ActiveUUID
|
|
8
8
|
def uuid(*column_names)
|
9
9
|
options = column_names.extract_options!
|
10
10
|
column_names.each do |name|
|
11
|
-
type =
|
11
|
+
type = ActiveRecord::Base.connection.adapter_name.downcase == 'postgresql' ? 'uuid' : 'binary(16)'
|
12
12
|
column(name, "#{type}#{' PRIMARY KEY' if options.delete(:primary_key)}", options)
|
13
13
|
end
|
14
14
|
end
|
@@ -29,12 +29,12 @@ module ActiveUUID
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def simplified_type_with_uuid(field_type)
|
32
|
-
return :uuid if field_type == 'binary(16)'
|
32
|
+
return :uuid if field_type == 'binary(16)' || field_type == 'binary(16,0)'
|
33
33
|
simplified_type_without_uuid(field_type)
|
34
34
|
end
|
35
35
|
|
36
36
|
alias_method_chain :type_cast, :uuid
|
37
|
-
alias_method_chain :type_cast_code, :uuid
|
37
|
+
alias_method_chain :type_cast_code, :uuid if ActiveRecord::VERSION::MAJOR < 4
|
38
38
|
alias_method_chain :simplified_type, :uuid
|
39
39
|
end
|
40
40
|
end
|
@@ -43,6 +43,12 @@ module ActiveUUID
|
|
43
43
|
extend ActiveSupport::Concern
|
44
44
|
|
45
45
|
included do
|
46
|
+
def type_cast_with_uuid(value)
|
47
|
+
return UUIDTools::UUID.serialize(value) if type == :uuid
|
48
|
+
type_cast_without_uuid(value)
|
49
|
+
end
|
50
|
+
alias_method_chain :type_cast, :uuid if ActiveRecord::VERSION::MAJOR >= 4
|
51
|
+
|
46
52
|
def simplified_type_with_pguuid(field_type)
|
47
53
|
return :uuid if field_type == 'uuid'
|
48
54
|
simplified_type_without_pguuid(field_type)
|
data/lib/activeuuid/uuid.rb
CHANGED
@@ -8,6 +8,10 @@ module UUIDTools
|
|
8
8
|
# duck typing activerecord 3.1 dirty hack )
|
9
9
|
def gsub *; self; end
|
10
10
|
|
11
|
+
def next
|
12
|
+
self.class.random_create
|
13
|
+
end
|
14
|
+
|
11
15
|
def quoted_id
|
12
16
|
s = raw.unpack("H*")[0]
|
13
17
|
"x'#{s}'"
|
@@ -50,25 +54,25 @@ end
|
|
50
54
|
module Arel
|
51
55
|
module Visitors
|
52
56
|
class DepthFirst < Arel::Visitors::Visitor
|
53
|
-
def visit_UUIDTools_UUID(o)
|
57
|
+
def visit_UUIDTools_UUID(o, a = nil)
|
54
58
|
o.quoted_id
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
62
|
class MySQL < Arel::Visitors::ToSql
|
59
|
-
def visit_UUIDTools_UUID(o)
|
63
|
+
def visit_UUIDTools_UUID(o, a = nil)
|
60
64
|
o.quoted_id
|
61
65
|
end
|
62
66
|
end
|
63
67
|
|
64
68
|
class SQLite < Arel::Visitors::ToSql
|
65
|
-
def visit_UUIDTools_UUID(o)
|
69
|
+
def visit_UUIDTools_UUID(o, a = nil)
|
66
70
|
o.quoted_id
|
67
71
|
end
|
68
72
|
end
|
69
73
|
|
70
74
|
class PostgreSQL < Arel::Visitors::ToSql
|
71
|
-
def visit_UUIDTools_UUID(o)
|
75
|
+
def visit_UUIDTools_UUID(o, a = nil)
|
72
76
|
"'#{o.to_s}'"
|
73
77
|
end
|
74
78
|
end
|
@@ -81,6 +85,7 @@ module ActiveUUID
|
|
81
85
|
|
82
86
|
included do
|
83
87
|
class_attribute :_natural_key, instance_writer: false
|
88
|
+
class_attribute :_uuid_namespace, instance_writer: false
|
84
89
|
class_attribute :_uuid_generator, instance_writer: false
|
85
90
|
self._uuid_generator = :random
|
86
91
|
|
@@ -93,6 +98,11 @@ module ActiveUUID
|
|
93
98
|
self._natural_key = attributes
|
94
99
|
end
|
95
100
|
|
101
|
+
def uuid_namespace(namespace)
|
102
|
+
namespace = UUIDTools::UUID.parse_string(namespace) unless namespace.is_a? UUIDTools::UUID
|
103
|
+
self._uuid_namespace = namespace
|
104
|
+
end
|
105
|
+
|
96
106
|
def uuid_generator(generator_name)
|
97
107
|
self._uuid_generator = generator_name
|
98
108
|
end
|
@@ -104,7 +114,7 @@ module ActiveUUID
|
|
104
114
|
EOS
|
105
115
|
end
|
106
116
|
|
107
|
-
def instantiate_with_uuid(record)
|
117
|
+
def instantiate_with_uuid(record, record_models = nil)
|
108
118
|
uuid_columns.each do |uuid_column|
|
109
119
|
record[uuid_column] = UUIDTools::UUID.serialize(record[uuid_column]).to_s if record[uuid_column]
|
110
120
|
end
|
@@ -120,7 +130,7 @@ module ActiveUUID
|
|
120
130
|
if _natural_key
|
121
131
|
# TODO if all the attributes return nil you might want to warn about this
|
122
132
|
chained = _natural_key.map { |attribute| self.send(attribute) }.join('-')
|
123
|
-
UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, chained)
|
133
|
+
UUIDTools::UUID.sha1_create(_uuid_namespace || UUIDTools::UUID_OID_NAMESPACE, chained)
|
124
134
|
else
|
125
135
|
case _uuid_generator
|
126
136
|
when :random
|
data/lib/activeuuid/version.rb
CHANGED
@@ -14,7 +14,7 @@ describe ActiveRecord::Base do
|
|
14
14
|
connection.drop_table table_name
|
15
15
|
end
|
16
16
|
|
17
|
-
specify { connection.table_exists?(table_name).should
|
17
|
+
specify { connection.table_exists?(table_name).should be_truthy }
|
18
18
|
|
19
19
|
context '#add_column' do
|
20
20
|
let(:column_name) { :uuid_column }
|
@@ -22,7 +22,7 @@ describe ActiveRecord::Base do
|
|
22
22
|
|
23
23
|
before { connection.add_column table_name, column_name, :uuid }
|
24
24
|
|
25
|
-
specify { connection.column_exists?(table_name, column_name).should
|
25
|
+
specify { connection.column_exists?(table_name, column_name).should be_truthy }
|
26
26
|
specify { column.should_not be_nil }
|
27
27
|
|
28
28
|
it 'should have proper sql type' do
|
@@ -85,8 +85,8 @@ describe Article do
|
|
85
85
|
|
86
86
|
context '#destroy' do
|
87
87
|
subject { article }
|
88
|
-
its(:delete) { should
|
89
|
-
its(:destroy) { should
|
88
|
+
its(:delete) { should be_truthy }
|
89
|
+
its(:destroy) { should be_truthy }
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -132,8 +132,8 @@ describe UuidArticle do
|
|
132
132
|
|
133
133
|
context '#destroy' do
|
134
134
|
subject { article }
|
135
|
-
its(:delete) { should
|
136
|
-
its(:destroy) { should
|
135
|
+
its(:delete) { should be_truthy }
|
136
|
+
its(:destroy) { should be_truthy }
|
137
137
|
end
|
138
138
|
|
139
139
|
context '#reload' do
|
@@ -184,4 +184,26 @@ describe UuidArticle do
|
|
184
184
|
end
|
185
185
|
end
|
186
186
|
end
|
187
|
-
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe UuidArticleWithNaturalKey do
|
190
|
+
let!(:article) { Fabricate :uuid_article_with_natural_key }
|
191
|
+
let!(:id) { article.id }
|
192
|
+
let!(:uuid) { UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE, article.title) }
|
193
|
+
subject { article }
|
194
|
+
context 'natural_key' do
|
195
|
+
its(:id) { should == uuid }
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe UuidArticleWithNamespace do
|
200
|
+
let!(:article) { Fabricate :uuid_article_with_namespace }
|
201
|
+
let!(:id) { article.id }
|
202
|
+
let!(:namespace) { UuidArticleWithNamespace._uuid_namespace }
|
203
|
+
let!(:uuid) { UUIDTools::UUID.sha1_create(namespace, article.title) }
|
204
|
+
subject { article }
|
205
|
+
context 'natural_key_with_namespace' do
|
206
|
+
its(:id) { should == uuid }
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
data/spec/spec_helper.rb
CHANGED
@@ -8,7 +8,7 @@ require 'active_support/all'
|
|
8
8
|
|
9
9
|
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
10
10
|
ActiveRecord::Base.configurations = YAML::load(File.read(File.dirname(__FILE__) + "/support/database.yml"))
|
11
|
-
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3")
|
11
|
+
ActiveRecord::Base.establish_connection((ENV["DB"] || "sqlite3").to_sym)
|
12
12
|
|
13
13
|
require 'activeuuid'
|
14
14
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeuuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-06-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -29,6 +29,22 @@ dependencies:
|
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.99.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.99.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-its
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
@@ -204,6 +220,8 @@ files:
|
|
204
220
|
- activeuuid.gemspec
|
205
221
|
- gemfiles/Gemfile.rails-3-1
|
206
222
|
- gemfiles/Gemfile.rails-3-2
|
223
|
+
- gemfiles/Gemfile.rails-4-0
|
224
|
+
- gemfiles/Gemfile.rails-4-1
|
207
225
|
- gemfiles/Gemfile.rails-head
|
208
226
|
- lib/activeuuid.rb
|
209
227
|
- lib/activeuuid/patches.rb
|
@@ -212,6 +230,8 @@ files:
|
|
212
230
|
- lib/activeuuid/version.rb
|
213
231
|
- spec/fabricators/article_fabricator.rb
|
214
232
|
- spec/fabricators/uuid_article_fabricator.rb
|
233
|
+
- spec/fabricators/uuid_article_with_namespace_fabricator.rb
|
234
|
+
- spec/fabricators/uuid_article_with_natural_key_fabricator.rb
|
215
235
|
- spec/lib/activerecord_spec.rb
|
216
236
|
- spec/lib/uuid_mokeypatch_spec.rb
|
217
237
|
- spec/spec_helper.rb
|
@@ -220,6 +240,8 @@ files:
|
|
220
240
|
- spec/support/migrate/20120817081813_create_uuid_articles.rb
|
221
241
|
- spec/support/models/article.rb
|
222
242
|
- spec/support/models/uuid_article.rb
|
243
|
+
- spec/support/models/uuid_article_with_namespace.rb
|
244
|
+
- spec/support/models/uuid_article_with_natural_key.rb
|
223
245
|
- spec/support/spec_for_adapter.rb
|
224
246
|
homepage: https://github.com/jashmenn/activeuuid
|
225
247
|
licenses: []
|
@@ -233,27 +255,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
233
255
|
- - ! '>='
|
234
256
|
- !ruby/object:Gem::Version
|
235
257
|
version: '0'
|
236
|
-
segments:
|
237
|
-
- 0
|
238
|
-
hash: 852897539311025223
|
239
258
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
240
259
|
none: false
|
241
260
|
requirements:
|
242
261
|
- - ! '>='
|
243
262
|
- !ruby/object:Gem::Version
|
244
263
|
version: '0'
|
245
|
-
segments:
|
246
|
-
- 0
|
247
|
-
hash: 852897539311025223
|
248
264
|
requirements: []
|
249
265
|
rubyforge_project:
|
250
|
-
rubygems_version: 1.8.
|
266
|
+
rubygems_version: 1.8.23
|
251
267
|
signing_key:
|
252
268
|
specification_version: 3
|
253
269
|
summary: Add binary UUIDs to ActiveRecord in MySQL
|
254
270
|
test_files:
|
255
271
|
- spec/fabricators/article_fabricator.rb
|
256
272
|
- spec/fabricators/uuid_article_fabricator.rb
|
273
|
+
- spec/fabricators/uuid_article_with_namespace_fabricator.rb
|
274
|
+
- spec/fabricators/uuid_article_with_natural_key_fabricator.rb
|
257
275
|
- spec/lib/activerecord_spec.rb
|
258
276
|
- spec/lib/uuid_mokeypatch_spec.rb
|
259
277
|
- spec/spec_helper.rb
|
@@ -262,4 +280,6 @@ test_files:
|
|
262
280
|
- spec/support/migrate/20120817081813_create_uuid_articles.rb
|
263
281
|
- spec/support/models/article.rb
|
264
282
|
- spec/support/models/uuid_article.rb
|
283
|
+
- spec/support/models/uuid_article_with_namespace.rb
|
284
|
+
- spec/support/models/uuid_article_with_natural_key.rb
|
265
285
|
- spec/support/spec_for_adapter.rb
|