uuids 1.0.0.pre.rc1 → 1.0.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.
- checksums.yaml +4 -4
- data/README.rdoc +7 -28
- data/app/models/uuids/uuid.rb +15 -11
- data/config/locales/en.yml +3 -1
- data/config/locales/ru.yml +3 -1
- data/lib/tasks/uuids_tasks.rake +0 -40
- data/lib/uuids/base.rb +30 -19
- data/lib/uuids/version.rb +1 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +2186 -0
- data/spec/models/uuids/uuid_spec.rb +17 -7
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e1135e09e3a04defaa19436933e9b299a7bb438
|
4
|
+
data.tar.gz: 9243d46849dc1f81e115685880b4e8d8d013b2ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41349f357ecc392440a79ceb11be07c3aed5b67beef4ea1e0cf519198c9e43ce533bd8ca2afb2055ddd0f57d6e7998b11ce9b92bfa14a1793195e54ab1b7d020
|
7
|
+
data.tar.gz: cb56fc5edcf95f63ce210075db61a5b4e9c9116fe0f75f730b4986b8578afb57515def53f6ed65e6227e633591c85b24fccc60ed3695839dcda8a538d3b15b83
|
data/README.rdoc
CHANGED
@@ -50,7 +50,7 @@ Now the model of cities should **know nothing about outer models** that use it.
|
|
50
50
|
|
51
51
|
=== Translations
|
52
52
|
|
53
|
-
Error messages are translated to English and Russian (see
|
53
|
+
Error messages are translated to English and Russian (see <tt>config/locales</tt>).
|
54
54
|
Translations to other languages are welcome.
|
55
55
|
|
56
56
|
== Installation
|
@@ -85,7 +85,7 @@ Run from a command line in application root:
|
|
85
85
|
|
86
86
|
== Usage
|
87
87
|
|
88
|
-
=== Adding UUIDs
|
88
|
+
=== Adding UUIDs to models
|
89
89
|
|
90
90
|
Add the assotiation to your AR model:
|
91
91
|
|
@@ -114,11 +114,10 @@ The first uuid is added by default. It can also be set manually:
|
|
114
114
|
The destruction of object is forbidden if it has a +uuid+. You should reassign
|
115
115
|
all object's UUIDs to another object in advance.
|
116
116
|
|
117
|
-
=== Referring model by UUID
|
117
|
+
=== Referring model by UUID
|
118
118
|
|
119
119
|
Instead of <tt>ActiveRecord::Associations</tt> +belongs_to+, +has_one+ and
|
120
|
-
+has_many+, you should define custom methods
|
121
|
-
explicitly.
|
120
|
+
+has_many+, you should define custom methods explicitly.
|
122
121
|
|
123
122
|
class CreateStreetsTable < ActiveRecord::Migration
|
124
123
|
def change
|
@@ -142,32 +141,12 @@ explicitly.
|
|
142
141
|
end
|
143
142
|
end
|
144
143
|
|
145
|
-
== Uninstallation
|
146
|
-
|
147
|
-
To uninstall the module you need to:
|
148
|
-
|
149
|
-
* rollback and remove uuid's migration;
|
150
|
-
* remove all lines containing <tt>include Uuids::Base</tt>, +has_uuids+ and <tt>by_uuid: true</tt> from any model;
|
151
|
-
* remove the gem dependencies from application's +Gemfile+ and gemspec.
|
152
|
-
|
153
|
-
=== Rails app
|
154
|
-
|
155
|
-
Run from a command line in application root:
|
156
|
-
|
157
|
-
$ rake uuids:uninstall
|
158
|
-
|
159
|
-
=== Rails mountable engine
|
160
|
-
|
161
|
-
Run from a command line in engine root:
|
162
|
-
|
163
|
-
$ rake app:uuids:uninstall
|
164
|
-
|
165
144
|
== Contributing
|
166
145
|
|
167
146
|
1. Fork it ( https://github.com/nepalez/uuids/fork )
|
168
|
-
2. Create your feature branch (
|
169
|
-
3. Commit your changes (
|
170
|
-
4. Push to the branch (
|
147
|
+
2. Create your feature branch (<tt>git checkout -b my-new-feature</tt>)
|
148
|
+
3. Commit your changes (<tt>git commit -am 'Add some feature'</tt>)
|
149
|
+
4. Push to the branch (<tt>git push origin my-new-feature</tt>)
|
171
150
|
5. Create a new Pull Request
|
172
151
|
|
173
152
|
== License
|
data/app/models/uuids/uuid.rb
CHANGED
@@ -2,16 +2,14 @@ module Uuids
|
|
2
2
|
|
3
3
|
# Stores uuids assigned to corresponding records.
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
5
|
+
# Attributes:
|
7
6
|
# +value+:: A value of uuid as defined in
|
8
7
|
# {RFC4122}[http://www.ietf.org/rfc/rfc4122.txt].
|
9
8
|
# Assigned by default on creation. Cannot be set or edited manually.
|
10
9
|
# +record+:: An AR record the uuid is assigned to.
|
11
10
|
# Required attribute. Can be changed.
|
12
11
|
#
|
13
|
-
#
|
14
|
-
#
|
12
|
+
# @example
|
15
13
|
# # Create the uuid
|
16
14
|
# uuid = Uuids::Uuid.create! record: some_record
|
17
15
|
#
|
@@ -23,16 +21,17 @@ module Uuids
|
|
23
21
|
#
|
24
22
|
class Uuid < ActiveRecord::Base
|
25
23
|
|
24
|
+
# A format for UUID following RFC4122
|
25
|
+
UUID_FORMAT = /[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}/
|
26
|
+
|
26
27
|
attr_readonly :value
|
27
28
|
belongs_to :record, polymorphic: true
|
28
|
-
validate :record_present?
|
29
|
-
before_destroy :forbid_destruction
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
validates :value, format: { with: UUID_FORMAT }, allow_nil: true
|
31
|
+
validate :record_present?
|
32
|
+
|
33
|
+
before_create :set_default_value
|
34
|
+
before_destroy :forbid_destruction
|
36
35
|
|
37
36
|
private
|
38
37
|
|
@@ -42,6 +41,11 @@ module Uuids
|
|
42
41
|
errors.add :record, :blank, uuid: value
|
43
42
|
end
|
44
43
|
|
44
|
+
# Sets a new value by default
|
45
|
+
def set_default_value
|
46
|
+
self.value = SecureRandom.uuid unless value
|
47
|
+
end
|
48
|
+
|
45
49
|
# Forbids destruction of the record.
|
46
50
|
def forbid_destruction
|
47
51
|
errors.add :base, :destruction_forbidden
|
data/config/locales/en.yml
CHANGED
@@ -7,4 +7,6 @@ en:
|
|
7
7
|
base:
|
8
8
|
destruction_forbidden: "Destruction of UUID is forbidden. Instead of destruction reassign the UUID to another record!"
|
9
9
|
record:
|
10
|
-
blank: "Define a record the UUID: %{uuid} is assigned to!"
|
10
|
+
blank: "Define a record the UUID: %{uuid} is assigned to!"
|
11
|
+
value:
|
12
|
+
invalid: "%{value} is not a proper UUID."
|
data/config/locales/ru.yml
CHANGED
@@ -7,4 +7,6 @@ ru:
|
|
7
7
|
base:
|
8
8
|
destruction_forbidden: "Удаление UUID запрещено. Вместо удаления перенаправьте UUID на другую запись!"
|
9
9
|
record:
|
10
|
-
blank: "Укажите запись, на которую ссылается UUID: %{uuid}!"
|
10
|
+
blank: "Укажите запись, на которую ссылается UUID: %{uuid}!"
|
11
|
+
value:
|
12
|
+
invalid: "%{value} не является корректным UUID."
|
data/lib/tasks/uuids_tasks.rake
CHANGED
@@ -4,44 +4,4 @@ namespace :uuids do
|
|
4
4
|
task install: %w(install:migrations) do
|
5
5
|
sh "rake db:migrate SCOPE=uuids"
|
6
6
|
end
|
7
|
-
|
8
|
-
desc "Uninstalls and removes the uuids gem from a Rails application"
|
9
|
-
task :uninstall do
|
10
|
-
sh "rake db:rollback SCOPE=uuids"
|
11
|
-
remove_gem
|
12
|
-
sh "bundle"
|
13
|
-
end
|
14
|
-
|
15
|
-
def remove_gem
|
16
|
-
say "Removing the 'uuids' gem" do
|
17
|
-
GEMFILE = /gem\s+["|']uuids["|']/
|
18
|
-
GEMSPEC = /_dependency\s+["|']uuids["|']/
|
19
|
-
remove_from_file "Gemfile", GEMFILE
|
20
|
-
Dir["*.gemspec"].each { |file| remove_from_file file, GEMSPEC }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def remove_from_file(name, regex)
|
25
|
-
say_with_time name do
|
26
|
-
temp = File.read(name).split("\n").reject { |line| line[regex] }.join "\n"
|
27
|
-
File.write name, temp
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def say(name)
|
32
|
-
print "== #{ name } #{ "=" * (75 - name.count) }\n"
|
33
|
-
yield
|
34
|
-
print "\n"
|
35
|
-
end
|
36
|
-
|
37
|
-
def say_with_time(name)
|
38
|
-
start = seconds
|
39
|
-
print "-- remove from #{ name }\n"
|
40
|
-
yield
|
41
|
-
print " -> #{ seconds - start }s\n"
|
42
|
-
end
|
43
|
-
|
44
|
-
def seconds
|
45
|
-
Time.now.to_f.round(4)
|
46
|
-
end
|
47
7
|
end
|
data/lib/uuids/base.rb
CHANGED
@@ -4,19 +4,6 @@ module Uuids
|
|
4
4
|
module Base
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
# Prevents the module usage outside an ActiveRecord model.
|
8
|
-
#
|
9
|
-
# class Text < String
|
10
|
-
# include Uuids::Base
|
11
|
-
# end
|
12
|
-
# # => raises a TypeError
|
13
|
-
#
|
14
|
-
def self.included(klass)
|
15
|
-
unless klass.ancestors.include? ActiveRecord::Base
|
16
|
-
fail TypeError.new("#{ klass.name } isn't an ActiveRecord model.")
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
7
|
# Methods added to the ActiveRecord model.
|
21
8
|
module ClassMethods
|
22
9
|
|
@@ -24,9 +11,25 @@ module Uuids
|
|
24
11
|
|
25
12
|
# Declares:
|
26
13
|
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
14
|
+
# +uuids+:: association attribute;
|
15
|
+
# +uuid+:: method (string);
|
16
|
+
# <tt>uuid</tt>:: relation scope.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# class City < ActiveRecord::Base
|
20
|
+
# has_uuids
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# city = City.create!
|
24
|
+
#
|
25
|
+
# city.uuids.map(&:value)
|
26
|
+
# # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
|
27
|
+
#
|
28
|
+
# city.uuid
|
29
|
+
# # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
|
30
|
+
#
|
31
|
+
# City.by_uuid("51f50391-fcd2-4f69-aab7-6ef31b29c379").to_a == [city]
|
32
|
+
# # => true
|
30
33
|
#
|
31
34
|
def has_uuids
|
32
35
|
define_uuids
|
@@ -49,14 +52,22 @@ module Uuids
|
|
49
52
|
|
50
53
|
# Defines the <tt>uuid</tt> relation scope.
|
51
54
|
def define_scope
|
52
|
-
scope
|
53
|
-
|
54
|
-
|
55
|
+
scope(
|
56
|
+
:by_uuid,
|
57
|
+
->(value) { joins(:uuids).where(uuids_uuids: { value: value }).uniq }
|
58
|
+
)
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
62
|
private
|
59
63
|
|
64
|
+
# Prevents the module usage outside an ActiveRecord model.
|
65
|
+
def self.included(klass)
|
66
|
+
unless klass.ancestors.include? ActiveRecord::Base
|
67
|
+
fail TypeError.new("#{ klass.name } isn't an ActiveRecord model.")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
60
71
|
# Creates the uuids by default preventing a record from being ureferrable
|
61
72
|
def add_default_uuid
|
62
73
|
uuids.present? || uuids.new
|
data/lib/uuids/version.rb
CHANGED
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|