bson-objectid-to-uuid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc4da50ca71a305a945837dc1ef9b0cc9a0ce1f7423cc230a666edbf60a221c9
4
+ data.tar.gz: 7541368e1751f76bd29467aac100fadf11a4c6f41f7391570ba90ea25a1c9f1c
5
+ SHA512:
6
+ metadata.gz: b69e2ff4ee1542c710f8dd199067b0c1a2319ae8ca24a6093174cc49af1a0873cc6199fc9cf8398464ac08202f7a64d32176243f30df1b43e1e9b603bd5352a1
7
+ data.tar.gz: '09aa3e38f6cfa169750a4e19264103ce650994d38c90b271b9a603c66945456bfbc99b014723a8bc0b5ecf22ba27bea1aac534f28d57d54a66dc5990ae0538a9'
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,39 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
5
+ # URISchemes: http, https
6
+ Metrics/LineLength:
7
+ Max: 120
8
+
9
+ Naming/FileName:
10
+ Exclude:
11
+ - 'lib/bson-objectid-to-uuid.rb'
12
+
13
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
14
+ # SupportedStyles: nested, compact
15
+ Style/ClassAndModuleChildren:
16
+ EnforcedStyle: compact
17
+ Exclude:
18
+ - 'lib/bson/object_id/uuid_convertable/version.rb'
19
+
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ # Cop supports --auto-correct.
24
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
25
+ # SupportedStyles: with_first_parameter, with_fixed_indentation
26
+ Layout/AlignParameters:
27
+ EnforcedStyle: with_fixed_indentation
28
+
29
+ # Cop supports --auto-correct.
30
+ # Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
31
+ # SupportedStyles: aligned, indented
32
+ Layout/MultilineMethodCallIndentation:
33
+ EnforcedStyle: indented
34
+
35
+ # Cop supports --auto-correct.
36
+ # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles.
37
+ # SupportedStyles: space, no_space
38
+ Layout/SpaceInsideHashLiteralBraces:
39
+ EnforcedStyle: no_space
@@ -0,0 +1 @@
1
+ bson-objectid-to-uuid
@@ -0,0 +1 @@
1
+ ruby-2.5
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rafael.gaspar@me.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in bson-objectid-to-uuid.gemspec
8
+ gemspec
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bson-objectid-to-uuid (0.1.0)
5
+ bson (~> 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ bson (4.3.0)
12
+ minitest (5.11.3)
13
+ parallel (1.12.1)
14
+ parser (2.4.0.2)
15
+ ast (~> 2.3)
16
+ powerpack (0.1.1)
17
+ rainbow (3.0.0)
18
+ rake (10.5.0)
19
+ rubocop (0.52.1)
20
+ parallel (~> 1.10)
21
+ parser (>= 2.4.0.2, < 3.0)
22
+ powerpack (~> 0.1)
23
+ rainbow (>= 2.2.2, < 4.0)
24
+ ruby-progressbar (~> 1.7)
25
+ unicode-display_width (~> 1.0, >= 1.0.1)
26
+ ruby-progressbar (1.9.0)
27
+ simple_uuid (0.4.0)
28
+ unicode-display_width (1.3.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bson-objectid-to-uuid!
35
+ bundler (~> 1.16)
36
+ minitest (~> 5.0)
37
+ rake (~> 10.0)
38
+ rubocop (~> 0.52)
39
+ simple_uuid (>= 0.4)
40
+
41
+ BUNDLED WITH
42
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Eadbox
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # BSON::ObjectID::UUIDConvertable
2
+
3
+ This gem allows for you to use `BSON::ObjectId`'s and `UUIDv1`'s interchangeably with the mongo-ruby driver and/or mongoid.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bson-objectid-to-uuid'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bson-objectid-to-uuid
20
+
21
+ ## Usage
22
+
23
+ Convert a `BSON::ObjectId` to `UUIDv1`:
24
+
25
+ ```ruby
26
+ oid = BSON::ObjectId.new # => BSON::ObjectId('5a785ae00d6cab0695bb7694')
27
+ oid.to_uuid # => "c9edf094-0a77-11e8-8076-bb0d6cab0695"
28
+ ```
29
+
30
+ Convert `UUIDv1` to `BSON::ObjectId`:
31
+
32
+ ```ruby
33
+ BSON::ObjectId.from_uuid("c9edf094-0a77-11e8-8076-bb0d6cab0695") # => BSON::ObjectId('5a785ae00d6cab0695bb7694')
34
+ ```
35
+
36
+ ## Usage with Mongoid
37
+
38
+ Getting the `UUIDv1` for a `Mongoid::Document`:
39
+
40
+ ```ruby
41
+ user = User.first # => #<User ... >
42
+ user._id.to_uuid # => "c9edf094-0a77-11e8-8076-bb0d6cab0695"
43
+ ```
44
+
45
+ Finding a `Mongoid::Document` using `UUIDv1`:
46
+
47
+ ```ruby
48
+ user = User.find("c9edf094-0a77-11e8-8076-bb0d6cab0695") # => # => #<User ... >
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eadbox/bson-objectid-to-uuid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
+
65
+ ## Code of Conduct
66
+
67
+ Everyone interacting in the BSON::ObjectID::UUIDConvertable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/eadbox/bson-objectid-to-uuid/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'bson-objectid-to-uuid'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/bson/object_id/uuid_convertable/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bson-objectid-to-uuid'
7
+ spec.version = BSON::ObjectId::UUIDConvertable::VERSION
8
+ spec.authors = ['Rafael Gaspar']
9
+ spec.email = ['rafael.gaspar@me.com']
10
+
11
+ spec.summary = "Converts BSON::ObjectId to and from UUIDv1's."
12
+ spec.description = "Converts BSON::ObjectId to and from UUIDv1's."
13
+ spec.homepage = 'https://github.com/eadbox/bson-objectid-to-uuid'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'bson', '~> 4.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.52'
29
+ spec.add_development_dependency 'simple_uuid', '>= 0.4'
30
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bson/object_id/uuid_convertable/version'
4
+
5
+ require 'bson'
6
+ require 'bson/object_id/uuid_convertable'
7
+
8
+ BSON::ObjectId.prepend(BSON::ObjectId::UUIDConvertable)
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
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('%06x-%04x-%04x-%04x-%012x', *bytes)
27
+ end
28
+
29
+ module ClassMethods
30
+ def legal?(string)
31
+ UUID_REGEX.match(string.to_s) ? true : super
32
+ end
33
+
34
+ def from_string(string)
35
+ UUID_REGEX.match(string.to_s) ? from_uuid(string) : super
36
+ end
37
+
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
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))
51
+ end
52
+ end
53
+
54
+ def self.prepended(base)
55
+ class << base
56
+ prepend ClassMethods
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BSON
4
+ class ObjectId
5
+ module UUIDConvertable
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bson-objectid-to-uuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafael Gaspar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bson
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.52'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.52'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simple_uuid
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0.4'
97
+ description: Converts BSON::ObjectId to and from UUIDv1's.
98
+ email:
99
+ - rafael.gaspar@me.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rubocop.yml"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - bson-objectid-to-uuid.gemspec
118
+ - lib/bson-objectid-to-uuid.rb
119
+ - lib/bson/object_id/uuid_convertable.rb
120
+ - lib/bson/object_id/uuid_convertable/version.rb
121
+ homepage: https://github.com/eadbox/bson-objectid-to-uuid
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.7.4
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Converts BSON::ObjectId to and from UUIDv1's.
145
+ test_files: []