uuids 4.1.7 → 4.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +1 -1
- data/lib/uuids/base/belongs_to.rb +8 -27
- data/lib/uuids/base.rb +3 -3
- data/lib/uuids/version.rb +1 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/uuids/lib/base/belongs_by_uuid_to_spec.rb +73 -0
- data/spec/uuids/lib/base/has_uuids_spec.rb +65 -0
- data/spec/uuids/lib/base_spec.rb +5 -131
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13e36a1b5f35a339e4111da9623e53eec0009ebe
|
4
|
+
data.tar.gz: 629d838eb2a302cdb12cc45f247b81105eb5ee07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e04939b2fc5a1266dfecdd1e9d6cfd7ad07e42c7a0eb8e7e3d594598be53f37ead9424278f9f90cf1c4032a9c5236d26fe5b8e15af4cfc7bc30f0d086c6ae1f0
|
7
|
+
data.tar.gz: 45fc2caa49c7352dac77163c7757310cf56476b08933335575fafb11841ec01c8f21112d3452a089fdd43bf00682fd2832c262776318c1cd2c5b8a1883c35ff2
|
data/README.rdoc
CHANGED
@@ -125,7 +125,7 @@ all object's UUIDs to another record in advance.
|
|
125
125
|
class Street < ActiveRecord::Base
|
126
126
|
include Uuids::Base
|
127
127
|
|
128
|
-
belongs_by_uuid_to :city
|
128
|
+
belongs_by_uuid_to :city
|
129
129
|
end
|
130
130
|
|
131
131
|
This will add both getter and setter for the attribute:
|
@@ -9,7 +9,7 @@ module Uuids
|
|
9
9
|
# city = City.new
|
10
10
|
# city.respond_to? :state # => true
|
11
11
|
# city.respond_to? :state= # => true
|
12
|
-
class BelongsTo < Struct.new(:klass, :name
|
12
|
+
class BelongsTo < Struct.new(:klass, :name)
|
13
13
|
|
14
14
|
# @api hide
|
15
15
|
# @!attribute klass
|
@@ -21,11 +21,6 @@ module Uuids
|
|
21
21
|
# The name of the attribute to be added.
|
22
22
|
# @return [String, Symbol] The name.
|
23
23
|
|
24
|
-
# @api hide
|
25
|
-
# @!attribute model
|
26
|
-
# The model to be referred by uuid.
|
27
|
-
# @return [ActiveRecord::Base] The model class to be referred to.
|
28
|
-
|
29
24
|
# @api hide
|
30
25
|
# @!attribute [r] class_name
|
31
26
|
# The name of the {#klass} constant.
|
@@ -34,14 +29,6 @@ module Uuids
|
|
34
29
|
@class_name ||= klass.name
|
35
30
|
end
|
36
31
|
|
37
|
-
# @api hide
|
38
|
-
# @!attribute [r] model_name
|
39
|
-
# The name of the {#model} constant.
|
40
|
-
# @return [String] The {#model} name.
|
41
|
-
def model_name
|
42
|
-
@model_name ||= model.name
|
43
|
-
end
|
44
|
-
|
45
32
|
# @api hide
|
46
33
|
# @!attribute [r] model_name
|
47
34
|
# The name of the {#klass} db table column to store reference to the
|
@@ -57,11 +44,9 @@ module Uuids
|
|
57
44
|
# @param [ActiveRecord::Base] klass The model to add attribute to.
|
58
45
|
# Should have a corresponding column for reference by uuid.
|
59
46
|
# @param [String, Symbol] name The name of the attribute to be added.
|
60
|
-
# @param [ActiveRecord::Base] model The model to be referred by uuid.
|
61
|
-
# Should include the <tt>Uuids::Base</tt> module.
|
62
47
|
# @raise (see Uuids::Base::BelongsTo#add)
|
63
|
-
def self.add(klass, name
|
64
|
-
new(klass, name
|
48
|
+
def self.add(klass, name)
|
49
|
+
new(klass, name).add
|
65
50
|
end
|
66
51
|
|
67
52
|
# @api hide
|
@@ -71,7 +56,6 @@ module Uuids
|
|
71
56
|
# @raise [NotImplementedError] if the {#klass} doesn't have the
|
72
57
|
# correspondint {#column_name} column.
|
73
58
|
def add
|
74
|
-
check_model
|
75
59
|
check_column
|
76
60
|
add_getter
|
77
61
|
add_setter
|
@@ -79,12 +63,6 @@ module Uuids
|
|
79
63
|
|
80
64
|
private
|
81
65
|
|
82
|
-
def check_model
|
83
|
-
return if model.ancestors.include? Uuids::Base
|
84
|
-
fail TypeError
|
85
|
-
.new "#{ model_name } class doesn't include the Uuids::Base module"
|
86
|
-
end
|
87
|
-
|
88
66
|
def check_column
|
89
67
|
return if klass.attribute_names.include? column_name
|
90
68
|
fail NotImplementedError
|
@@ -99,6 +77,10 @@ module Uuids
|
|
99
77
|
klass.class_eval setter
|
100
78
|
end
|
101
79
|
|
80
|
+
def uuids
|
81
|
+
"Uuids::Models::Uuid"
|
82
|
+
end
|
83
|
+
|
102
84
|
def getter
|
103
85
|
"def #{ name };
|
104
86
|
uuid = read_attribute :#{ column_name };
|
@@ -106,14 +88,13 @@ module Uuids
|
|
106
88
|
if Array(@#{ name }.try(:uuids)).map(&:value).include? uuid;
|
107
89
|
@#{ name };
|
108
90
|
else;
|
109
|
-
@#{ name } = #{
|
91
|
+
@#{ name } = #{ uuids }.where(value: uuid).first.try :record;
|
110
92
|
end;
|
111
93
|
end"
|
112
94
|
end
|
113
95
|
|
114
96
|
def setter
|
115
97
|
"def #{ name }=(value);
|
116
|
-
fail TypeError unless value.nil? || value.is_a?(#{ model_name });
|
117
98
|
write_attribute :#{ column_name }, value.try(:uuid);
|
118
99
|
#{ name };
|
119
100
|
end"
|
data/lib/uuids/base.rb
CHANGED
@@ -74,9 +74,9 @@ module Uuids
|
|
74
74
|
# @param [Hash] options The options for the association.
|
75
75
|
# @option options [Class] :class The model of the attribute.
|
76
76
|
# @option options [String] :class_name The name of the attribute's model.
|
77
|
-
def belongs_by_uuid_to(name,
|
78
|
-
|
79
|
-
BelongsTo.add self, name
|
77
|
+
def belongs_by_uuid_to(name, opts = nil)
|
78
|
+
warn "[DEPRECATION] .belongs_by_uuid_to options are deprecated" if opts
|
79
|
+
BelongsTo.add self, name
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
data/lib/uuids/version.rb
CHANGED
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Uuids
|
4
|
+
describe "Base.belongs_by_uuid_to" do
|
5
|
+
|
6
|
+
context "when a corresponding _uuid column is absent" do
|
7
|
+
|
8
|
+
it "raises NotImplementedError" do
|
9
|
+
# The city has no "country_uuid" column
|
10
|
+
expect { City.send :belongs_by_uuid_to, :country }
|
11
|
+
.to raise_error { NotImplementedError }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context ":state" do
|
16
|
+
|
17
|
+
let!(:state) { State.create uuid: SecureRandom.uuid }
|
18
|
+
|
19
|
+
before { City.send :belongs_by_uuid_to, :state }
|
20
|
+
subject { City.new }
|
21
|
+
|
22
|
+
describe "#state" do
|
23
|
+
|
24
|
+
it "is defined" do
|
25
|
+
expect(subject).to respond_to :state
|
26
|
+
expect(subject.method(:state).arity).to eq 0
|
27
|
+
end
|
28
|
+
|
29
|
+
it "corresponds to #state_uuid" do
|
30
|
+
expect { subject.state_uuid = state.uuid }
|
31
|
+
.to change { subject.state }.to state
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#state=" do
|
36
|
+
|
37
|
+
it "is defined" do
|
38
|
+
expect(subject).to respond_to :state=
|
39
|
+
expect(subject.method(:state=).arity).to eq 1
|
40
|
+
end
|
41
|
+
|
42
|
+
it "sets the #state" do
|
43
|
+
expect { subject.state = state }.to change { subject.state }.to state
|
44
|
+
end
|
45
|
+
|
46
|
+
it "sets the #state_uuid" do
|
47
|
+
expect { subject.state = state }
|
48
|
+
.to change { subject.state_uuid }.to state.uuid
|
49
|
+
end
|
50
|
+
|
51
|
+
it "resets the #state" do
|
52
|
+
subject.state = state
|
53
|
+
expect { subject.state = nil }.to change { subject.state }.to nil
|
54
|
+
end
|
55
|
+
|
56
|
+
it "resets the #state_uuid" do
|
57
|
+
subject.state = state
|
58
|
+
expect { subject.state = nil }.to change { subject.state_uuid }.to nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# it "resets the object uuid" do
|
63
|
+
# subject.state = state
|
64
|
+
# expect { subject.state = nil }.to change { subject.state_uuid }.to nil
|
65
|
+
# end
|
66
|
+
|
67
|
+
# it "sets the object by uuid" do
|
68
|
+
# expect { subject.state_uuid = state.uuid }
|
69
|
+
# .to change { subject.state }.to state
|
70
|
+
# end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Uuids
|
4
|
+
describe "Base.has_uuids" do
|
5
|
+
|
6
|
+
# Dependencies
|
7
|
+
let(:uuids_class) { Uuids::Models::Uuid }
|
8
|
+
|
9
|
+
# For this specification, the City AR model has been prepared in the
|
10
|
+
# `spec dummy/app/models/city.rb`. The model included Uuids::Base.
|
11
|
+
before { City.send :has_uuids }
|
12
|
+
subject { City.new }
|
13
|
+
|
14
|
+
it "defines the #uuids attribute" do
|
15
|
+
subject.uuids.new value: SecureRandom.uuid
|
16
|
+
subject.save!
|
17
|
+
expect(uuids_class.first.try(:record)).to eq subject
|
18
|
+
expect(subject.uuids.first).to eq uuids_class.first
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defines the #uuid method" do
|
22
|
+
2.times { subject.uuids.new value: SecureRandom.uuid }
|
23
|
+
subject.save!
|
24
|
+
expect(subject.uuid).to eq subject.uuids.map(&:value).sort.first
|
25
|
+
end
|
26
|
+
|
27
|
+
it "defines the #uuid= method" do
|
28
|
+
value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
29
|
+
subject.uuid = value
|
30
|
+
expect(subject.uuids.map(&:value)).to include(value)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "defines the #uuids= method" do
|
34
|
+
value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
35
|
+
subject.uuids = [value]
|
36
|
+
expect(subject.uuids.map(&:value)).to include(value)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "defines +by_uuid+ class scope" do
|
40
|
+
subject.save!
|
41
|
+
subject.uuids.create!
|
42
|
+
values = subject.reload.uuids.map(&:value)
|
43
|
+
expect(City.by_uuid(*values).to_a).to eq [subject]
|
44
|
+
expect(City.by_uuid).to eq City.all
|
45
|
+
end
|
46
|
+
|
47
|
+
it "creates the first uuid by default" do
|
48
|
+
expect(subject.uuid).to be_present
|
49
|
+
end
|
50
|
+
|
51
|
+
it "requires the #uuids attribute" do
|
52
|
+
subject.save!
|
53
|
+
subject.uuids.each(&:delete)
|
54
|
+
expect(subject.reload).not_to be_valid
|
55
|
+
end
|
56
|
+
|
57
|
+
it "forbids destruction if uuids present" do
|
58
|
+
subject.save!
|
59
|
+
expect { subject.destroy! }.to raise_error
|
60
|
+
subject.uuids.each(&:delete)
|
61
|
+
subject.reload
|
62
|
+
expect { subject.destroy! }.not_to raise_error
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/uuids/lib/base_spec.rb
CHANGED
@@ -3,149 +3,23 @@ require "spec_helper"
|
|
3
3
|
module Uuids
|
4
4
|
describe Base do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
subject { Uuids::Base }
|
7
|
+
before { class Test; end }
|
8
|
+
after { begin; Uuids.send :remove_constant, :Test; rescue; end }
|
9
9
|
|
10
10
|
describe ".included" do
|
11
11
|
|
12
12
|
context "from non AR class" do
|
13
13
|
|
14
|
-
before { class Test; end }
|
15
|
-
after do
|
16
|
-
begin; described_module.send :remove_constant, :Test; rescue; end
|
17
|
-
end
|
18
|
-
|
19
14
|
it "fails" do
|
20
|
-
expect { Test.include
|
15
|
+
expect { Test.include subject }.to raise_error
|
21
16
|
end
|
22
17
|
end
|
23
18
|
|
24
19
|
context "from AR model" do
|
25
20
|
|
26
21
|
it "doesn't fail" do
|
27
|
-
expect { City.include
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe ".has_uuids" do
|
33
|
-
|
34
|
-
# For this specification, the City AR model has been prepared in the
|
35
|
-
# `spec dummy/app/models/city.rb`. The model included Uuids::Base.
|
36
|
-
before { City.send :has_uuids }
|
37
|
-
subject { City.new }
|
38
|
-
|
39
|
-
it "defines the #uuids attribute" do
|
40
|
-
subject.uuids.new value: SecureRandom.uuid
|
41
|
-
subject.save!
|
42
|
-
expect(uuids_class.first.try(:record)).to eq subject
|
43
|
-
expect(subject.uuids.first).to eq uuids_class.first
|
44
|
-
end
|
45
|
-
|
46
|
-
it "defines the #uuid method" do
|
47
|
-
2.times { subject.uuids.new value: SecureRandom.uuid }
|
48
|
-
subject.save!
|
49
|
-
expect(subject.uuid).to eq subject.uuids.map(&:value).sort.first
|
50
|
-
end
|
51
|
-
|
52
|
-
it "defines the #uuid= method" do
|
53
|
-
value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
54
|
-
subject.uuid = value
|
55
|
-
expect(subject.uuids.map(&:value)).to include(value)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "defines the #uuids= method" do
|
59
|
-
value = "3ea8fd89-232f-4ed1-90b5-743da173cd7d"
|
60
|
-
subject.uuids = [value]
|
61
|
-
expect(subject.uuids.map(&:value)).to include(value)
|
62
|
-
end
|
63
|
-
|
64
|
-
it "defines +by_uuid+ class scope" do
|
65
|
-
subject.save!
|
66
|
-
subject.uuids.create!
|
67
|
-
values = subject.reload.uuids.map(&:value)
|
68
|
-
expect(City.by_uuid(*values).to_a).to eq [subject]
|
69
|
-
expect(City.by_uuid).to eq City.all
|
70
|
-
end
|
71
|
-
|
72
|
-
it "creates the first uuid by default" do
|
73
|
-
expect(subject.uuid).to be_present
|
74
|
-
end
|
75
|
-
|
76
|
-
it "requires the #uuids attribute" do
|
77
|
-
subject.save!
|
78
|
-
subject.uuids.each(&:delete)
|
79
|
-
expect(subject.reload).not_to be_valid
|
80
|
-
end
|
81
|
-
|
82
|
-
it "forbids destruction if uuids present" do
|
83
|
-
subject.save!
|
84
|
-
expect { subject.destroy! }.to raise_error
|
85
|
-
subject.uuids.each(&:delete)
|
86
|
-
subject.reload
|
87
|
-
expect { subject.destroy! }.not_to raise_error
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe ".belongs_by_uuid_to" do
|
92
|
-
|
93
|
-
context "when a model doesn't include Uuids::Base" do
|
94
|
-
|
95
|
-
it "raises TypeError" do
|
96
|
-
expect { City.send :belongs_by_uuid_to, :state, class: Record }
|
97
|
-
.to raise_error { TypeError }
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
context "when a class doesn't have column for uuids" do
|
102
|
-
|
103
|
-
it "raises NotImplementedError" do
|
104
|
-
expect { City.send :belongs_by_uuid_to, :country, class: State }
|
105
|
-
.to raise_error { NotImplementedError }
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context "with valid arguments" do
|
110
|
-
|
111
|
-
before { City.send :belongs_by_uuid_to, :state, class: State }
|
112
|
-
subject { City.new }
|
113
|
-
|
114
|
-
let!(:uuid) { "01234567-89ab-cdef-0123-456789abcdef" }
|
115
|
-
let!(:state) { State.create uuid: uuid }
|
116
|
-
|
117
|
-
it "defines the getter" do
|
118
|
-
expect(subject).to respond_to :state
|
119
|
-
expect(subject.method(:state).arity).to eq 0
|
120
|
-
end
|
121
|
-
|
122
|
-
it "defines the setter" do
|
123
|
-
expect(subject).to respond_to :state=
|
124
|
-
expect(subject.method(:state=).arity).to eq 1
|
125
|
-
end
|
126
|
-
|
127
|
-
it "sets the object" do
|
128
|
-
expect { subject.state = state }.to change { subject.state }.to state
|
129
|
-
end
|
130
|
-
|
131
|
-
it "sets the object uuid" do
|
132
|
-
expect { subject.state = state }
|
133
|
-
.to change { subject.state_uuid }.to state.uuid
|
134
|
-
end
|
135
|
-
|
136
|
-
it "resets the object" do
|
137
|
-
subject.state = state
|
138
|
-
expect { subject.state = nil }.to change { subject.state }.to nil
|
139
|
-
end
|
140
|
-
|
141
|
-
it "resets the object uuid" do
|
142
|
-
subject.state = state
|
143
|
-
expect { subject.state = nil }.to change { subject.state_uuid }.to nil
|
144
|
-
end
|
145
|
-
|
146
|
-
it "sets the object by uuid" do
|
147
|
-
expect { subject.state_uuid = state.uuid }
|
148
|
-
.to change { subject.state }.to state
|
22
|
+
expect { City.include subject }.not_to raise_error
|
149
23
|
end
|
150
24
|
end
|
151
25
|
end
|
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.1.
|
4
|
+
version: 4.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hexx-active_record
|
@@ -256,6 +256,8 @@ files:
|
|
256
256
|
- spec/support/runtime/config/factory_girl.rb
|
257
257
|
- spec/support/runtime/config/i18n.rb
|
258
258
|
- spec/uuids/bin/uuids_spec.rb
|
259
|
+
- spec/uuids/lib/base/belongs_by_uuid_to_spec.rb
|
260
|
+
- spec/uuids/lib/base/has_uuids_spec.rb
|
259
261
|
- spec/uuids/lib/base_spec.rb
|
260
262
|
- spec/uuids/models/uuid_spec.rb
|
261
263
|
- spec/uuids/services/add_spec.rb
|
@@ -285,6 +287,8 @@ specification_version: 4
|
|
285
287
|
summary: UUIDs AR model.
|
286
288
|
test_files:
|
287
289
|
- spec/uuids/models/uuid_spec.rb
|
290
|
+
- spec/uuids/lib/base/belongs_by_uuid_to_spec.rb
|
291
|
+
- spec/uuids/lib/base/has_uuids_spec.rb
|
288
292
|
- spec/uuids/lib/base_spec.rb
|
289
293
|
- spec/uuids/services/add_spec.rb
|
290
294
|
- spec/uuids/bin/uuids_spec.rb
|