uuid_attribute 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/uuid_attribute/railtie.rb +25 -9
- data/lib/uuid_attribute/utils.rb +22 -19
- data/lib/uuid_attribute/uuid.rb +1 -3
- data/lib/uuid_attribute/version.rb +1 -1
- data/test/test_helper.rb +7 -1
- data/test/uuid/test_configuration.rb +35 -0
- data/test/uuid/test_type.rb +40 -0
- data/test/uuid/test_utilities.rb +58 -0
- metadata +22 -4
- data/test/uuid/test_attribute.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3a31aeee55a99380975bf4cf4fbcfe7c506618bace27420afcb31d31ae06ec2
|
4
|
+
data.tar.gz: 304c13913329ea077e252deb3fb3f3bd0c4001c335db877650ac2d467b696e77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb6fd591641a8adef2c268d319bbd678c6bc930258ed12ec751d3d3be5101b4cf35baf63801ddfe2d39a532dd2db8582d8f10b664dc982bcfe24519893985f99
|
7
|
+
data.tar.gz: 92036ff31a27d90e635bc6d3fae13a153145531fefe391f43cb0e6e4eab19d54990cc69fb6b1d133017aa3a7e1050edc26103e3c4c7a0c8635297019c3eeb9a7
|
data/CHANGELOG.md
CHANGED
@@ -13,9 +13,9 @@ module UuidAttribute
|
|
13
13
|
field_info.type == :binary && field_info.limit == 16
|
14
14
|
end
|
15
15
|
|
16
|
-
def binary16?(field)
|
17
|
-
field_info =
|
18
|
-
binary16_structure?(field_info) &&
|
16
|
+
def binary16?(klass, field)
|
17
|
+
field_info = klass.attribute_types[field]
|
18
|
+
binary16_structure?(field_info) && klass.method_defined?("#{field}?")
|
19
19
|
end
|
20
20
|
|
21
21
|
def valid_default_rails_ids?(att)
|
@@ -38,14 +38,30 @@ end
|
|
38
38
|
=end
|
39
39
|
|
40
40
|
config.after_initialize do
|
41
|
-
# unless SimpleForm.configured?
|
42
|
-
# warn '[Simple Form] Simple Form is not configured in the application and will use the default values.' +
|
43
|
-
# ' Use `rails generate simple_form:install` to generate the Simple Form configuration.'
|
44
|
-
# end
|
45
|
-
|
46
41
|
ActiveRecord::Type.register(:uuid, ::UuidAttribute::UUID)
|
47
42
|
|
48
|
-
|
43
|
+
if UuidAttribute.auto_detect_binary_ids
|
44
|
+
models = []
|
45
|
+
Dir["#{Rails.root}/app/models/*"].each do |file|
|
46
|
+
model = File.basename(file, ".*").classify
|
47
|
+
models << model unless models.include?(model)
|
48
|
+
end
|
49
|
+
|
50
|
+
models -= %w[ActiveRecord Concern]
|
51
|
+
|
52
|
+
models.each do |model|
|
53
|
+
model = model.constantize
|
54
|
+
model.attribute_names.each do |att|
|
55
|
+
next unless valid_default_rails_ids?(att) && binary16?(model, att)
|
56
|
+
|
57
|
+
default = nil
|
58
|
+
default = -> { SecureRandom.uuid } if att.eql? "id"
|
59
|
+
model.define_attribute att, ::UuidAttribute::UUID.new, default: default
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# ActiveRecord::Base.include ::UuidAttribute::ActiveModel if defined? ActiveRecord::Base
|
49
65
|
|
50
66
|
if UuidAttribute.default_primary_id
|
51
67
|
# Configure UUID as Default Primary Key
|
data/lib/uuid_attribute/utils.rb
CHANGED
@@ -9,11 +9,7 @@ module UuidAttribute
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def hex_from_binary(bytes)
|
12
|
-
bytes.unpack1("H*")
|
13
|
-
end
|
14
|
-
|
15
|
-
def mysql_value(value)
|
16
|
-
"x'#{value}'"
|
12
|
+
normalize(bytes.unpack1("H*"))
|
17
13
|
end
|
18
14
|
|
19
15
|
def raw_bytes(uuid_string)
|
@@ -22,13 +18,14 @@ module UuidAttribute
|
|
22
18
|
|
23
19
|
def parse(str)
|
24
20
|
return nil if str.length.zero?
|
21
|
+
return str if str.length == 36
|
25
22
|
|
26
23
|
case str.length
|
27
|
-
when
|
28
|
-
|
24
|
+
when 32
|
25
|
+
format_uuid(str)
|
29
26
|
when 16
|
30
|
-
|
31
|
-
when
|
27
|
+
format_uuid(hex_from_binary(str))
|
28
|
+
when 22
|
32
29
|
unshort(str)
|
33
30
|
end
|
34
31
|
end
|
@@ -39,11 +36,23 @@ module UuidAttribute
|
|
39
36
|
a b c d e f g h i j k l m n o p q r s t u v w x y z
|
40
37
|
].freeze
|
41
38
|
|
39
|
+
def format_uuid(uuid)
|
40
|
+
# TODO: Maybe raise a exception?
|
41
|
+
return uuid if uuid.length != 32
|
42
|
+
|
43
|
+
uuid = uuid.downcase
|
44
|
+
[
|
45
|
+
uuid[0..7],
|
46
|
+
uuid[8..11],
|
47
|
+
uuid[12..15],
|
48
|
+
uuid[16..19],
|
49
|
+
uuid[20..31]
|
50
|
+
].join("-")
|
51
|
+
end
|
52
|
+
|
42
53
|
def shorten(decimal, alphabet = DEFAULT_BASE62)
|
43
|
-
alphabet = alphabet.to_a
|
44
54
|
radix = alphabet.length
|
45
|
-
|
46
|
-
i = normalized_decimal.to_i
|
55
|
+
i = normalize(decimal).to_i(16).to_i
|
47
56
|
out = []
|
48
57
|
return alphabet[0] if i.zero?
|
49
58
|
|
@@ -64,13 +73,7 @@ module UuidAttribute
|
|
64
73
|
end
|
65
74
|
|
66
75
|
uuid = num.to_s(16).rjust(32, "0")
|
67
|
-
|
68
|
-
uuid[0..7],
|
69
|
-
uuid[8..11],
|
70
|
-
uuid[12..15],
|
71
|
-
uuid[16..19],
|
72
|
-
uuid[20..31]
|
73
|
-
].join("-")
|
76
|
+
format_uuid(uuid)
|
74
77
|
end
|
75
78
|
end
|
76
79
|
end
|
data/lib/uuid_attribute/uuid.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "simplecov"
|
3
4
|
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
-
|
5
|
+
|
6
|
+
SimpleCov.start "rails" do
|
7
|
+
add_filter %r{^/test/}
|
8
|
+
add_filter "lib/uuid_attribute/version"
|
9
|
+
end
|
5
10
|
|
6
11
|
require "minitest/autorun"
|
12
|
+
require "uuid_attribute"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
module UUID
|
6
|
+
class TestConfiguration < Minitest::Test
|
7
|
+
def test_that_it_has_a_version_number
|
8
|
+
refute_nil ::UuidAttribute::VERSION
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_auto_detect_binary_ids
|
12
|
+
::UuidAttribute.setup do |config|
|
13
|
+
config.auto_detect_binary_ids = false
|
14
|
+
end
|
15
|
+
refute ::UuidAttribute.auto_detect_binary_ids
|
16
|
+
|
17
|
+
::UuidAttribute.setup do |config|
|
18
|
+
config.auto_detect_binary_ids = true
|
19
|
+
end
|
20
|
+
assert ::UuidAttribute.auto_detect_binary_ids
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_default_primary_id
|
24
|
+
::UuidAttribute.setup do |config|
|
25
|
+
config.default_primary_id = false
|
26
|
+
end
|
27
|
+
refute ::UuidAttribute.default_primary_id
|
28
|
+
|
29
|
+
::UuidAttribute.setup do |config|
|
30
|
+
config.default_primary_id = true
|
31
|
+
end
|
32
|
+
assert ::UuidAttribute.default_primary_id
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require "test_helper"
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
module UUID
|
6
|
+
class TestType < Minitest::Test
|
7
|
+
UNORMALIZED_UUID = "a0b1c2d3-e4f5-a6b7-c8d9-e0f1a2b3c4d5".freeze
|
8
|
+
NORMALIZED_UUID = "A0B1C2D3E4F5A6B7C8D9E0F1A2B3C4D5".freeze
|
9
|
+
BINARY_UUID = "\xA0\xB1\xC2\xD3\xE4\xF5\xA6\xB7\xC8\xD9\xE0\xF1\xA2\xB3\xC4\xD5".freeze
|
10
|
+
SHORTEN_UUID = "4tE0ZuelqYsF4p2FEnW2qb".freeze
|
11
|
+
|
12
|
+
def test_type
|
13
|
+
assert_equal(::UuidAttribute::UUID.new.type,:uuid)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_deserialize
|
17
|
+
assert_nil(::UuidAttribute::UUID.new.deserialize(nil))
|
18
|
+
assert_equal(
|
19
|
+
::UuidAttribute::UUID.new.deserialize(UNORMALIZED_UUID),
|
20
|
+
SHORTEN_UUID
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_serialize
|
25
|
+
assert_nil(::UuidAttribute::UUID.new.serialize(nil))
|
26
|
+
result = ::UuidAttribute::UUID.new.serialize(UNORMALIZED_UUID)
|
27
|
+
assert_equal("ActiveModel::Type::Binary::Data", result.class.name)
|
28
|
+
assert_equal(BINARY_UUID.bytes, result.to_s.bytes)
|
29
|
+
assert_equal(BINARY_UUID.bytes, [result.hex].pack("H*").bytes)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_cast
|
33
|
+
assert_nil(::UuidAttribute::UUID.new.cast(nil))
|
34
|
+
assert_equal(
|
35
|
+
::UuidAttribute::UUID.new.cast(UNORMALIZED_UUID),
|
36
|
+
SHORTEN_UUID
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
module UUID
|
6
|
+
class TestUtilities < Minitest::Test
|
7
|
+
UNORMALIZED_UUID = "a0b1c2d3-e4f5-a6b7-c8d9-e0f1a2b3c4d5"
|
8
|
+
NORMALIZED_UUID = "A0B1C2D3E4F5A6B7C8D9E0F1A2B3C4D5"
|
9
|
+
BINARY_UUID = "\xA0\xB1\xC2\xD3\xE4\xF5\xA6\xB7\xC8\xD9\xE0\xF1\xA2\xB3\xC4\xD5"
|
10
|
+
SHORTEN_UUID = "4tE0ZuelqYsF4p2FEnW2qb"
|
11
|
+
|
12
|
+
def test_normalization
|
13
|
+
result = ::UuidAttribute::Utils.normalize(UNORMALIZED_UUID)
|
14
|
+
assert_equal(NORMALIZED_UUID, result)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_hex_from_binary
|
18
|
+
result = ::UuidAttribute::Utils.hex_from_binary(BINARY_UUID)
|
19
|
+
assert_equal(NORMALIZED_UUID, result)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_raw_bytes
|
23
|
+
result = ::UuidAttribute::Utils.raw_bytes(NORMALIZED_UUID)
|
24
|
+
assert_equal(BINARY_UUID.bytes, result.bytes)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_shorten
|
28
|
+
result = ::UuidAttribute::Utils.shorten(UNORMALIZED_UUID)
|
29
|
+
assert_equal(SHORTEN_UUID, result)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_unshorten
|
33
|
+
result = ::UuidAttribute::Utils.unshort(SHORTEN_UUID)
|
34
|
+
assert_equal(UNORMALIZED_UUID, result)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_short_and_unshort
|
38
|
+
random_uuid = SecureRandom.uuid
|
39
|
+
shorted_uuid = ::UuidAttribute::Utils.shorten(random_uuid)
|
40
|
+
unshorted_uuid = ::UuidAttribute::Utils.unshort(shorted_uuid)
|
41
|
+
assert_equal(random_uuid, unshorted_uuid)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_parser
|
45
|
+
result = ::UuidAttribute::Utils.parse(NORMALIZED_UUID)
|
46
|
+
assert_equal(UNORMALIZED_UUID, result)
|
47
|
+
|
48
|
+
result = ::UuidAttribute::Utils.parse(BINARY_UUID)
|
49
|
+
assert_equal(UNORMALIZED_UUID, result)
|
50
|
+
|
51
|
+
result = ::UuidAttribute::Utils.parse(SHORTEN_UUID)
|
52
|
+
assert_equal(UNORMALIZED_UUID, result)
|
53
|
+
|
54
|
+
result = ::UuidAttribute::Utils.parse(UNORMALIZED_UUID)
|
55
|
+
assert_equal(UNORMALIZED_UUID, result)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uuid_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Negri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.2'
|
27
41
|
description: UUID attribute for ActiveRecord
|
28
42
|
email:
|
29
43
|
- patrick@iugu.com
|
@@ -41,7 +55,9 @@ files:
|
|
41
55
|
- lib/uuid_attribute/uuid.rb
|
42
56
|
- lib/uuid_attribute/version.rb
|
43
57
|
- test/test_helper.rb
|
44
|
-
- test/uuid/
|
58
|
+
- test/uuid/test_configuration.rb
|
59
|
+
- test/uuid/test_type.rb
|
60
|
+
- test/uuid/test_utilities.rb
|
45
61
|
homepage: https://github.com/iugu/activerecord-uuid-attribute
|
46
62
|
licenses:
|
47
63
|
- MIT
|
@@ -72,4 +88,6 @@ specification_version: 4
|
|
72
88
|
summary: UUID attribute for ActiveRecord
|
73
89
|
test_files:
|
74
90
|
- test/test_helper.rb
|
75
|
-
- test/uuid/
|
91
|
+
- test/uuid/test_configuration.rb
|
92
|
+
- test/uuid/test_type.rb
|
93
|
+
- test/uuid/test_utilities.rb
|
data/test/uuid/test_attribute.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test_helper"
|
4
|
-
|
5
|
-
module UUID
|
6
|
-
class TestAttribute < Minitest::Test
|
7
|
-
def test_that_it_has_a_version_number
|
8
|
-
refute_nil ::UUID::Attribute::VERSION
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_it_does_something_useful
|
12
|
-
assert false
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|