uuids 1.4.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/uuids/uuid.rb +24 -22
- data/bin/uuids +1 -0
- data/db/migrate/20141016112506_create_uuids_uuids.rb +3 -0
- data/lib/uuids/base/has_uuids.rb +33 -39
- data/lib/uuids/base.rb +24 -25
- data/lib/uuids/version.rb +1 -1
- data/lib/uuids.rb +2 -2
- data/spec/app/models/uuids/uuid_spec.rb +0 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/lib/uuids/base_spec.rb +5 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47eac9c29e60c9923b3ccc38491475e2090447f4
|
4
|
+
data.tar.gz: da49a4fb5fe1cac362c92521d2a5a79691f97a2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8fd6392d538cb0b819b1d3a54e691b180a60b50f85142cd69b855f3b471782b794eadbd5aeace541ee0b69b866e88f2f0a495047f0bd48462aca24afc59db6c
|
7
|
+
data.tar.gz: c9823e9694e4b849ee54f78b64aa96804830d43d0963c887dbd5b3c3a2db8987f301dda4c18f71b8381b999b15b9a670723ead18703c8db475b91aa91059fd1e
|
data/app/models/uuids/uuid.rb
CHANGED
@@ -1,38 +1,40 @@
|
|
1
1
|
module Uuids
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# {RFC4122}[http://www.ietf.org/rfc/rfc4122.txt].
|
8
|
-
# Assigned by default on creation. Cannot be set or edited manually.
|
9
|
-
# +record+:: An AR record the uuid is assigned to.
|
10
|
-
# Required attribute. Can be changed.
|
3
|
+
# The model describes uuids assigned to corresponding records.
|
4
|
+
# @see http://www.ietf.org/rfc/rfc4122.txt RFC4122 standard.
|
5
|
+
# @see https://en.wikipedia.org/wiki/Universally_unique_identifier Wikipedia
|
6
|
+
# article.
|
11
7
|
#
|
12
8
|
# @example
|
13
|
-
#
|
14
|
-
#
|
9
|
+
# # Creates the uuid
|
10
|
+
# uuid = Uuids::Uuid.create! record: some_record
|
15
11
|
#
|
16
|
-
#
|
17
|
-
#
|
12
|
+
# # Finds a record by uuid
|
13
|
+
# Uuid.find_by_value(uuid.value).record # => some_record
|
18
14
|
#
|
19
|
-
#
|
20
|
-
#
|
15
|
+
# # Reassigns a uuid to another record
|
16
|
+
# uuid.update_attributes! record: another_record
|
21
17
|
#
|
22
18
|
class Uuid < ActiveRecord::Base
|
23
19
|
self.table_name = :uuids_uuids
|
24
20
|
|
25
|
-
#
|
26
|
-
|
21
|
+
# @!attribute [r] value
|
22
|
+
# Assigned by default on creation. Cannot be set or edited manually.
|
23
|
+
# @return [String] uuid value as defined in RFC4122.
|
24
|
+
|
25
|
+
UUID = /[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}/
|
26
|
+
attr_readonly :value
|
27
|
+
validates :value, format: { with: UUID }, allow_nil: true
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
# @!attribute record
|
30
|
+
# @return [ActiveRecord::Base] an object the uuid is assigned to.
|
30
31
|
|
31
|
-
|
32
|
-
validate
|
32
|
+
belongs_to :record, polymorphic: true
|
33
|
+
validate :record_present?
|
33
34
|
|
34
|
-
|
35
|
-
|
35
|
+
# Callbacks
|
36
|
+
after_initialize :set_default_value
|
37
|
+
before_destroy :forbid_destruction
|
36
38
|
|
37
39
|
private
|
38
40
|
|
data/bin/uuids
CHANGED
data/lib/uuids/base/has_uuids.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Uuids
|
2
2
|
module Base
|
3
3
|
|
4
|
-
# Defines
|
4
|
+
# Defines methods to be added by the +has_uuids+ class helper.
|
5
5
|
module HasUuids
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
@@ -10,25 +10,20 @@ module Uuids
|
|
10
10
|
|
11
11
|
# Selects records by uuid.
|
12
12
|
#
|
13
|
-
# by_uuid(*values)
|
14
|
-
#
|
15
13
|
# @example
|
14
|
+
# class MyRecord < ActiveRecord::Base
|
15
|
+
# include Uuids::Base
|
16
|
+
# has_uuids
|
17
|
+
# end
|
16
18
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# MyRecord.by_uuid(
|
23
|
-
# "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
|
24
|
-
# "9223238d-a7e3-2d36-a93d-1e50fea02de2"
|
25
|
-
# )
|
26
|
-
# # => #<ActiveRecord::Relation ...>
|
27
|
-
#
|
28
|
-
# Params:
|
29
|
-
# <tt>*values</tt>:: a list of uuids to select records by.
|
19
|
+
# MyRecord.by_uuid(
|
20
|
+
# "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
|
21
|
+
# "9223238d-a7e3-2d36-a93d-1e50fea02de2"
|
22
|
+
# )
|
23
|
+
# # => #<ActiveRecord::Relation ...>
|
30
24
|
#
|
31
|
-
#
|
25
|
+
# @param values [Array<String>] a list of uuids to select records by.
|
26
|
+
# @return [ActiveRecord::Relation] scope.
|
32
27
|
def by_uuid(*values)
|
33
28
|
first_value = values.first
|
34
29
|
list = first_value.is_a?(Array) ? first_value : values
|
@@ -39,10 +34,11 @@ module Uuids
|
|
39
34
|
# Returns the first UUID (sorted as a string) for the record.
|
40
35
|
#
|
41
36
|
# @example
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
37
|
+
# record.uuids.new value: "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
|
38
|
+
# record.uuids.new value: "9223238d-a7e3-2d36-a93d-1e50fea02de2"
|
39
|
+
# record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
|
45
40
|
#
|
41
|
+
# @return [String] first value of the record's uuids.
|
46
42
|
def uuid
|
47
43
|
uuids.map(&:value).sort.first
|
48
44
|
end
|
@@ -50,12 +46,11 @@ module Uuids
|
|
50
46
|
# Assigns a new uuid to the record.
|
51
47
|
#
|
52
48
|
# @example
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
# Params:
|
57
|
-
# <tt>value</tt>:: a string value of uuid to assign.
|
49
|
+
# record.uuid = "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
|
50
|
+
# record.uuid # => "23423fe3-28d8-a1e5-bde3-2e08074aa92d"
|
58
51
|
#
|
52
|
+
# @param value [String] a value of uuid to assign.
|
53
|
+
# @return object
|
59
54
|
def uuid=(value)
|
60
55
|
uuids.new value: value
|
61
56
|
end
|
@@ -63,19 +58,18 @@ module Uuids
|
|
63
58
|
# Assigns a list of new uuids to the record.
|
64
59
|
#
|
65
60
|
# @example
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
# Params:
|
77
|
-
# <tt>values</tt>:: an array of uuids string values to assign.
|
61
|
+
# record.uuids = [
|
62
|
+
# "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
|
63
|
+
# "9223238d-a7e3-2d36-a93d-1e50fea02de2"
|
64
|
+
# ]
|
65
|
+
# record.uuids.map(&:value)
|
66
|
+
# # => [
|
67
|
+
# "23423fe3-28d8-a1e5-bde3-2e08074aa92d",
|
68
|
+
# "9223238d-a7e3-2d36-a93d-1e50fea02de2"
|
69
|
+
# ]
|
78
70
|
#
|
71
|
+
# @param values [Array<String>] an array of uuids string values to assign
|
72
|
+
# @return object.
|
79
73
|
def uuids=(*values)
|
80
74
|
first = values.first
|
81
75
|
list = first.is_a?(Array) ? first : values
|
@@ -84,12 +78,12 @@ module Uuids
|
|
84
78
|
|
85
79
|
private
|
86
80
|
|
87
|
-
# Creates the
|
81
|
+
# Creates the uuid value by default.
|
88
82
|
def add_default_uuid
|
89
83
|
uuids.present? || uuids.new
|
90
84
|
end
|
91
85
|
|
92
|
-
# Prevents destruction of a record before its uuids reassigned to
|
86
|
+
# Prevents destruction of a record before its uuids are reassigned to
|
93
87
|
# other records.
|
94
88
|
def prevent_destruction
|
95
89
|
return true if uuids.blank?
|
data/lib/uuids/base.rb
CHANGED
@@ -2,7 +2,7 @@ require_relative "base/has_uuids"
|
|
2
2
|
|
3
3
|
module Uuids
|
4
4
|
|
5
|
-
#
|
5
|
+
# Defines the +has_uuids+ model class helper.
|
6
6
|
module Base
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
@@ -11,42 +11,41 @@ module Uuids
|
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
#
|
14
|
+
# The helper defines:
|
15
15
|
#
|
16
|
-
# +uuids+:: association
|
17
|
-
# +uuid+::
|
18
|
-
# <tt>uuid=</tt>:: new uuid
|
19
|
-
# <tt>uuids=</tt>::
|
20
|
-
# <tt>
|
16
|
+
# +uuids+:: the +ActiveRecord+ association with a record's uuids.
|
17
|
+
# +uuid+:: a virtual attribute that returns the first value of uuids.
|
18
|
+
# <tt>uuid=</tt>:: new uuid setter.
|
19
|
+
# <tt>uuids=</tt>:: uuids group setter.
|
20
|
+
# <tt>by_uuid</tt>:: the +ActiveRecord+ relation scope.
|
21
21
|
#
|
22
22
|
# @example
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
23
|
+
# class City < ActiveRecord::Base
|
24
|
+
# has_uuids
|
25
|
+
# end
|
26
26
|
#
|
27
|
-
#
|
27
|
+
# city = City.create!
|
28
28
|
#
|
29
|
-
#
|
30
|
-
#
|
29
|
+
# city.uuids.map(&:value)
|
30
|
+
# # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
|
31
31
|
#
|
32
|
-
#
|
33
|
-
#
|
32
|
+
# city.uuid
|
33
|
+
# # => 51f50391-fcd2-4f69-aab7-6ef31b29c379
|
34
34
|
#
|
35
|
-
#
|
35
|
+
# city.uuid = "51f50391-fcd2-4f69-aab7-6ef31b29c379"
|
36
36
|
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
# City.by_uuid("51f50391-fcd2-4f69-aab7-6ef31b29c379").to_a == [city]
|
43
|
-
# # => true
|
37
|
+
# city.uuids = [
|
38
|
+
# "51f50391-fcd2-4f69-aab7-6ef31b29c379",
|
39
|
+
# "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
40
|
+
# ]
|
44
41
|
#
|
42
|
+
# City.by_uuid("51f50391-fcd2-4f69-aab7-6ef31b29c379").to_a == [city]
|
43
|
+
# # => true
|
45
44
|
def has_uuids
|
46
45
|
has_many :uuids, class_name: "Uuids::Uuid", as: :record, validate: false
|
47
46
|
include Uuids::Base::HasUuids
|
48
|
-
|
49
|
-
before_destroy
|
47
|
+
after_initialize :add_default_uuid
|
48
|
+
before_destroy :prevent_destruction
|
50
49
|
validates :uuids, presence: true, on: :update
|
51
50
|
end
|
52
51
|
end
|
data/lib/uuids/version.rb
CHANGED
data/lib/uuids.rb
CHANGED
@@ -3,11 +3,11 @@ require "active_record"
|
|
3
3
|
require "i18n"
|
4
4
|
require "securerandom"
|
5
5
|
|
6
|
-
#
|
6
|
+
# The +uuids+ gem content.
|
7
7
|
root = File.expand_path "../..", __FILE__
|
8
8
|
Dir[File.join(root, "lib/uuids/**/*.rb")].each { |f| require f }
|
9
9
|
Dir[File.join(root, "app/**/*.rb")].each { |f| require f }
|
10
10
|
|
11
|
-
# Namespace for
|
11
|
+
# Namespace for the +uuids+ gem code.
|
12
12
|
module Uuids
|
13
13
|
end
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
data/spec/lib/uuids/base_spec.rb
CHANGED
@@ -45,17 +45,14 @@ module Uuids
|
|
45
45
|
|
46
46
|
it "defines the #uuid= method" do
|
47
47
|
value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
48
|
-
|
49
|
-
|
48
|
+
subject.uuid = value
|
49
|
+
expect(subject.uuids.map(&:value)).to be_include(value)
|
50
50
|
end
|
51
51
|
|
52
52
|
it "defines the #uuids= method" do
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
]
|
57
|
-
expect { subject.uuids = values }
|
58
|
-
.to change { subject.uuids.map(&:value) }.to values
|
53
|
+
value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
54
|
+
subject.uuids = [value]
|
55
|
+
expect(subject.uuids.map(&:value)).to be_include(value)
|
59
56
|
end
|
60
57
|
|
61
58
|
it "defines +by_uuid+ class scope" do
|
@@ -66,7 +63,6 @@ module Uuids
|
|
66
63
|
end
|
67
64
|
|
68
65
|
it "creates the first uuid by default" do
|
69
|
-
subject.save!
|
70
66
|
expect(subject.uuid).not_to be_blank
|
71
67
|
end
|
72
68
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uuids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|