uuid_attribute 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b93f3aeb7c291e0f7769a8c22f30ffa03ae43af266b535004c6e2e65dff40981
4
- data.tar.gz: a301839295c42abd36b29e96b514c40c05ca4a8f39317035fedd4c6dab9285f2
3
+ metadata.gz: a3a31aeee55a99380975bf4cf4fbcfe7c506618bace27420afcb31d31ae06ec2
4
+ data.tar.gz: 304c13913329ea077e252deb3fb3f3bd0c4001c335db877650ac2d467b696e77
5
5
  SHA512:
6
- metadata.gz: 5f87cd2276f59c7ef15bfd0758b9870cf6ff73fbc8788191ff6afb953af49d673f3765dcfd52911d58535ff8b6b02ba90b69244ea33516f1c9a7db2008b03ad7
7
- data.tar.gz: 8f6129b56a17c07986a7ae0f49a5b9c8303f494fd8f3b82974fa40a77d0d90d05996fdf634cc1eee877891505282c3ba8149ce80774aa4e31c46e5af45800a76
6
+ metadata.gz: eb6fd591641a8adef2c268d319bbd678c6bc930258ed12ec751d3d3be5101b4cf35baf63801ddfe2d39a532dd2db8582d8f10b664dc982bcfe24519893985f99
7
+ data.tar.gz: 92036ff31a27d90e635bc6d3fae13a153145531fefe391f43cb0e6e4eab19d54990cc69fb6b1d133017aa3a7e1050edc26103e3c4c7a0c8635297019c3eeb9a7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.2.0 - 2022-08-20
4
+
5
+ * Added tests
6
+ * Fixed autodetecting of binary(16) as UUID
7
+
3
8
  ## 0.1.1 - 2022-08-19
4
9
 
5
10
  * First public release
@@ -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 = self.class.attribute_types[field]
18
- binary16_structure?(field_info) && respond_to?("#{field}?")
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
- ActiveRecord::Base.include ::UuidAttribute::ActiveModel if defined? ActiveRecord::Base
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
@@ -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 36
28
- normalize(str)
24
+ when 32
25
+ format_uuid(str)
29
26
  when 16
30
- normalize(hex_from_binary(str))
31
- when 20, 21, 22
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
- normalized_decimal = normalize(decimal).to_i(16)
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
@@ -22,9 +22,7 @@ module UuidAttribute
22
22
  end
23
23
 
24
24
  def cast(value)
25
- return if value.nil?
26
-
27
- Utils.shorten(Utils.parse(value.to_s))
25
+ deserialize(value)
28
26
  end
29
27
  end
30
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UuidAttribute
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
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
- require "uuid/attribute"
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.1.2
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-19 00:00:00.000000000 Z
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/test_attribute.rb
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/test_attribute.rb
91
+ - test/uuid/test_configuration.rb
92
+ - test/uuid/test_type.rb
93
+ - test/uuid/test_utilities.rb
@@ -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