attr_uuid 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +1 -0
- data/attr_uuid.gemspec +27 -0
- data/lib/attr_uuid.rb +82 -0
- data/lib/attr_uuid/railtie.rb +11 -0
- data/lib/attr_uuid/version.rb +3 -0
- data/spec/config/database.json.sample +7 -0
- data/spec/db/create_database.sql +1 -0
- data/spec/lib/attr_uuid_spec.rb +291 -0
- data/spec/spec_helper.rb +18 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 203bfad60d62588e004febd0e0da42650e7d5f07
|
4
|
+
data.tar.gz: a97228bf96e15b6016f8e718fa0da4decc9a0078
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b2a471a2ca67c0d1085242449b8d7e74d943bd1a3ddfca08dce40598ff8004ec7174c5aa9e3b70e6ebc82f6748d56be2cbb0254cdc5cc02492aa8caf63d9df4
|
7
|
+
data.tar.gz: b653111918be2f358917c557393e9160796b009d367e32a332f5e9eb814b2883e581282270f94a34128e36ba442d7c38ee976658b685e58a9b44dcb6be79f9e4
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
*.swp
|
4
|
+
.bundle
|
5
|
+
.config
|
6
|
+
.rspec
|
7
|
+
.yardoc
|
8
|
+
Gemfile.lock
|
9
|
+
InstalledFiles
|
10
|
+
_yardoc
|
11
|
+
coverage
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/config/database.json
|
17
|
+
spec/reports
|
18
|
+
test/tmp
|
19
|
+
test/version_tmp
|
20
|
+
tmp
|
21
|
+
vendor/bundle/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Shou Takenaka
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# attr\_uuid
|
2
|
+
|
3
|
+
attr\_uuid makes binary uuid attribute easy to use.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'attr_uuid'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install attr_uuid
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
1. In a migration file, prevent generate id column and add uuid column (tinyblob).
|
22
|
+
|
23
|
+
class CreateUsers < ActiveRecord::Migration
|
24
|
+
def up
|
25
|
+
create_table :users, :id => false do |t|
|
26
|
+
t.column :uuid, :tinyblob
|
27
|
+
t.string :user_name
|
28
|
+
end
|
29
|
+
execute "ALTER TABLE users ADD PRIMARY KEY (`uuid`(16))"
|
30
|
+
end
|
31
|
+
def down
|
32
|
+
drop_table :users
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
2. Change primary\_key to uuid, and call `attr_uuid` method in a model class. When you pass `autofill: true` to `attr_uuid`, it hooks `before_create` callback to set automatically generated uuid as 16 byte binary data to id attirbute.
|
37
|
+
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
self.primary_key = "uuid"
|
40
|
+
attr_uuid :id, column_name: "uuid", autofill: true
|
41
|
+
end
|
42
|
+
|
43
|
+
3. `attr_uuid` adds `#hex_id`, `#hex_id=`, `#formatted_id` and `#formatted_id=` to the model to refer formatted uuid.
|
44
|
+
|
45
|
+
user = User.create(:user_name => "foo")
|
46
|
+
user.id #=> "3~\x05v\xCBfAQ\xA2\xE1\xE0\xFC\x04\xFB3\xD1"
|
47
|
+
user.hex_id #=> "337e0576cb664151a2e1e0fc04fb33d1"
|
48
|
+
user.formatted_id #=> "337e0576-cb66-4151-a2e1-e0fc04fb33d1"
|
49
|
+
|
50
|
+
user.hex_id = "9f648b40fa6e11e3a3ac0800200c9a66"
|
51
|
+
user.id #=> "\x9Fd\x8B@\xFAn\x11\xE3\xA3\xAC\b\x00 \f\x9Af"
|
52
|
+
user.formatted_id #=> "9f648b40-fa6e-11e3-a3ac-0800200c9a66"
|
53
|
+
|
54
|
+
|
55
|
+
5. `attr_uuid` also adds `.find_by_hex_id` and `.find_by_formatted_id` method to retrive model from data store with an uuid string.
|
56
|
+
|
57
|
+
User.find_by_hex_id("337e0576cb664151a2e1e0fc04fb33d1")
|
58
|
+
User.find_by_formatted_id("337e0576-cb66-4151-a2e1-e0fc04fb33d1")
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/attr_uuid.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'attr_uuid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "attr_uuid"
|
8
|
+
gem.version = AttrUuid::VERSION
|
9
|
+
gem.authors = ["Shou Takenaka"]
|
10
|
+
gem.email = ["sasaminn@gmail.com"]
|
11
|
+
gem.description = %q{This gem makes binary uuid attribute easy to use}
|
12
|
+
gem.summary = %q{Easy to use binay uuid attribute}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "activerecord"
|
21
|
+
gem.add_dependency "uuidtools"
|
22
|
+
|
23
|
+
gem.add_development_dependency "mysql2"
|
24
|
+
gem.add_development_dependency "rspec"
|
25
|
+
gem.add_development_dependency "simplecov"
|
26
|
+
gem.add_development_dependency "with_model"
|
27
|
+
end
|
data/lib/attr_uuid.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require "attr_uuid/version"
|
2
|
+
require "active_record"
|
3
|
+
require "uuidtools"
|
4
|
+
|
5
|
+
module AttrUuid
|
6
|
+
require "attr_uuid/railtie" if defined?(Rails)
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send :extend, ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def attr_uuid(name, options = {})
|
14
|
+
name = name.to_s if name.is_a?(Symbol)
|
15
|
+
column_name = (options[:column_name] || name)
|
16
|
+
column_name = column_name.to_s if column_name.is_a?(Symbol)
|
17
|
+
|
18
|
+
if !name.is_a?(String) || !column_name.is_a?(String)
|
19
|
+
return
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method "formatted_#{name}" do
|
23
|
+
binary = self.send(column_name)
|
24
|
+
if binary.nil?
|
25
|
+
return nil
|
26
|
+
else
|
27
|
+
return UUIDTools::UUID.parse_raw(binary).to_s
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method "formatted_#{name}=" do |value|
|
32
|
+
uuid = UUIDTools::UUID.parse(value)
|
33
|
+
self.send("#{column_name}=", uuid.raw)
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method "hex_#{name}" do
|
37
|
+
binary = self.send(column_name)
|
38
|
+
if binary.nil?
|
39
|
+
return nil
|
40
|
+
else
|
41
|
+
return UUIDTools::UUID.parse_raw(binary).hexdigest
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method "hex_#{name}=" do |value|
|
46
|
+
uuid = UUIDTools::UUID.parse_hexdigest(value)
|
47
|
+
self.send("#{column_name}=", uuid.raw)
|
48
|
+
end
|
49
|
+
|
50
|
+
if self < ActiveRecord::Base
|
51
|
+
(class << self; self end).class_eval do
|
52
|
+
define_method "find_by_formatted_#{name}" do |value|
|
53
|
+
begin
|
54
|
+
uuid = UUIDTools::UUID.parse(value)
|
55
|
+
return self.send("find_by_#{column_name}", uuid.raw)
|
56
|
+
rescue
|
57
|
+
return nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
define_method "find_by_hex_#{name}" do |value|
|
61
|
+
begin
|
62
|
+
uuid = UUIDTools::UUID.parse_hexdigest(value)
|
63
|
+
return self.send("find_by_#{column_name}", uuid.raw)
|
64
|
+
rescue
|
65
|
+
return nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
if options[:autofill]
|
71
|
+
before_create do
|
72
|
+
value = self.send(column_name)
|
73
|
+
if value.blank?
|
74
|
+
uuid = UUIDTools::UUID.timestamp_create
|
75
|
+
self.send("#{column_name}=", uuid.raw)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
CREATE DATABASE attr_uuid_test DEFAULT CHARACTER SET utf8;
|
@@ -0,0 +1,291 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_record"
|
3
|
+
require "attr_uuid"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
config_json = File.open(File.join("spec", "config", "database.json")).read
|
7
|
+
config = JSON.parse(config_json, :symbolize_keys => true)
|
8
|
+
ActiveRecord::Base.establish_connection(config)
|
9
|
+
|
10
|
+
class Model1
|
11
|
+
include AttrUuid
|
12
|
+
attr_uuid :uuid
|
13
|
+
attr_accessor :uuid
|
14
|
+
def initialize(uuid)
|
15
|
+
@uuid = UUIDTools::UUID.parse(uuid).raw
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Model2
|
20
|
+
include AttrUuid
|
21
|
+
attr_uuid 1
|
22
|
+
end
|
23
|
+
|
24
|
+
class Model3
|
25
|
+
include AttrUuid
|
26
|
+
attr_uuid :uuid, :column_name => 1
|
27
|
+
end
|
28
|
+
|
29
|
+
describe AttrUuid do
|
30
|
+
context "when PORO" do
|
31
|
+
subject(:model) { Model1.new("faea220a-e94e-442c-9ca0-5b39753e3549") }
|
32
|
+
|
33
|
+
describe ".attr_uuid" do
|
34
|
+
context "when argument is String" do
|
35
|
+
it { is_expected.to respond_to :formatted_uuid }
|
36
|
+
it { is_expected.to respond_to :formatted_uuid= }
|
37
|
+
it { is_expected.to respond_to :hex_uuid }
|
38
|
+
it { is_expected.to respond_to :hex_uuid= }
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when argument is neither String nor Symbol" do
|
42
|
+
subject(:model) { Model2.new }
|
43
|
+
it { is_expected.not_to respond_to :formatted_1 }
|
44
|
+
it { is_expected.not_to respond_to :formatted_1= }
|
45
|
+
it { is_expected.not_to respond_to :hex_1 }
|
46
|
+
it { is_expected.not_to respond_to :hex_1= }
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when column alias name is neither String nor Symbol" do
|
50
|
+
subject(:model) { Model3.new }
|
51
|
+
it { is_expected.not_to respond_to :formatted_1 }
|
52
|
+
it { is_expected.not_to respond_to :formatted_1= }
|
53
|
+
it { is_expected.not_to respond_to :hex_1 }
|
54
|
+
it { is_expected.not_to respond_to :hex_1= }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#formatted_xxx" do
|
59
|
+
it "returns formatted attribute value" do
|
60
|
+
expect(model.formatted_uuid).to eq "faea220a-e94e-442c-9ca0-5b39753e3549"
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when uuid is nil" do
|
64
|
+
before { model.uuid = nil }
|
65
|
+
it { expect(model.formatted_uuid).to be_nil }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#formatted_xxx=" do
|
70
|
+
it "updates original attribute" do
|
71
|
+
uuid = UUIDTools::UUID.parse("d8354fff-f782-4b86-b4a7-7db46a5426d7")
|
72
|
+
model.formatted_uuid = uuid.to_s
|
73
|
+
expect(model.uuid).to eq uuid.raw
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#hex_xxx" do
|
78
|
+
it "returns hex digested attribute value" do
|
79
|
+
expect(model.hex_uuid).to eq "faea220ae94e442c9ca05b39753e3549"
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when uuid is nil" do
|
83
|
+
before { model.uuid = nil }
|
84
|
+
it { expect(model.hex_uuid).to be_nil }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#hex_xxx=" do
|
89
|
+
it "updates original attribute" do
|
90
|
+
uuid = UUIDTools::UUID.parse("d8354fff-f782-4b86-b4a7-7db46a5426d7")
|
91
|
+
model.hex_uuid = uuid.hexdigest
|
92
|
+
expect(model.uuid).to eq uuid.raw
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "when ActiveRecord" do
|
98
|
+
context "when disable autofill" do
|
99
|
+
with_model :dummy do
|
100
|
+
table do |t|
|
101
|
+
t.binary :uuid
|
102
|
+
end
|
103
|
+
|
104
|
+
model do
|
105
|
+
include AttrUuid
|
106
|
+
attr_uuid :uuid
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "when save without uuid" do
|
111
|
+
subject(:model) { Dummy.create! }
|
112
|
+
it { expect(model.uuid).to be_nil }
|
113
|
+
end
|
114
|
+
|
115
|
+
subject(:model) do
|
116
|
+
uuid = UUIDTools::UUID.parse("faea220a-e94e-442c-9ca0-5b39753e3549")
|
117
|
+
Dummy.new {|o| o.uuid = uuid.raw }
|
118
|
+
end
|
119
|
+
|
120
|
+
describe ".attr_uuid" do
|
121
|
+
it { is_expected.to respond_to :formatted_uuid }
|
122
|
+
it { is_expected.to respond_to :formatted_uuid= }
|
123
|
+
it { is_expected.to respond_to :hex_uuid }
|
124
|
+
it { is_expected.to respond_to :hex_uuid= }
|
125
|
+
it { expect(Dummy).to respond_to :find_by_formatted_uuid }
|
126
|
+
it { expect(Dummy).to respond_to :find_by_hex_uuid }
|
127
|
+
end
|
128
|
+
|
129
|
+
describe ".find_by_formatted_xxx" do
|
130
|
+
before { model.save! }
|
131
|
+
subject(:result) { Dummy.find_by_formatted_uuid(uuid) }
|
132
|
+
|
133
|
+
context "when uuid matched" do
|
134
|
+
let(:uuid) { "faea220a-e94e-442c-9ca0-5b39753e3549" }
|
135
|
+
it { expect(result).to eq model }
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when no uuid matched" do
|
139
|
+
let(:uuid) { "00000000-e94e-442c-9ca0-5b39753e3549" }
|
140
|
+
it { expect(result).to be_nil }
|
141
|
+
end
|
142
|
+
|
143
|
+
context "when uuid is nil" do
|
144
|
+
let(:uuid) { nil }
|
145
|
+
it { expect(result).to be_nil }
|
146
|
+
end
|
147
|
+
|
148
|
+
context "when uuid isn't String" do
|
149
|
+
let(:uuid) { 1 }
|
150
|
+
it { expect(result).to be_nil }
|
151
|
+
end
|
152
|
+
|
153
|
+
context "when uuid format is invalid" do
|
154
|
+
let(:uuid) { "invalid" }
|
155
|
+
it { expect(result).to be_nil }
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe ".find_by_hex_xxx" do
|
160
|
+
before { model.save! }
|
161
|
+
subject(:result) { Dummy.find_by_hex_uuid(uuid) }
|
162
|
+
|
163
|
+
context "when uuid matched" do
|
164
|
+
let(:uuid) { "faea220ae94e442c9ca05b39753e3549" }
|
165
|
+
it { expect(result).to eq model }
|
166
|
+
end
|
167
|
+
|
168
|
+
context "when no uuid matched" do
|
169
|
+
let(:uuid) { "00000000e94e442c9ca05b39753e3549" }
|
170
|
+
it { expect(result).to be_nil }
|
171
|
+
end
|
172
|
+
|
173
|
+
context "when uuid is nil" do
|
174
|
+
let(:uuid) { nil }
|
175
|
+
it { expect(result).to be_nil }
|
176
|
+
end
|
177
|
+
|
178
|
+
context "when uuid isn't String" do
|
179
|
+
let(:uuid) { 1 }
|
180
|
+
it { expect(result).to be_nil }
|
181
|
+
end
|
182
|
+
|
183
|
+
context "when uuid format is invalid" do
|
184
|
+
let(:uuid) { "invalid" }
|
185
|
+
it { expect(result).to be_nil }
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context "when column name alias enabled" do
|
191
|
+
with_model :dummy do
|
192
|
+
table do |t|
|
193
|
+
t.binary :x_uuid
|
194
|
+
end
|
195
|
+
|
196
|
+
model do
|
197
|
+
include AttrUuid
|
198
|
+
attr_uuid :uuid, :column_name => "x_uuid"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context "when save without uuid" do
|
203
|
+
subject(:model) { Dummy.create! }
|
204
|
+
it { expect(model.x_uuid).to be_nil }
|
205
|
+
end
|
206
|
+
|
207
|
+
subject(:model) do
|
208
|
+
uuid = UUIDTools::UUID.parse("faea220a-e94e-442c-9ca0-5b39753e3549")
|
209
|
+
Dummy.new {|o| o.x_uuid = uuid.raw }
|
210
|
+
end
|
211
|
+
|
212
|
+
describe ".attr_uuid" do
|
213
|
+
it { is_expected.to respond_to :formatted_uuid }
|
214
|
+
it { is_expected.to respond_to :formatted_uuid= }
|
215
|
+
it { is_expected.to respond_to :hex_uuid }
|
216
|
+
it { is_expected.to respond_to :hex_uuid= }
|
217
|
+
it { expect(Dummy).to respond_to :find_by_formatted_uuid }
|
218
|
+
it { expect(Dummy).to respond_to :find_by_hex_uuid }
|
219
|
+
end
|
220
|
+
|
221
|
+
describe ".find_by_formatted_xxx" do
|
222
|
+
before { model.save! }
|
223
|
+
subject(:result) { Dummy.find_by_formatted_uuid(uuid) }
|
224
|
+
|
225
|
+
context "when uuid matched" do
|
226
|
+
let(:uuid) { "faea220a-e94e-442c-9ca0-5b39753e3549" }
|
227
|
+
it { expect(result).to eq model }
|
228
|
+
end
|
229
|
+
|
230
|
+
context "when no uuid matched" do
|
231
|
+
let(:uuid) { "00000000-e94e-442c-9ca0-5b39753e3549" }
|
232
|
+
it { expect(result).to be_nil }
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe ".find_by_hex_xxx" do
|
237
|
+
before { model.save! }
|
238
|
+
subject(:result) { Dummy.find_by_hex_uuid(uuid) }
|
239
|
+
|
240
|
+
context "when uuid matched" do
|
241
|
+
let(:uuid) { "faea220ae94e442c9ca05b39753e3549" }
|
242
|
+
it { expect(result).to eq model }
|
243
|
+
end
|
244
|
+
|
245
|
+
context "when no uuid matched" do
|
246
|
+
let(:uuid) { "00000000e94e442c9ca05b39753e3549" }
|
247
|
+
it { expect(result).to be_nil }
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
context "when enable autofill" do
|
253
|
+
with_model :dummy do
|
254
|
+
table do |t|
|
255
|
+
t.binary :uuid
|
256
|
+
end
|
257
|
+
|
258
|
+
model do
|
259
|
+
include AttrUuid
|
260
|
+
attr_uuid :uuid, :autofill => true
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context "when uuid is nil" do
|
265
|
+
before do
|
266
|
+
@uuid = UUIDTools::UUID.parse("080cd5cb-9556-4c07-9af3-a4559cf52627")
|
267
|
+
UUIDTools::UUID.stub(:timestamp_create).and_return(@uuid)
|
268
|
+
end
|
269
|
+
subject(:model) { Dummy.create! }
|
270
|
+
it { expect(model.uuid).to eq @uuid.raw }
|
271
|
+
end
|
272
|
+
|
273
|
+
context "when uuid is empty" do
|
274
|
+
before do
|
275
|
+
@uuid = UUIDTools::UUID.parse("40d5fafe-ff68-4606-9de7-554eae0d77a3")
|
276
|
+
UUIDTools::UUID.stub(:timestamp_create).and_return(@uuid)
|
277
|
+
end
|
278
|
+
subject(:model) { Dummy.create! {|o| o.uuid = ""} }
|
279
|
+
it { expect(model.uuid).to eq @uuid.raw }
|
280
|
+
end
|
281
|
+
|
282
|
+
context "when uuid is set" do
|
283
|
+
subject(:model) do
|
284
|
+
@uuid = UUIDTools::UUID.parse("3e1fe985-2fbf-44ce-a5fb-d1b3db49260d")
|
285
|
+
Dummy.create! {|o| o.uuid = @uuid.raw }
|
286
|
+
end
|
287
|
+
it { expect(model.uuid).to eq @uuid.raw }
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "simplecov"
|
4
|
+
require "with_model"
|
5
|
+
|
6
|
+
SimpleCov.start do
|
7
|
+
add_filter "spec"
|
8
|
+
add_filter "vendor"
|
9
|
+
end
|
10
|
+
|
11
|
+
$: << File.join(File.dirname(__FILE__), "..", "lib")
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.run_all_when_everything_filtered = true
|
15
|
+
config.filter_run :focus
|
16
|
+
config.order = 'random'
|
17
|
+
config.extend WithModel
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shou Takenaka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: uuidtools
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mysql2
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: with_model
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: This gem makes binary uuid attribute easy to use
|
98
|
+
email:
|
99
|
+
- sasaminn@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- attr_uuid.gemspec
|
110
|
+
- lib/attr_uuid.rb
|
111
|
+
- lib/attr_uuid/railtie.rb
|
112
|
+
- lib/attr_uuid/version.rb
|
113
|
+
- spec/config/database.json.sample
|
114
|
+
- spec/db/create_database.sql
|
115
|
+
- spec/lib/attr_uuid_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
homepage: ''
|
118
|
+
licenses: []
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.2.2
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Easy to use binay uuid attribute
|
140
|
+
test_files:
|
141
|
+
- spec/config/database.json.sample
|
142
|
+
- spec/db/create_database.sql
|
143
|
+
- spec/lib/attr_uuid_spec.rb
|
144
|
+
- spec/spec_helper.rb
|