mysql-binuuid-rails 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8236b24424eb481ae0a04a19a4fc8801868d05b
4
+ data.tar.gz: 56fe9da411b6fb733ce53fd70ef9b1ecfeca8853
5
+ SHA512:
6
+ metadata.gz: c96352beab858b8e93ee4c4a872402387781750312388d02b3644f80cc1a6fb12f461fbaf54c0d55582b54efba22bce677b40a6ffb16ba28ee3ea7a8d9bb2ffb
7
+ data.tar.gz: b7e9554728d7bd7302bcdf292cc11f7f344951d60cf32c4753e1e0d51a8c2558b4f66b1cbd19e23577293990580209023b2532494971ebc648e4701294cf2065
@@ -0,0 +1,4 @@
1
+ /.bundle
2
+ /.DS_Store
3
+ /tmp
4
+ /*.gem
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.7
5
+ - 2.3.4
6
+ - 2.4.1
7
+ env:
8
+ matrix:
9
+ - RAILS_VERSION=5.0.4
10
+ - RAILS_VERSION=5.1.0
11
+ - RAILS_VERSION=5.1.1
12
+ - RAILS_VERSION=5.1.2
13
+ before_install:
14
+ - gem update bundler
15
+ - bundle update rails
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in the gemspec
4
+ gemspec
@@ -0,0 +1,125 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mysql-binuuid-rails (0.1.0)
5
+ rails (>= 5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.1.2)
11
+ actionpack (= 5.1.2)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (~> 0.6.1)
14
+ actionmailer (5.1.2)
15
+ actionpack (= 5.1.2)
16
+ actionview (= 5.1.2)
17
+ activejob (= 5.1.2)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.1.2)
21
+ actionview (= 5.1.2)
22
+ activesupport (= 5.1.2)
23
+ rack (~> 2.0)
24
+ rack-test (~> 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.1.2)
28
+ activesupport (= 5.1.2)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.1.2)
34
+ activesupport (= 5.1.2)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.1.2)
37
+ activesupport (= 5.1.2)
38
+ activerecord (5.1.2)
39
+ activemodel (= 5.1.2)
40
+ activesupport (= 5.1.2)
41
+ arel (~> 8.0)
42
+ activesupport (5.1.2)
43
+ concurrent-ruby (~> 1.0, >= 1.0.2)
44
+ i18n (~> 0.7)
45
+ minitest (~> 5.1)
46
+ tzinfo (~> 1.1)
47
+ arel (8.0.0)
48
+ builder (3.2.3)
49
+ concurrent-ruby (1.0.5)
50
+ erubi (1.6.1)
51
+ globalid (0.4.0)
52
+ activesupport (>= 4.2.0)
53
+ i18n (0.8.6)
54
+ loofah (2.0.3)
55
+ nokogiri (>= 1.5.9)
56
+ mail (2.6.6)
57
+ mime-types (>= 1.16, < 4)
58
+ method_source (0.8.2)
59
+ mime-types (3.1)
60
+ mime-types-data (~> 3.2015)
61
+ mime-types-data (3.2016.0521)
62
+ mini_portile2 (2.2.0)
63
+ minitest (5.10.3)
64
+ minitest-hooks (1.4.0)
65
+ minitest-spec-context (0.0.3)
66
+ mysql2 (0.4.8)
67
+ nio4r (2.1.0)
68
+ nokogiri (1.8.0)
69
+ mini_portile2 (~> 2.2.0)
70
+ rack (2.0.3)
71
+ rack-test (0.6.3)
72
+ rack (>= 1.0)
73
+ rails (5.1.2)
74
+ actioncable (= 5.1.2)
75
+ actionmailer (= 5.1.2)
76
+ actionpack (= 5.1.2)
77
+ actionview (= 5.1.2)
78
+ activejob (= 5.1.2)
79
+ activemodel (= 5.1.2)
80
+ activerecord (= 5.1.2)
81
+ activesupport (= 5.1.2)
82
+ bundler (>= 1.3.0, < 2.0)
83
+ railties (= 5.1.2)
84
+ sprockets-rails (>= 2.0.0)
85
+ rails-dom-testing (2.0.3)
86
+ activesupport (>= 4.2.0)
87
+ nokogiri (>= 1.6)
88
+ rails-html-sanitizer (1.0.3)
89
+ loofah (~> 2.0)
90
+ railties (5.1.2)
91
+ actionpack (= 5.1.2)
92
+ activesupport (= 5.1.2)
93
+ method_source
94
+ rake (>= 0.8.7)
95
+ thor (>= 0.18.1, < 2.0)
96
+ rake (12.0.0)
97
+ sprockets (3.7.1)
98
+ concurrent-ruby (~> 1.0)
99
+ rack (> 1, < 3)
100
+ sprockets-rails (3.2.0)
101
+ actionpack (>= 4.0)
102
+ activesupport (>= 4.0)
103
+ sprockets (>= 3.0.0)
104
+ thor (0.19.4)
105
+ thread_safe (0.3.6)
106
+ tzinfo (1.2.3)
107
+ thread_safe (~> 0.1)
108
+ websocket-driver (0.6.5)
109
+ websocket-extensions (>= 0.1.0)
110
+ websocket-extensions (0.1.2)
111
+
112
+ PLATFORMS
113
+ ruby
114
+
115
+ DEPENDENCIES
116
+ bundler
117
+ minitest
118
+ minitest-hooks
119
+ minitest-spec-context
120
+ mysql-binuuid-rails!
121
+ mysql2
122
+ rake
123
+
124
+ BUNDLED WITH
125
+ 1.15.3
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Mark Oude Veldhuis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ [![Build Status](https://travis-ci.org/nedap/mysql-binuuid-rails.svg?branch=master)](https://travis-ci.org/nedap/mysql-binuuid-rails)
2
+ [![Code Climate](https://codeclimate.com/github/nedap/mysql-binuuid-rails/badges/gpa.svg)](https://codeclimate.com/github/nedap/mysql-binuuid-rails)
3
+
4
+ # mysql-binuuid-rails
5
+
6
+ `mysql-binuuid-rails` leverages the Attributes API of Rails 5 and lets you
7
+ define attributes of type UUID on your models. By doing so, you can store your
8
+ UUIDs as binary values in your database, and still be able to query using
9
+ the string representations since the database will take care of the
10
+ type conversion.
11
+
12
+ As the name suggests, it only supports MySQL. If you're on PostgreSQL, you
13
+ can use UUIDs the proper way already.
14
+
15
+ If you were to store a UUID of 32 characters (without the dashes) as text in
16
+ your database, it would cost you at least 32 bytes. And that only counts if
17
+ every character only requires 1 byte. But that completely depends on your
18
+ encoding. If every character requires 2 bytes, storing it would already cost
19
+ you 64 bytes. And that's a lot, if you think about the fact that a UUID is
20
+ only 128 bits.
21
+
22
+ Being 128 bits, a UUID fits precisely in a column of 16 bytes. Though it won't
23
+ be really readable it sure saves up a lot of space and it's only 4x bigger
24
+ than a 32-bit integer, or 2x bigger than a 64-bit integer.
25
+
26
+ Not to mention the space you'll be saving when you create an index on the
27
+ column holding your UUID.
28
+
29
+ # Installation
30
+ You know the drill, add this line to your gemfile:
31
+
32
+ ```
33
+ gem 'mysql-binuuid-rails'
34
+ ```
35
+
36
+ # Usage
37
+ Using binary columns for UUIDs is very easy. There's only two steps you need to
38
+ perform which are described here.
39
+
40
+ ## Adding the column to store your UUID
41
+ Suppose you have a model called `Book` to which you want to add a unique
42
+ identifier in the form of a UUID. First, make sure your database is able to
43
+ hold this attribute. So let's create a migration.
44
+
45
+ ```
46
+ $ rails g migration AddUuidColumnToBooks
47
+ ```
48
+
49
+ Open up the migration file and change it as you'd like:
50
+
51
+ ```ruby
52
+ class AddUuidColumnToBooks < ActiveRecord::Migration[5.1]
53
+ def change
54
+ # 'uuid' is the column name, and 'binary' is the column type. You have to
55
+ # specify it as a binary column yourself. And because we know that a UUID
56
+ # takes up 16 bytes, we set can specify its limit.
57
+ add_column :books, :uuid, :binary, limit: 16
58
+ end
59
+ end
60
+ ```
61
+
62
+ Perform the migration:
63
+
64
+ ```
65
+ rails db:migrate
66
+ ```
67
+
68
+ ## Tell your model how to handle the binary UUID column
69
+ All you have to do now, is specify in your `Book` model how Rails should handle
70
+ the `uuid` column. Open up `app/models/book.rb` and simply add the following
71
+ single line:
72
+
73
+ ```ruby
74
+ class Book < ApplicationRecord
75
+ attribute :uuid, MySQLBinUUID::Type.new
76
+ end
77
+ ```
78
+
79
+ # Contributing
80
+
81
+ To start coding on `mysql-binuuid-rails`, fork the project, clone it locally
82
+ and then run `bin/setup` to get up and running. If you want to fool around in
83
+ a console with the changes you made, run `bin/console`.
84
+
85
+ Bug reports and pull requests are welcome on GitHub at
86
+ https://github.com/nedap/mysql-binuuid-rails
87
+
88
+
89
+ # License
90
+
91
+ The gem is available as open source under the terms of the
92
+ [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/all"
5
+ require "mysql-binuuid-rails"
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,6 @@
1
+ require 'mysql-binuuid/type'
2
+
3
+ ActiveModel::Type.register(:uuid, MySQLBinUUID::Type)
4
+
5
+ module MySQLBinUUID
6
+ end
@@ -0,0 +1,77 @@
1
+ module MySQLBinUUID
2
+ class Type < ActiveModel::Type::Binary
3
+
4
+ def type
5
+ :uuid
6
+ end
7
+
8
+ # Invoked when a value that is returned from the database needs to be
9
+ # displayed into something readable again.
10
+ def cast(value)
11
+ if value.is_a?(MySQLBinUUID::Type::Data)
12
+ # It could be a Data object, in which case we should add dashes to the
13
+ # string value from there.
14
+ add_dashes(value.to_s)
15
+ elsif value.is_a?(String) && value.encoding == Encoding::ASCII_8BIT
16
+ # We cannot unpack something that looks like a UUID, with or without
17
+ # dashes. Not entirely sure why ActiveRecord does a weird combination of
18
+ # cast and serialize before anything needs to be saved..
19
+ undashed_uuid = value.unpack("H*")[0]
20
+ add_dashes(undashed_uuid.to_s)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ # Invoked when the provided value needs to be serialized before storing
27
+ # it to the database.
28
+ def serialize(value)
29
+ return if value.nil?
30
+ Data.new(strip_dashes(value))
31
+ end
32
+
33
+ # We're inheriting from the Binary type since ActiveRecord in that case
34
+ # will get the hex value. All we need to do to provide the hex value of the
35
+ # UUID, is to return the UUID without dashes. And because this inherits
36
+ # from Binary::Data, ActiveRecord will quote the hex value as required by
37
+ # the database to store it.
38
+ class Data < ActiveModel::Type::Binary::Data
39
+ def initialize(value)
40
+ @value = value
41
+ end
42
+
43
+ def hex
44
+ @value
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # A UUID consists of 5 groups of characters.
51
+ # 8 chars - 4 chars - 4 chars - 4 chars - 12 characters
52
+ #
53
+ # This function re-introduces the dashes since we removed them during
54
+ # serialization, so:
55
+ #
56
+ # add_dashes("2b4a233152694c6e9d1e098804ab812b")
57
+ # => "2b4a2331-5269-4c6e-9d1e-098804ab812b"
58
+ #
59
+ def add_dashes(uuid)
60
+ return uuid if uuid =~ /\-/
61
+ [uuid[0..7], uuid[8..11], uuid[12..15], uuid[16..19], uuid[20..-1]].join("-")
62
+ end
63
+
64
+ # A UUID has 4 dashes is displayed with 4 dashes at the same place all
65
+ # the time. So they don't add anything semantically. We can safely remove
66
+ # them before storing to the database, and re-add them whenever we
67
+ # retrieved a value from the database.
68
+ #
69
+ # strip_dashes("2b4a2331-5269-4c6e-9d1e-098804ab812b")
70
+ # => "2b4a233152694c6e9d1e098804ab812b"
71
+ #
72
+ def strip_dashes(uuid)
73
+ uuid.delete("-")
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module MySQLBinUUID
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "mysql-binuuid/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "mysql-binuuid-rails"
9
+ spec.version = MySQLBinUUID::VERSION
10
+ spec.authors = ["Mark Oude Veldhuis"]
11
+ spec.email = ["mark.oudeveldhuis@nedap.com"]
12
+
13
+ spec.summary = "Let ActiveRecord serialize and cast your UUIDs to and from binary columns in your database."
14
+ spec.homepage = "https://github.com/nedap/mysql-binuuid-rails"
15
+ spec.license = "MIT"
16
+
17
+ spec.require_paths = ["lib"]
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.required_ruby_version = ">= 2.2"
23
+
24
+ spec.add_runtime_dependency "rails", ENV["RAILS_VERSION"] || ">= 5"
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "mysql2"
29
+ spec.add_development_dependency "minitest"
30
+ spec.add_development_dependency "minitest-spec-context"
31
+ spec.add_development_dependency "minitest-hooks"
32
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql-binuuid-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Oude Veldhuis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mysql2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-spec-context
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-hooks
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - mark.oudeveldhuis@nedap.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".ruby-version"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/mysql-binuuid-rails.rb
129
+ - lib/mysql-binuuid/type.rb
130
+ - lib/mysql-binuuid/version.rb
131
+ - mysql-binuuid-rails.gemspec
132
+ homepage: https://github.com/nedap/mysql-binuuid-rails
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '2.2'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.6.11
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Let ActiveRecord serialize and cast your UUIDs to and from binary columns
156
+ in your database.
157
+ test_files: []