cool_id 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +15 -0
- data/README.md +62 -19
- data/lib/cool_id/version.rb +1 -1
- data/lib/cool_id.rb +4 -3
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd52ffe11cf7b25bf473b06e879a7df67eb4aea3b9169b0961342fef30a6fce7
|
4
|
+
data.tar.gz: b92c14dd88880ad52de1884f8a1e2720915b5ee24b742c0db8a966071bd07196
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f41bf3c022796fdaf4e8f53af035b6515c6a6b5ac59b749c52a17247bb6710b7296424d2a86c20f484500cae15f7daec32ec1a3980717441a0fec186ad528945
|
7
|
+
data.tar.gz: c81557d5b123b122be77208ef6c74421d81c048dd409f289421ee1bfbaea075550621c77d0c6d92f5b11df393b4fd3be820e62cf2ef491a8e48958705e218d5c
|
data/LICENSE
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ISC License
|
2
|
+
|
3
|
+
Copyright (c) Peter Schilling
|
4
|
+
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# cool id
|
2
2
|
|
3
|
-
|
3
|
+
gem for rails apps to generates string ids with a prefix, followed by a [nanoid](https://zelark.github.io/nano-id-cc/). similar to the ids you see in stripe's api. also able to lookup any record by id, similar to rails' globalid.
|
4
4
|
|
5
5
|
```ruby
|
6
6
|
class User < ActiveRecord::Base
|
@@ -10,26 +10,18 @@ end
|
|
10
10
|
|
11
11
|
User.create!(name: "...").id
|
12
12
|
# => "usr_vktd1b5v84lr"
|
13
|
-
|
14
|
-
class Customer < ActiveRecord::Base
|
15
|
-
include CoolId::Model
|
16
|
-
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
|
17
|
-
end
|
18
|
-
|
19
|
-
Customer.create!(name: "...").id
|
20
|
-
# => "cus_UHNYBINU"
|
21
13
|
```
|
22
14
|
|
23
|
-
|
15
|
+
lookup any record by its id
|
24
16
|
|
25
17
|
```ruby
|
26
|
-
user = User.create!(name: "John Doe")
|
27
|
-
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
|
28
|
-
|
29
18
|
CoolId.locate("usr_vktd1b5v84lr")
|
30
19
|
# => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
|
20
|
+
```
|
31
21
|
|
32
|
-
|
22
|
+
and parse ids
|
23
|
+
|
24
|
+
```ruby
|
33
25
|
parsed = CoolId.parse("usr_vktd1b5v84lr")
|
34
26
|
# => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>
|
35
27
|
|
@@ -37,8 +29,41 @@ parsed.model_class
|
|
37
29
|
# => User
|
38
30
|
```
|
39
31
|
|
32
|
+
and generate ids without creating a record
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# generate an id, e.g. for batch inserts or upserts
|
36
|
+
User.generate_cool_id
|
37
|
+
# => "usr_vktd1b5v84lr"
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
it takes parameters to change the alphabet or length
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class Customer < ActiveRecord::Base
|
45
|
+
include CoolId::Model
|
46
|
+
cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
|
47
|
+
end
|
48
|
+
|
49
|
+
Customer.create!(name: "...").id
|
50
|
+
# => "cus_UHNYBINU"
|
51
|
+
```
|
52
|
+
|
53
|
+
and these can be configured globally
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
CoolId.configure do |config|
|
57
|
+
config.separator = "-"
|
58
|
+
config.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
59
|
+
config.length = 8
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
40
63
|
## installation
|
41
64
|
|
65
|
+
add cool_id to your Gemfile:
|
66
|
+
|
42
67
|
```bash
|
43
68
|
bundle add cool_id
|
44
69
|
```
|
@@ -47,7 +72,7 @@ bundle add cool_id
|
|
47
72
|
gem "cool_id"
|
48
73
|
```
|
49
74
|
|
50
|
-
###
|
75
|
+
### adding cool_id to a single model
|
51
76
|
|
52
77
|
use string ids when creating a table
|
53
78
|
|
@@ -66,9 +91,9 @@ class User < ActiveRecord::Base
|
|
66
91
|
end
|
67
92
|
```
|
68
93
|
|
69
|
-
### all models
|
94
|
+
### using cool_id on all models
|
70
95
|
|
71
|
-
use string ids on all new generated migrations
|
96
|
+
you have drank the coolaid. setup rails to use string ids on all new generated migrations
|
72
97
|
|
73
98
|
```ruby
|
74
99
|
# config/initializers/generators.rb
|
@@ -77,7 +102,7 @@ Rails.application.config.generators do |g|
|
|
77
102
|
end
|
78
103
|
```
|
79
104
|
|
80
|
-
setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it
|
105
|
+
then setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it
|
81
106
|
|
82
107
|
```ruby
|
83
108
|
# app/models/application_record.rb
|
@@ -87,3 +112,21 @@ class ApplicationRecord < ActiveRecord::Base
|
|
87
112
|
enforce_cool_id_for_descendants
|
88
113
|
end
|
89
114
|
```
|
115
|
+
|
116
|
+
### graphql
|
117
|
+
|
118
|
+
if you use the graphql ruby node interface, you can implement [object identification](https://graphql-ruby.org/schema/object_identification)
|
119
|
+
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# app/graphql/app_schema.rb
|
123
|
+
class AppSchema < GraphQL::Schema
|
124
|
+
def self.id_from_object(object, type_definition, query_ctx)
|
125
|
+
object.id
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.object_from_id(id, query_ctx)
|
129
|
+
CoolId.locate(id)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
data/lib/cool_id/version.rb
CHANGED
data/lib/cool_id.rb
CHANGED
@@ -5,7 +5,7 @@ require "nanoid"
|
|
5
5
|
require "active_support/concern"
|
6
6
|
|
7
7
|
module CoolId
|
8
|
-
class
|
8
|
+
class NotConfiguredError < StandardError; end
|
9
9
|
|
10
10
|
# defaults based on https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api
|
11
11
|
DEFAULT_SEPARATOR = "_"
|
@@ -110,7 +110,7 @@ module CoolId
|
|
110
110
|
@cool_id_setup_required = true
|
111
111
|
end
|
112
112
|
|
113
|
-
def
|
113
|
+
def skip_enforce_cool_id
|
114
114
|
@cool_id_setup_required = false
|
115
115
|
end
|
116
116
|
|
@@ -134,7 +134,8 @@ module CoolId
|
|
134
134
|
|
135
135
|
def ensure_cool_id_configured
|
136
136
|
if self.class.cool_id_setup_required && self.class.cool_id_config.nil?
|
137
|
-
|
137
|
+
suggested_prefix = self.class.name.downcase[0..2]
|
138
|
+
raise NotConfiguredError, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id' to opt out.\n\ne.g.\n\nclass #{self.class} < ApplicationRecord\n cool_id prefix: \"#{suggested_prefix}\"\nend"
|
138
139
|
end
|
139
140
|
end
|
140
141
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cool_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Schilling
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nanoid
|
@@ -76,13 +76,15 @@ files:
|
|
76
76
|
- ".rspec"
|
77
77
|
- ".standard.yml"
|
78
78
|
- CHANGELOG.md
|
79
|
+
- LICENSE
|
79
80
|
- README.md
|
80
81
|
- Rakefile
|
81
82
|
- lib/cool_id.rb
|
82
83
|
- lib/cool_id/version.rb
|
83
84
|
- sig/cool_id.rbs
|
84
85
|
homepage: https://github.com/schpet/cool_id
|
85
|
-
licenses:
|
86
|
+
licenses:
|
87
|
+
- ISC
|
86
88
|
metadata:
|
87
89
|
homepage_uri: https://github.com/schpet/cool_id
|
88
90
|
source_code_uri: https://github.com/schpet/cool_id
|