attr_uuid 1.0.0 → 1.1.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/Gemfile +1 -1
- data/README.md +4 -1
- data/lib/attr_uuid.rb +16 -0
- data/lib/attr_uuid/version.rb +1 -1
- data/spec/config/database.json.sample +2 -2
- data/spec/lib/attr_uuid_spec.rb +86 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51a0b80b942f58b334cbb9458874a24f5e598c84
|
4
|
+
data.tar.gz: 418ae9bd4ca21a79cdeee77914f45a27bff8550b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 998bdb4e35cc4709ac9367ba97f77023049cdd23ccd3999f502bea4bcd90de702b42bebd2e758938390fbe1aae33920224c7dc6e3bf067dcf51c565f58b28524
|
7
|
+
data.tar.gz: a927a2066c26ac3c366276398d99fca182adfaf327ea75a9facb0014f1a3e899dce3d2f4ba502bca7e6f996395050dfe3b20d3d8db0882917a605e211802cdc2
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -52,10 +52,13 @@ Or install it yourself as:
|
|
52
52
|
user.formatted_id #=> "9f648b40-fa6e-11e3-a3ac-0800200c9a66"
|
53
53
|
|
54
54
|
|
55
|
-
5. `attr_uuid` also adds `.find_by_hex_id` and `.
|
55
|
+
5. `attr_uuid` also adds `.find_by_hex_id`, `.find_by_formatted_id`, `.find_all_by_hex_id` and `.find_all_by_formatted_id`
|
56
|
+
methods to retrive model from data store with an uuid string.
|
56
57
|
|
57
58
|
User.find_by_hex_id("337e0576cb664151a2e1e0fc04fb33d1")
|
58
59
|
User.find_by_formatted_id("337e0576-cb66-4151-a2e1-e0fc04fb33d1")
|
60
|
+
User.find_all_by_hex_id(["337e0576cb664151a2e1e0fc04fb33d1", "5a038fcdfcd644b5bffa0245c7cfd7fa", "165a33cff2a947549d960d95adfd119a"])
|
61
|
+
User.find_all_by_formatted_id(["337e0576-cb66-4151-a2e1-e0fc04fb33d1", "5a038fcd-fcd6-44b5-bffa-0245c7cfd7fa", "165a33cf-f2a9-4754-9d96-0d95adfd119a"])
|
59
62
|
|
60
63
|
## Contributing
|
61
64
|
|
data/lib/attr_uuid.rb
CHANGED
@@ -49,6 +49,22 @@ module AttrUuid
|
|
49
49
|
|
50
50
|
if self < ActiveRecord::Base
|
51
51
|
(class << self; self end).class_eval do
|
52
|
+
define_method "find_all_by_formatted_#{name}" do |value|
|
53
|
+
begin
|
54
|
+
ids = (Array.try_convert(value) || []).map { |o| UUIDTools::UUID.parse(o).raw }
|
55
|
+
self.where(column_name => ids)
|
56
|
+
rescue
|
57
|
+
return []
|
58
|
+
end
|
59
|
+
end
|
60
|
+
define_method "find_all_by_hex_#{name}" do |value|
|
61
|
+
begin
|
62
|
+
ids = (Array.try_convert(value) || []).map { |o| UUIDTools::UUID.parse_hexdigest(o).raw }
|
63
|
+
self.where(column_name => ids)
|
64
|
+
rescue
|
65
|
+
return []
|
66
|
+
end
|
67
|
+
end
|
52
68
|
define_method "find_by_formatted_#{name}" do |value|
|
53
69
|
begin
|
54
70
|
uuid = UUIDTools::UUID.parse(value)
|
data/lib/attr_uuid/version.rb
CHANGED
data/spec/lib/attr_uuid_spec.rb
CHANGED
@@ -68,9 +68,9 @@ describe AttrUuid do
|
|
68
68
|
|
69
69
|
describe "#formatted_xxx=" do
|
70
70
|
it "updates original attribute" do
|
71
|
-
|
72
|
-
model.formatted_uuid =
|
73
|
-
expect(model.uuid).to eq
|
71
|
+
o = UUIDTools::UUID.parse("d8354fff-f782-4b86-b4a7-7db46a5426d7")
|
72
|
+
model.formatted_uuid = o.to_s
|
73
|
+
expect(model.uuid).to eq o.raw
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -87,9 +87,9 @@ describe AttrUuid do
|
|
87
87
|
|
88
88
|
describe "#hex_xxx=" do
|
89
89
|
it "updates original attribute" do
|
90
|
-
|
91
|
-
model.hex_uuid =
|
92
|
-
expect(model.uuid).to eq
|
90
|
+
o = UUIDTools::UUID.parse("d8354fff-f782-4b86-b4a7-7db46a5426d7")
|
91
|
+
model.hex_uuid = o.hexdigest
|
92
|
+
expect(model.uuid).to eq o.raw
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
@@ -112,24 +112,28 @@ describe AttrUuid do
|
|
112
112
|
it { expect(model.uuid).to be_nil }
|
113
113
|
end
|
114
114
|
|
115
|
-
|
116
|
-
|
117
|
-
Dummy.new {|o| o.uuid =
|
115
|
+
let(:model) do
|
116
|
+
id = UUIDTools::UUID.parse("faea220a-e94e-442c-9ca0-5b39753e3549")
|
117
|
+
Dummy.new {|o| o.uuid = id.raw }
|
118
118
|
end
|
119
119
|
|
120
120
|
describe ".attr_uuid" do
|
121
|
+
subject { model }
|
121
122
|
it { is_expected.to respond_to :formatted_uuid }
|
122
123
|
it { is_expected.to respond_to :formatted_uuid= }
|
123
124
|
it { is_expected.to respond_to :hex_uuid }
|
124
125
|
it { is_expected.to respond_to :hex_uuid= }
|
126
|
+
it { expect(Dummy).to respond_to :find_all_by_formatted_uuid }
|
127
|
+
it { expect(Dummy).to respond_to :find_all_by_hex_uuid }
|
125
128
|
it { expect(Dummy).to respond_to :find_by_formatted_uuid }
|
126
129
|
it { expect(Dummy).to respond_to :find_by_hex_uuid }
|
127
130
|
end
|
128
131
|
|
129
132
|
describe ".find_by_formatted_xxx" do
|
130
|
-
before { model.save! }
|
131
133
|
subject(:result) { Dummy.find_by_formatted_uuid(uuid) }
|
132
134
|
|
135
|
+
before { model.save! }
|
136
|
+
|
133
137
|
context "when uuid matched" do
|
134
138
|
let(:uuid) { "faea220a-e94e-442c-9ca0-5b39753e3549" }
|
135
139
|
it { expect(result).to eq model }
|
@@ -156,6 +160,76 @@ describe AttrUuid do
|
|
156
160
|
end
|
157
161
|
end
|
158
162
|
|
163
|
+
describe ".find_all_by_formatted_xxx" do
|
164
|
+
subject(:result) { Dummy.find_all_by_formatted_uuid(uuid) }
|
165
|
+
|
166
|
+
let!(:model1) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse("faea220a-e94e-442c-9ca0-5b39753e3549").raw }.tap(&:save!) }
|
167
|
+
let!(:model2) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse("e7a8d36b-9bca-4a82-bebb-5fbd08cac267").raw }.tap(&:save!) }
|
168
|
+
let!(:model3) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse("fd7d3422-450d-4066-8810-5970281879b4").raw }.tap(&:save!) }
|
169
|
+
let!(:model4) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse("97fceb6b-db8d-42fb-b842-4b4371f8e795").raw }.tap(&:save!) }
|
170
|
+
let!(:model5) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse("84ce3004-753f-4667-a9fa-b1177b37d989").raw }.tap(&:save!) }
|
171
|
+
|
172
|
+
context "when uuid matched" do
|
173
|
+
let(:uuid) { ["faea220a-e94e-442c-9ca0-5b39753e3549", "fd7d3422-450d-4066-8810-5970281879b4", "97fceb6b-db8d-42fb-b842-4b4371f8e795" ] }
|
174
|
+
it { expect(result).to eq [model1, model3, model4] }
|
175
|
+
end
|
176
|
+
|
177
|
+
context "when no uuid matched" do
|
178
|
+
let(:uuid) { ["00000000-e94e-442c-9ca0-5b39753e3549"] }
|
179
|
+
it { expect(result).to eq [] }
|
180
|
+
end
|
181
|
+
|
182
|
+
context "when uuid is nil" do
|
183
|
+
let(:uuid) { nil }
|
184
|
+
it { expect(result).to eq [] }
|
185
|
+
end
|
186
|
+
|
187
|
+
context "when uuid isn't String" do
|
188
|
+
let(:uuid) { 1 }
|
189
|
+
it { expect(result).to eq [] }
|
190
|
+
end
|
191
|
+
|
192
|
+
context "when uuid format is invalid" do
|
193
|
+
let(:uuid) { ["invalid"] }
|
194
|
+
it { expect(result).to eq [] }
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe ".find_all_by_hex_xxx" do
|
199
|
+
subject(:result) { Dummy.find_all_by_hex_uuid(uuid) }
|
200
|
+
|
201
|
+
let!(:model1) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse_hexdigest("faea220ae94e442c9ca05b39753e3549").raw }.tap(&:save!) }
|
202
|
+
let!(:model2) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse_hexdigest("e7a8d36b9bca4a82bebb5fbd08cac267").raw }.tap(&:save!) }
|
203
|
+
let!(:model3) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse_hexdigest("fd7d3422450d406688105970281879b4").raw }.tap(&:save!) }
|
204
|
+
let!(:model4) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse_hexdigest("97fceb6bdb8d42fbb8424b4371f8e795").raw }.tap(&:save!) }
|
205
|
+
let!(:model5) { Dummy.new {|o| o.uuid = UUIDTools::UUID.parse_hexdigest("84ce3004753f4667a9fab1177b37d989").raw }.tap(&:save!) }
|
206
|
+
|
207
|
+
context "when uuid matched" do
|
208
|
+
let(:uuid) { ["faea220ae94e442c9ca05b39753e3549", "fd7d3422450d406688105970281879b4", "97fceb6bdb8d42fbb8424b4371f8e795" ] }
|
209
|
+
it { expect(result).to eq [model1, model3, model4] }
|
210
|
+
end
|
211
|
+
|
212
|
+
context "when no uuid matched" do
|
213
|
+
let(:uuid) { ["00000000e94e442c9ca05b39753e3549"] }
|
214
|
+
it { expect(result).to eq [] }
|
215
|
+
end
|
216
|
+
|
217
|
+
context "when uuid is nil" do
|
218
|
+
let(:uuid) { nil }
|
219
|
+
it { expect(result).to eq [] }
|
220
|
+
end
|
221
|
+
|
222
|
+
context "when uuid isn't String" do
|
223
|
+
let(:uuid) { 1 }
|
224
|
+
it { expect(result).to eq [] }
|
225
|
+
end
|
226
|
+
|
227
|
+
context "when uuid format is invalid" do
|
228
|
+
let(:uuid) { ["invalid"] }
|
229
|
+
it { expect(result).to eq [] }
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
159
233
|
describe ".find_by_hex_xxx" do
|
160
234
|
before { model.save! }
|
161
235
|
subject(:result) { Dummy.find_by_hex_uuid(uuid) }
|
@@ -205,8 +279,8 @@ describe AttrUuid do
|
|
205
279
|
end
|
206
280
|
|
207
281
|
subject(:model) do
|
208
|
-
|
209
|
-
Dummy.new {|o| o.x_uuid =
|
282
|
+
id = UUIDTools::UUID.parse("faea220a-e94e-442c-9ca0-5b39753e3549")
|
283
|
+
Dummy.new {|o| o.x_uuid = id.raw }
|
210
284
|
end
|
211
285
|
|
212
286
|
describe ".attr_uuid" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_uuid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shou Takenaka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.4.6
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: Easy to use binay uuid attribute
|