cool_id 0.1.3 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a61d8d06c3414dcec52ecd9a3f28696d0fafb683b2b206e025c33a1deb03de9
4
- data.tar.gz: bc13a903ed6d5239eae6ee216f5da45699a525d755e914f9f9c2b81b6e33d26c
3
+ metadata.gz: 0d44c3207248595af232a200cf0e809224d74033d97d3cac52c6d06d5416b682
4
+ data.tar.gz: d221baeb8e79ea8ea8ca2b9503685e4bfa7cb8d57b53a3ba0e4d301138a08300
5
5
  SHA512:
6
- metadata.gz: 77aac33c12253962d7beabffc5d7101379f40d991b580c67ea0a306cd82f8d2befda21b23fbd3c3735a516863ef4c3565b18084f67ed001172b7a395527f3f4a
7
- data.tar.gz: d3c269343bd10098fdd902424c10c22e676a0cea67d5dff404628863ea4d085319017b31d6c602364da024d36c30a470e1bd3ade7b687379bbbd3f011a72191c
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,31 +1,132 @@
1
- # CoolId
1
+ # cool_id
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cool_id`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ```ruby
6
+ class User < ActiveRecord::Base
7
+ include CoolId::Model
8
+ cool_id prefix: "usr"
9
+ end
6
10
 
7
- ## Installation
11
+ User.create!(name: "...").id
12
+ # => "usr_vktd1b5v84lr"
13
+ ```
8
14
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
15
+ it can also lookup records similar to global id:
10
16
 
11
- Install the gem and add to the application's Gemfile by executing:
17
+ ```ruby
18
+ CoolId.locate("usr_vktd1b5v84lr")
19
+ # => #<User id: "usr_vktd1b5v84lr", name: "John Doe">
20
+ ```
12
21
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
+ and parse ids
14
23
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
24
+ ```ruby
25
+ parsed = CoolId.parse("usr_vktd1b5v84lr")
26
+ # => #<struct CoolId::Id key="vktd1b5v84lr", prefix="usr", id="usr_vktd1b5v84lr", model_class=User>
16
27
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
28
+ parsed.model_class
29
+ # => User
30
+ ```
18
31
 
19
- ## Usage
32
+ and generate ids without creating a record
20
33
 
21
- TODO: Write usage instructions here
34
+ ```ruby
35
+ # generate an id, e.g. for batch inserts or upserts
36
+ User.generate_cool_id
37
+ # => "usr_vktd1b5v84lr"
22
38
 
23
- ## Development
39
+ ```
24
40
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+ it takes parameters to change the alphabet or length
26
42
 
27
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+ ```ruby
44
+ class Customer < ActiveRecord::Base
45
+ include CoolId::Model
46
+ cool_id prefix: "cus", alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", length: 8
47
+ end
28
48
 
29
- ## Contributing
49
+ Customer.create!(name: "...").id
50
+ # => "cus_UHNYBINU"
51
+ ```
30
52
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cool_id.
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
+
63
+ ## installation
64
+
65
+ add cool_id to your Gemfile:
66
+
67
+ ```bash
68
+ bundle add cool_id
69
+ ```
70
+
71
+ ```ruby
72
+ gem "cool_id"
73
+ ```
74
+
75
+ ### using cool_id in one model
76
+
77
+ use string ids when creating a table
78
+
79
+ ```ruby
80
+ create_table :users, id: :string do |t|
81
+ t.string :name
82
+ end
83
+ ```
84
+
85
+ include the `CoolId::Model` concern in the active record model and set up a prefix
86
+
87
+ ```ruby
88
+ class User < ActiveRecord::Base
89
+ include CoolId::Model
90
+ cool_id prefix: "usr"
91
+ end
92
+ ```
93
+
94
+ ### using cool_id on all models
95
+
96
+ you have drank the coolaid. setup rails to use string ids on all new generated migrations
97
+
98
+ ```ruby
99
+ # config/initializers/generators.rb
100
+ Rails.application.config.generators do |g|
101
+ g.orm :active_record, primary_key_type: :string
102
+ end
103
+ ```
104
+
105
+ then setup `ApplicationRecord` to include cool id and ensure it's setup in classes that inherit from it
106
+
107
+ ```ruby
108
+ # app/models/application_record.rb
109
+ class ApplicationRecord < ActiveRecord::Base
110
+ include CoolId::Model
111
+ primary_abstract_class
112
+ enforce_cool_id_for_descendants
113
+ end
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
+ ```
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoolId
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.6"
5
5
  end
data/lib/cool_id.rb CHANGED
@@ -106,11 +106,11 @@ module CoolId
106
106
  CoolId.generate_id(@cool_id_config)
107
107
  end
108
108
 
109
- def ensure_cool_id_setup
109
+ def enforce_cool_id_for_descendants
110
110
  @cool_id_setup_required = true
111
111
  end
112
112
 
113
- def skip_cool_id_setup
113
+ def skip_enforce_cool_id_for_descendants
114
114
  @cool_id_setup_required = false
115
115
  end
116
116
 
@@ -134,7 +134,7 @@ 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
- raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_cool_id_setup' to opt out."
137
+ raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id_for_descendants' to opt out."
138
138
  end
139
139
  end
140
140
  end
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.3
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