ulid-rails 0.1.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 +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +49 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ulid/rails.rb +42 -0
- data/lib/ulid/rails/formatter.rb +23 -0
- data/lib/ulid/rails/patch.rb +13 -0
- data/lib/ulid/rails/type.rb +32 -0
- data/lib/ulid/rails/version.rb +5 -0
- data/ulid-rails.gemspec +32 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 83a5f3c3e4d66efd9d2df7948564b0e9603c1740dd2f3d8114f53d9db1da2d6b
|
4
|
+
data.tar.gz: ce3fa484e01d9886b24e9ad6c2e3ea18c7ddd7f2608888af48ef758abafd756e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6782bc00605c44caec5faeaa1ec5e94f5db637226300a1db95cb852633405bf95ca73099dcd275eb4b59c72fe3a140b40d722aebcccc86a56c0bd5b07f233731
|
7
|
+
data.tar.gz: aaa614bb9fca638ecad81d0c5f76027ef464f7ba1e9b1e4d1493455d56f23090d03dcaaf2e1cbae27da7b40a7a2bda7f06e8d6e371d7a586e8a78385a10dde8a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ulid-rails (0.1.0)
|
5
|
+
activemodel (>= 5.0)
|
6
|
+
activerecord (>= 5.0)
|
7
|
+
activesupport (>= 5.0)
|
8
|
+
base32-crockford (~> 0.1)
|
9
|
+
ulid (~> 1.0)
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
activemodel (5.2.1)
|
15
|
+
activesupport (= 5.2.1)
|
16
|
+
activerecord (5.2.1)
|
17
|
+
activemodel (= 5.2.1)
|
18
|
+
activesupport (= 5.2.1)
|
19
|
+
arel (>= 9.0)
|
20
|
+
activesupport (5.2.1)
|
21
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
22
|
+
i18n (>= 0.7, < 2)
|
23
|
+
minitest (~> 5.1)
|
24
|
+
tzinfo (~> 1.1)
|
25
|
+
arel (9.0.0)
|
26
|
+
base32-crockford (0.1.0)
|
27
|
+
concurrent-ruby (1.0.5)
|
28
|
+
i18n (1.1.0)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
minitest (5.11.3)
|
31
|
+
rake (10.5.0)
|
32
|
+
sysrandom (1.0.5)
|
33
|
+
thread_safe (0.3.6)
|
34
|
+
tzinfo (1.2.5)
|
35
|
+
thread_safe (~> 0.1)
|
36
|
+
ulid (1.0.0)
|
37
|
+
sysrandom (>= 1.0.0, < 2.0)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
bundler (~> 1.16)
|
44
|
+
minitest (~> 5.0)
|
45
|
+
rake (~> 10.0)
|
46
|
+
ulid-rails!
|
47
|
+
|
48
|
+
BUNDLED WITH
|
49
|
+
1.16.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Kazunori Kajihiro
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# ULID::Rails
|
2
|
+
|
3
|
+
This gem makes it possible to use [ULID](https://github.com/ulid/spec) for DB primary keys in a Ruby on Rails app.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ulid-rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ulid-rails
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Migrations
|
24
|
+
|
25
|
+
If you use MySQL, specify `id: false` to `create_table` and add `id` column as 16-byte binary type.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
def change
|
29
|
+
create_table :users, id: false do |t|
|
30
|
+
t.binary :id, limit: 16, primary_key: true # MySQL
|
31
|
+
# ...
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
If you use PostgreSQL, just specify `id: :uuid` to `create_table`
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
def change
|
40
|
+
create_table :users, id: :uuid do |t|
|
41
|
+
# ...
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
### Model Changes
|
48
|
+
|
49
|
+
Just add the below lines to your models.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class MyModel < ApplicationRecord
|
53
|
+
include ULID::Rails
|
54
|
+
ulid :id # The argument is the primary key column name
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
### `created_at` virtual column
|
59
|
+
|
60
|
+
**MySQL Only (for now)**
|
61
|
+
|
62
|
+
Since ULID includes milli seconds precision timestamp, you don't need to store `created_at`. You can instead create a virtual column that extracts the timestamp from the ULID column.
|
63
|
+
|
64
|
+
Defining the virtual `created_at` is kind of comlicated so this gem provides a helper method for it.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
create_table :users, id: false do |t|
|
68
|
+
t.binary :id, limit: 16, primary_key: true
|
69
|
+
t.datetime :updated_at
|
70
|
+
t.virtual_ulid_timestamp :created_at, :id
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
`virtual_ulid_timestamp` takes two arguments, the first one is the name of the column name (typically, `created_at`) and the second one is the ULID column that creation timestamp is extracted from.
|
75
|
+
|
76
|
+
## FAQ
|
77
|
+
|
78
|
+
### Foreign Keys
|
79
|
+
|
80
|
+
You need to specicfy `type` option
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
# MySQL
|
84
|
+
create_table :admin_usees do |t|
|
85
|
+
t.references :admin_user, foreign_key: true, type: "BINARY(16)"
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
## License
|
90
|
+
|
91
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ulid/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/ulid/rails.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_support/concern"
|
3
|
+
require "active_model/type"
|
4
|
+
require "ulid"
|
5
|
+
require "base32/crockford"
|
6
|
+
require "ulid/rails/version"
|
7
|
+
require "ulid/rails/type"
|
8
|
+
require "ulid/rails/patch"
|
9
|
+
|
10
|
+
module ULID
|
11
|
+
module Rails
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
def ulid(column_name = :id, primary_key: true)
|
16
|
+
attribute column_name, ULID::Rails::Type.new
|
17
|
+
|
18
|
+
before_create do
|
19
|
+
send("#{column_name}=", ULID.generate) if send(column_name).nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
if primary_key
|
23
|
+
define_method :created_at do
|
24
|
+
at = super() rescue nil
|
25
|
+
if !at && (id_val = send(column_name))
|
26
|
+
Time.zone.at((Base32::Crockford.decode(id_val) >> 80) / 1000.0)
|
27
|
+
else
|
28
|
+
at
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
define_singleton_method(:timestamp_attributes_for_create) do
|
33
|
+
[]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ActiveModel::Type.register(:ulid, ULID::Rails::Type)
|
40
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Patch::Migrations
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "base32/crockford"
|
2
|
+
|
3
|
+
module ULID
|
4
|
+
module Rails
|
5
|
+
module Formatter
|
6
|
+
def self.format(v)
|
7
|
+
if v.length == 32
|
8
|
+
Base32::Crockford.encode(v.hex)
|
9
|
+
else
|
10
|
+
v
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.unformat(v)
|
15
|
+
Base32::Crockford.decode(v).to_s(16).rjust(32, "0")
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.regexp
|
19
|
+
/\A[[:alnum:]]{25}\z/
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ULID
|
2
|
+
module Rails
|
3
|
+
module Patch
|
4
|
+
module Migrations
|
5
|
+
def virtual_ulid_timestamp(timestamp_column_name, ulid_column_name)
|
6
|
+
virtual timestamp_column_name,
|
7
|
+
type: :datetime,
|
8
|
+
as: "FROM_UNIXTIME(CONV(HEX(#{ulid_column_name} >> 80), 16, 10) / 1000.0)"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "active_model/type"
|
2
|
+
require "ulid/rails/formatter"
|
3
|
+
|
4
|
+
module ULID
|
5
|
+
module Rails
|
6
|
+
class Type < ActiveModel::Type::Binary
|
7
|
+
class Data < ActiveModel::Type::Binary::Data
|
8
|
+
alias_method :hex, :to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(formatter = Formatter)
|
12
|
+
@formatter = formatter
|
13
|
+
super()
|
14
|
+
end
|
15
|
+
|
16
|
+
def cast(value)
|
17
|
+
if value.is_a?(Data)
|
18
|
+
@formatter.format(value.to_s)
|
19
|
+
elsif value&.encoding == Encoding::ASCII_8BIT
|
20
|
+
@formatter.format(value.unpack("H*")[0])
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def serialize(value)
|
27
|
+
return if value.nil?
|
28
|
+
Data.new(@formatter.unformat(value))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/ulid-rails.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "ulid/rails/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ulid-rails"
|
7
|
+
spec.version = ULID::Rails::VERSION
|
8
|
+
spec.authors = ["Kazunori Kajihiro"]
|
9
|
+
spec.email = ["kazunori.kajihiro@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{ULID for rails}
|
12
|
+
spec.description = %q{ULID for rails}
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "ulid", "~> 1.0"
|
25
|
+
spec.add_dependency "base32-crockford", "~> 0.1"
|
26
|
+
spec.add_dependency "activesupport", ">= 5.0"
|
27
|
+
spec.add_dependency "activemodel", ">= 5.0"
|
28
|
+
spec.add_dependency "activerecord", ">= 5.0"
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ulid-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kazunori Kajihiro
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ulid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: base32-crockford
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :runtime
|
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: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.16'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.16'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: minitest
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '5.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '5.0'
|
125
|
+
description: ULID for rails
|
126
|
+
email:
|
127
|
+
- kazunori.kajihiro@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".travis.yml"
|
134
|
+
- Gemfile
|
135
|
+
- Gemfile.lock
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- bin/console
|
140
|
+
- bin/setup
|
141
|
+
- lib/ulid/rails.rb
|
142
|
+
- lib/ulid/rails/formatter.rb
|
143
|
+
- lib/ulid/rails/patch.rb
|
144
|
+
- lib/ulid/rails/type.rb
|
145
|
+
- lib/ulid/rails/version.rb
|
146
|
+
- ulid-rails.gemspec
|
147
|
+
homepage:
|
148
|
+
licenses:
|
149
|
+
- MIT
|
150
|
+
metadata: {}
|
151
|
+
post_install_message:
|
152
|
+
rdoc_options: []
|
153
|
+
require_paths:
|
154
|
+
- lib
|
155
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
requirements: []
|
166
|
+
rubyforge_project:
|
167
|
+
rubygems_version: 2.7.6
|
168
|
+
signing_key:
|
169
|
+
specification_version: 4
|
170
|
+
summary: ULID for rails
|
171
|
+
test_files: []
|