cool_id 0.1.5 → 0.1.6
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 +4 -4
- data/LICENSE +15 -0
- data/README.md +61 -18
- data/lib/cool_id/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d44c3207248595af232a200cf0e809224d74033d97d3cac52c6d06d5416b682
|
4
|
+
data.tar.gz: d221baeb8e79ea8ea8ca2b9503685e4bfa7cb8d57b53a3ba0e4d301138a08300
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ed700560671d811b0678c4fbe8f9e086c3ca0c9e8d04a54b7d40d586b20864e2add65adb9e5e53feb7915e472974dbfa6b31313dd6a64f4008aaf3f31503f70
|
7
|
+
data.tar.gz: df1cdc3098d9b4761a220fedeca362eeaeb5677d92ca7da594dc946223d814c81bb59db757ff08292fe72e3ce499dd95e1cc002ccf27c436aa097617bd14f2a9
|
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
|
-
a gem for rails that generates string ids for active record models with a per-model prefix followed by a nanoid.
|
3
|
+
a gem for rails that generates string primary key ids for active record models with a per-model prefix followed by a nanoid. this lets you create stripe style ids in your own rails app, and they'll be the same ids you see in your database.
|
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
|
-
it can also lookup records
|
15
|
+
it can also lookup records similar to global 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
|
+
### using cool_id in one 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
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Schilling
|
@@ -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
|