typed_uuid 2.1 → 2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d0743664e3f9987059821b25c8c42f80b062bbeeb3ba563e795d4b9845920505
4
- data.tar.gz: ec150decbaced84ca8cbd26b06af761551969ee9cc74145291c552215ab2a94b
3
+ metadata.gz: f5bf0c87bdd7950cf4aa85e788d0e4d0ac49ac842947471b2788db6c5e7b347b
4
+ data.tar.gz: a396047b9153d11c56ba87412ac7dbd6a3e17e9ecf701eebf9fef8e4542e9971
5
5
  SHA512:
6
- metadata.gz: e3e66ca9962d22a9ccece4458708c132e776062dc5d10071b715816c4c5329a99c6bea6b172e0f33a55c274a761e0850c1cb4a957b31de2124f3942d60204aa3
7
- data.tar.gz: 502b8f036034be6a080aa0722b26918810589fc741666d2ab4f0c125d987895bc2ba19ab886b79ff13b97e38b50698808dbc28eb80d2b1d822c1dda055f89ad3
6
+ metadata.gz: 7e3fe8ade426da5a52beef42674a737ec22837b5c8d758155ee4f962ed81a054cf0a1e8d36c6122e2f4a6ea14db9c0bc88586351812eb8b859a4eeeb886af621
7
+ data.tar.gz: 94434e3ab2d1fbb58a41c8dceb4558cf8a554ce16efd3b94aa9a0d62bde91b267da06804a91079076b1a88407e5cec362250a92146c8ac148ffe56ac0f83ebd2
@@ -33,6 +33,10 @@ module TypedUUID::ActiveRecord
33
33
  end
34
34
  end
35
35
 
36
+ def typed_uuid
37
+ TypedUUID.uuid(uuid_type_from_class(self))
38
+ end
39
+
36
40
  def uuid_type_from_table_name(table)
37
41
  type = defined_uuid_types.key(table.to_s)
38
42
  if type.nil?
@@ -41,12 +45,21 @@ module TypedUUID::ActiveRecord
41
45
 
42
46
  type
43
47
  end
48
+
49
+ def uuid_type_from_class(klass)
50
+ type = defined_uuid_types.key(klass.table_name)
51
+ if type.nil?
52
+ raise ArgumentError, "UUID Type for \"#{table}\" not defined"
53
+ end
54
+
55
+ type
56
+ end
44
57
 
45
58
  def class_from_uuid_type(type)
46
59
  if klass = uuid_type_cache[type]
47
60
  return klass
48
61
  else
49
- # Rails.application.eager_load! if !Rails.application.config.eager_load
62
+ Rails.application.eager_load! if !Rails.application.config.eager_load
50
63
 
51
64
  ::ActiveRecord::Base.descendants.select do |klass|
52
65
  next unless ( klass.superclass == ::ActiveRecord::Base || klass.superclass.abstract_class? )
@@ -60,8 +73,7 @@ module TypedUUID::ActiveRecord
60
73
  end
61
74
 
62
75
  def class_from_uuid(uuid)
63
- uuid = uuid.gsub('-', '')
64
- class_from_uuid_type((uuid[8..11].to_i(16) ^ uuid[16..19].to_i(16)) ^ uuid[12..15].to_i(16))
76
+ class_from_uuid_type(TypedUUID.enum(uuid))
65
77
  end
66
78
 
67
79
  end
@@ -7,9 +7,10 @@ class TypedUUID::Railtie < Rails::Railtie
7
7
  ActiveRecord::Base.extend TypedUUID::ActiveRecord
8
8
  end
9
9
 
10
- require 'active_record/connection_adapters/postgresql/schema_definitions'
10
+ require 'active_record/connection_adapters/postgresql/schema_definitions'
11
11
  ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include(TypedUUID::PsqlColumnMethods)
12
12
 
13
+ require 'active_record/connection_adapters/postgresql/schema_dumper'
13
14
  ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(TypedUUID::PsqlSchemaDumper)
14
15
  end
15
16
 
@@ -1,3 +1,3 @@
1
1
  module TypedUUID
2
- VERSION = '2.1'
2
+ VERSION = '2.2'
3
3
  end
data/lib/typed_uuid.rb CHANGED
@@ -2,6 +2,17 @@ module TypedUUID
2
2
  autoload :ActiveRecord, 'typed_uuid/active_record'
3
3
  autoload :PsqlColumnMethods, 'typed_uuid/psql_column_methods'
4
4
  autoload :PsqlSchemaDumper, 'typed_uuid/psql_schema_dumper'
5
+
6
+ def self.uuid(enum)
7
+ uuid = SecureRandom.random_bytes(16).unpack("NnnnnN")
8
+ uuid[2] = (uuid[1] ^ uuid[3]) ^ enum
9
+ "%08x-%04x-%04x-%04x-%04x%08x" % uuid
10
+ end
11
+
12
+ def self.enum(uuid)
13
+ uuid = uuid.gsub('-', '')
14
+ (uuid[8..11].to_i(16) ^ uuid[16..19].to_i(16)) ^ uuid[12..15].to_i(16)
15
+ end
5
16
  end
6
17
 
7
18
  require 'typed_uuid/railtie' if defined? Rails
data/test/test_helper.rb CHANGED
@@ -50,6 +50,7 @@ class ActiveSupport::TestCase
50
50
  end
51
51
 
52
52
  set_callback(:setup, :before) do
53
+ Rails.stubs(:application).returns(stub(config: stub(eager_load: true)))
53
54
  if !self.class.class_variable_defined?(:@@suite_setup_run)
54
55
  configuration = {
55
56
  adapter: "postgresql",
@@ -41,6 +41,12 @@ class FilterTest < ActiveSupport::TestCase
41
41
  end
42
42
  end
43
43
 
44
+ test 'typed_uuid' do
45
+ assert_equal 512, TypedUUID.enum(TypedUUID.uuid(512))
46
+ assert_equal FilterTest::Listing, ::ActiveRecord::Base.class_from_uuid(Listing.typed_uuid)
47
+ assert_equal FilterTest::Building, ::ActiveRecord::Base.class_from_uuid(Building.typed_uuid)
48
+ end
49
+
44
50
  test 'class_from uuid' do
45
51
  listing = Listing.create
46
52
  building = Building.create
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.1'
4
+ version: '2.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-24 00:00:00.000000000 Z
11
+ date: 2019-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake