bson-objectid-to-uuid 0.1.2 → 0.1.3

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: ab2cf162fc6fa17067092ddd2ad556868e586cad7be87fb22b776dc560385881
4
- data.tar.gz: 14a230ee7618ae6475959b54680ceb25dfe614adf1f4c87e42e2da3ab1403b90
3
+ metadata.gz: a00384765419e7adec1de6abb69b1e6e77fccce78dd26d0fd8dbaf1876e4662f
4
+ data.tar.gz: 1bc64ad47b29e7765c017b4204ba4685752265d87793141a06a26002b8fd2383
5
5
  SHA512:
6
- metadata.gz: b1cd0ad307ef28f97905d61e2e9e3abee2e8fa53b07eaefb07240dfd430a7d1e90071e27bb8f3f5bb42c4a669aded816a29b239ec6eac4914a777151fde8c7e1
7
- data.tar.gz: 2535e710f6a8e4af9155cc7f1a6fb78a13bfd9283012996f1a7bc9deee3070ccc0cb9ff5b07b78dc337b90ee8d7e358d6a916092547d0fbac23936aa47a18193
6
+ metadata.gz: 0e2e305bd9926d005e2851d5f18f7703065f9b8dce30c49475aad627202d14320cb5ff3b5ffa5c9aa56ca760c57d0012e06fd2f65b2fc81ba31ca9ea55f441e5
7
+ data.tar.gz: 49fced073c445c32a8356d26942fdc24a6bc7c34e50d990b5a94e4ead9a6ab1b0972599b4734f3cb62c8ba0ee9b21f1b078f8fd10cae18bf897a3bc3a2cc62bc
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bson-objectid-to-uuid (0.1.2)
4
+ bson-objectid-to-uuid (0.1.3)
5
5
  bson (~> 4.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # BSON::ObjectID::UUIDConvertable
2
2
 
3
- This gem allows for you to use `BSON::ObjectId`'s and `UUIDv1`'s interchangeably with the mongo-ruby driver and/or mongoid.
3
+ This gem allows for you to use `BSON::ObjectId`'s and `UUIDv4`'s interchangeably with the mongo-ruby driver and/or mongoid.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,17 +20,17 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Convert a `BSON::ObjectId` to `UUIDv1`:
23
+ Convert a `BSON::ObjectId` to `UUIDv4`:
24
24
 
25
25
  ```ruby
26
- oid = BSON::ObjectId.new # => BSON::ObjectId('5a785ae00d6cab0695bb7694')
27
- oid.to_uuid # => "c9edf094-0a77-11e8-8076-bb0d6cab0695"
26
+ oid = BSON::ObjectId.new # => BSON::ObjectId('5a74c31b0d6cabe30aa88fad')
27
+ oid.to_uuid # => "0bdaea5a-74c3-41b0-ad6c-abe30aa88fad"
28
28
  ```
29
29
 
30
- Convert `UUIDv1` to `BSON::ObjectId`:
30
+ Convert `UUIDv4` to `BSON::ObjectId`:
31
31
 
32
32
  ```ruby
33
- BSON::ObjectId.from_uuid("c9edf094-0a77-11e8-8076-bb0d6cab0695") # => BSON::ObjectId('5a785ae00d6cab0695bb7694')
33
+ BSON::ObjectId.from_uuid("0bdaea5a-74c3-41b0-ad6c-abe30aa88fad") # => BSON::ObjectId('5a74c31b0d6cabe30aa88fad')
34
34
  ```
35
35
 
36
36
  ## Usage with Mongoid
@@ -39,13 +39,13 @@ Getting the `UUIDv1` for a `Mongoid::Document`:
39
39
 
40
40
  ```ruby
41
41
  user = User.first # => #<User ... >
42
- user._id.to_uuid # => "c9edf094-0a77-11e8-8076-bb0d6cab0695"
42
+ user._id.to_uuid # => "0bdaea5a-74c3-41b0-ad6c-abe30aa88fad"
43
43
  ```
44
44
 
45
45
  Finding a `Mongoid::Document` using `UUIDv1`:
46
46
 
47
47
  ```ruby
48
- user = User.find("c9edf094-0a77-11e8-8076-bb0d6cab0695") # => # => #<User ... >
48
+ user = User.find("0bdaea5a-74c3-41b0-ad6c-abe30aa88fad") # => # => #<User ... >
49
49
  ```
50
50
 
51
51
  ## Development
@@ -1,53 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BSON::ObjectId::UUIDConvertable
4
- GREGORIAN_EPOCH_OFFSET = 0x01B2_1DD2_1381_4000 # Oct 15, 1582
5
-
6
- UUID_REGEX = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i
7
- UUID_VERSION = 0x1000
8
- UUID_VARIANT = 0b1000_0000_0000_0000
9
-
10
- def to_uuid # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
11
- timestamp = to_s[0..7].to_i(16) * 10_000_000 + GREGORIAN_EPOCH_OFFSET
12
- machine_pid = to_s[8..17].to_i(16)
13
-
14
- high_counter = to_s[18..19].to_i(16)
15
- mid_counter = to_s[20..21].to_i(16)
16
- low_counter = to_s[22..23].to_i(16)
17
-
18
- bytes = [
19
- (timestamp + low_counter) & 0xFFFF_FFFF,
20
- (timestamp >> 32) & 0xFFFF,
21
- ((timestamp >> 48) & 0x0FFF) | UUID_VERSION,
22
- mid_counter | UUID_VARIANT,
23
- (high_counter << 40) + machine_pid
24
- ]
25
-
26
- format('%08x-%04x-%04x-%04x-%012x', *bytes)
4
+ UUID_PREFIX = '0bdaea'
5
+ UUID_REGEX = /\A0bdaea[0-9a-f]{2}-[0-9a-f]{4}-4[0-9a-f]{3}-[ab][0-9a-f]{3}-[0-9a-f]{12}\z/i
6
+ UUID_VERSION = '4'
7
+ UUID_VARIANT = 'a'
8
+
9
+ def to_uuid
10
+ [
11
+ UUID_PREFIX + to_s[0..1], # 6 digit prefix + first 2 oid digits
12
+ to_s[2..5], # next 4 oid digits
13
+ UUID_VERSION + to_s[6..8], # 1 digit version(0x4) + next 3 oid digits
14
+ UUID_VARIANT + to_s[9..11], # 1 digit variant(0b101) + 1 zero bit + next 3 oid digits
15
+ to_s[12..23] # last 12 oid digits
16
+ ].join('-')
27
17
  end
28
18
 
29
19
  module ClassMethods
30
20
  def legal?(string)
31
- UUID_REGEX.match(string.to_s) ? true : super
21
+ UUID_REGEX.match?(string.to_s) ? true : super
32
22
  end
33
23
 
34
24
  def from_string(string)
35
- UUID_REGEX.match(string.to_s) ? from_uuid(string) : super
25
+ UUID_REGEX.match?(string.to_s) ? from_uuid(string) : super
36
26
  end
37
27
 
38
- def from_uuid(string) # rubocop:disable Metrics/AbcSize
39
- parts = string.split('-')
40
-
41
- timestamp = parts[0].to_i(16) + (parts[1].to_i(16) << 32) + ((parts[2].to_i(16) & 0x0FFF) << 48)
42
- machine_pid = parts[4][2..-1]
43
-
44
- high_counter = parts[4][0..1]
45
- mid_counter = parts[3][2..3]
46
- low_counter = (timestamp % 10_000_000) & 0xFF
47
-
48
- seconds = (timestamp - GREGORIAN_EPOCH_OFFSET) / 10_000_000
49
-
50
- from_string(format('%06x', seconds) + machine_pid + high_counter + mid_counter + format('%02x', low_counter))
28
+ def from_uuid(string)
29
+ from_string(string[6..7] + string[9..12] + string[15..17] + string[20..22] + string[24..35])
51
30
  end
52
31
  end
53
32
 
@@ -3,7 +3,7 @@
3
3
  module BSON
4
4
  class ObjectId
5
5
  module UUIDConvertable
6
- VERSION = '0.1.2'
6
+ VERSION = '0.1.3'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson-objectid-to-uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Gaspar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-05 00:00:00.000000000 Z
11
+ date: 2018-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bson