cool_id 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/README.md +31 -0
- data/Rakefile +10 -0
- data/lib/cool_id/version.rb +5 -0
- data/lib/cool_id.rb +48 -0
- data/sig/cool_id.rbs +4 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 31e57670575a5ab4ca42bdd225ada7b79aad7ff9bbd5ad9ffd72374cd121bf07
|
4
|
+
data.tar.gz: 02e56f84df9d458da1e7e93dc7ec8ff5bcfb33fdfd399f58fed40b36cd3995ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91c18c5d5a6be61f1f6ece0ddb1b7625c3d0c4675ec690550785221244a889b808c8032b22b79e8987d46cf5e5435f3f11b332298edc57a149348bf6f6f312f4
|
7
|
+
data.tar.gz: 138806308d00d10e64387254bfaef9650fbfe48e89d2201d301b385f26b9713120ee86ec8b0ed750e138accfa30ae1dfd7f15d3604b1b56275d5d3e40955f825
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# CoolId
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
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.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
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.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
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.
|
26
|
+
|
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).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cool_id.
|
data/Rakefile
ADDED
data/lib/cool_id.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "cool_id/version"
|
4
|
+
require "nanoid"
|
5
|
+
require "active_support/concern"
|
6
|
+
require "active_record"
|
7
|
+
|
8
|
+
module CoolId
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
# defaults copped from
|
12
|
+
# https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api
|
13
|
+
DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
|
14
|
+
DEFAULT_SEPARATOR = "_"
|
15
|
+
DEFAULT_LENGTH = 12
|
16
|
+
|
17
|
+
def self.generate_id(prefix: "", separator: DEFAULT_SEPARATOR, length: DEFAULT_LENGTH, alphabet: DEFAULT_ALPHABET)
|
18
|
+
id = Nanoid.generate(size: length, alphabet: alphabet)
|
19
|
+
[prefix, id].reject(&:empty?).join(separator)
|
20
|
+
end
|
21
|
+
|
22
|
+
module Model
|
23
|
+
extend ActiveSupport::Concern
|
24
|
+
|
25
|
+
class_methods do
|
26
|
+
attr_accessor :cool_id_prefix, :cool_id_separator, :cool_id_alphabet, :cool_id_length
|
27
|
+
|
28
|
+
def generate_cool_id
|
29
|
+
CoolId.generate_id(
|
30
|
+
prefix: cool_id_prefix,
|
31
|
+
separator: cool_id_separator || DEFAULT_SEPARATOR,
|
32
|
+
length: cool_id_length || DEFAULT_LENGTH,
|
33
|
+
alphabet: cool_id_alphabet || DEFAULT_ALPHABET
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
included do
|
39
|
+
before_create :set_cool_id
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def set_cool_id
|
44
|
+
self.id = self.class.generate_cool_id if id.blank?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/sig/cool_id.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cool_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Schilling
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nanoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '6.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.4'
|
69
|
+
description: generates primary keys using prefixed nanoids for ActiveRecord models
|
70
|
+
email:
|
71
|
+
- git@schpet.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".rspec"
|
77
|
+
- ".standard.yml"
|
78
|
+
- CHANGELOG.md
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/cool_id.rb
|
82
|
+
- lib/cool_id/version.rb
|
83
|
+
- sig/cool_id.rbs
|
84
|
+
homepage: https://github.com/schpet/cool_id
|
85
|
+
licenses: []
|
86
|
+
metadata:
|
87
|
+
homepage_uri: https://github.com/schpet/cool_id
|
88
|
+
source_code_uri: https://github.com/schpet/cool_id
|
89
|
+
changelog_uri: https://github.com/schpet/cool_id/tree/main/CHANGELOG.md
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 3.0.0
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.5.11
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: generates cool ids
|
109
|
+
test_files: []
|