cool_id 0.1.1 → 0.1.2
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/lib/cool_id/version.rb +1 -1
- data/lib/cool_id.rb +48 -39
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e34a657a23c3321adf021f9ab101a6cee1f792a3e8b71c11702663dd92ac6883
|
4
|
+
data.tar.gz: f364193fe7ec136a8a436032a5de7b16a7db6b32b4901baa72f3eaa4a39267ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54f60190090f171c8c9f3df697378ad65fb9224551b208fadaddf0b4e2da37818126cbb52127a79e32ede2156f9e59cfe81eedfd20a7bd50dcac83371056806e
|
7
|
+
data.tar.gz: eb277e15108b217785785803a854d6f88405d2e9fd2b8bcf84583890fbb1fb8a48b2392c2b2a9aa20567d547f92fd5ccd2bce946636acc8ccb1e7781d8985e87
|
data/lib/cool_id/version.rb
CHANGED
data/lib/cool_id.rb
CHANGED
@@ -3,29 +3,46 @@
|
|
3
3
|
require_relative "cool_id/version"
|
4
4
|
require "nanoid"
|
5
5
|
require "active_support/concern"
|
6
|
-
require "active_record"
|
7
6
|
|
8
7
|
module CoolId
|
9
8
|
class Error < StandardError; end
|
10
9
|
|
10
|
+
DEFAULT_SEPARATOR = "_"
|
11
|
+
DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
|
12
|
+
DEFAULT_LENGTH = 12
|
13
|
+
|
14
|
+
Id = Struct.new(:key, :prefix, :id, :model_class)
|
15
|
+
|
11
16
|
class << self
|
12
|
-
attr_accessor :separator
|
17
|
+
attr_accessor :separator, :alphabet, :length
|
13
18
|
|
14
19
|
def configure
|
15
20
|
yield self
|
16
21
|
end
|
17
22
|
|
23
|
+
def reset_configuration
|
24
|
+
self.separator = DEFAULT_SEPARATOR
|
25
|
+
self.alphabet = DEFAULT_ALPHABET
|
26
|
+
self.length = DEFAULT_LENGTH
|
27
|
+
end
|
28
|
+
|
18
29
|
def registry
|
19
30
|
@registry ||= Registry.new
|
20
31
|
end
|
21
32
|
|
22
33
|
def generate_id(config)
|
23
|
-
|
24
|
-
|
34
|
+
alphabet = config.alphabet || @alphabet
|
35
|
+
length = config.length || @length
|
36
|
+
id = Nanoid.generate(size: length, alphabet: alphabet)
|
37
|
+
|
38
|
+
"#{config.prefix}#{separator}#{id}"
|
25
39
|
end
|
26
40
|
end
|
27
41
|
|
28
|
-
|
42
|
+
# defaults based on https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api
|
43
|
+
self.separator = DEFAULT_SEPARATOR
|
44
|
+
self.alphabet = DEFAULT_ALPHABET
|
45
|
+
self.length = DEFAULT_LENGTH
|
29
46
|
|
30
47
|
class Registry
|
31
48
|
def initialize
|
@@ -36,43 +53,40 @@ module CoolId
|
|
36
53
|
@registry[prefix] = model_class
|
37
54
|
end
|
38
55
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def find_record(id)
|
44
|
-
prefix, _ = id.split(CoolId.separator, 2)
|
45
|
-
model_class = find_model(prefix)
|
46
|
-
model_class&.find_by(id: id)
|
56
|
+
def locate(id)
|
57
|
+
parsed = parse(id)
|
58
|
+
parsed&.model_class&.find_by(id: id)
|
47
59
|
end
|
48
60
|
|
49
|
-
def
|
50
|
-
prefix,
|
51
|
-
model_class =
|
52
|
-
model_class
|
61
|
+
def parse(id)
|
62
|
+
prefix, key = id.split(CoolId.separator, 2)
|
63
|
+
model_class = @registry[prefix]
|
64
|
+
return nil unless model_class
|
65
|
+
Id.new(key, prefix, id, model_class)
|
53
66
|
end
|
54
67
|
end
|
55
68
|
|
56
69
|
class Config
|
57
70
|
attr_reader :prefix, :length, :alphabet
|
58
71
|
|
59
|
-
def initialize(prefix
|
60
|
-
@prefix = prefix
|
72
|
+
def initialize(prefix:, length: nil, alphabet: nil)
|
61
73
|
@length = length
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
def alphabet=(value)
|
66
|
-
validate_alphabet(value)
|
67
|
-
@alphabet = value
|
74
|
+
@prefix = validate_prefix(prefix)
|
75
|
+
@alphabet = validate_alphabet(alphabet)
|
68
76
|
end
|
69
77
|
|
70
78
|
private
|
71
79
|
|
80
|
+
def validate_prefix(value)
|
81
|
+
raise ArgumentError, "Prefix cannot be nil" if value.nil?
|
82
|
+
raise ArgumentError, "Prefix cannot consist only of whitespace" if value.strip.empty?
|
83
|
+
value
|
84
|
+
end
|
85
|
+
|
72
86
|
def validate_alphabet(value)
|
73
|
-
if value.
|
74
|
-
|
75
|
-
|
87
|
+
return nil if value.nil?
|
88
|
+
raise ArgumentError, "Alphabet cannot include the separator '#{CoolId.separator}'" if value.include?(CoolId.separator)
|
89
|
+
value
|
76
90
|
end
|
77
91
|
end
|
78
92
|
|
@@ -82,18 +96,13 @@ module CoolId
|
|
82
96
|
class_methods do
|
83
97
|
attr_reader :cool_id_config
|
84
98
|
|
85
|
-
def cool_id(options
|
86
|
-
register_cool_id(options)
|
87
|
-
end
|
88
|
-
|
89
|
-
def register_cool_id(options = {})
|
90
|
-
raise ArgumentError, "Prefix cannot be empty" if options[:prefix] && options[:prefix].empty?
|
99
|
+
def cool_id(options)
|
91
100
|
@cool_id_config = Config.new(**options)
|
92
101
|
CoolId.registry.register(options[:prefix], self)
|
93
102
|
end
|
94
103
|
|
95
104
|
def generate_cool_id
|
96
|
-
CoolId.generate_id(@cool_id_config
|
105
|
+
CoolId.generate_id(@cool_id_config)
|
97
106
|
end
|
98
107
|
end
|
99
108
|
|
@@ -108,11 +117,11 @@ module CoolId
|
|
108
117
|
end
|
109
118
|
end
|
110
119
|
|
111
|
-
def self.
|
112
|
-
registry.
|
120
|
+
def self.locate(id)
|
121
|
+
registry.locate(id)
|
113
122
|
end
|
114
123
|
|
115
|
-
def self.
|
116
|
-
registry.
|
124
|
+
def self.parse(id)
|
125
|
+
registry.parse(id)
|
117
126
|
end
|
118
127
|
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.2
|
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-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nanoid
|