cool_id 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cool_id/version.rb +1 -1
- data/lib/cool_id.rb +86 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b8bf372b8ba519b55df6abcae1fa6de344364a77ca7c9d6f69e86257debac10
|
4
|
+
data.tar.gz: bfe555391aca0355a559243dc3e9ebd96bf02ee39ab141152941ee613d0cbdce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0d61a8a41f45d8e204a5690e07f8be25a05463783d52ef289e3bfe4f98614159e682d79d2fc889719efe983a2f7cc57db901f3111d89005bfdf50f3322dcc04
|
7
|
+
data.tar.gz: ea9cf71b9c7fc2296eb48e1bf7c80f278ddbcf5113d08e6e1e7de113b1419a7889af698fe9a19427bf22ca89762ece47295f89ad3dc0b31d2affa5e6882ce578
|
data/lib/cool_id/version.rb
CHANGED
data/lib/cool_id.rb
CHANGED
@@ -8,30 +8,92 @@ require "active_record"
|
|
8
8
|
module CoolId
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
class << self
|
12
|
+
attr_accessor :separator
|
13
|
+
|
14
|
+
def configure
|
15
|
+
yield self
|
16
|
+
end
|
17
|
+
|
18
|
+
def registry
|
19
|
+
@registry ||= Registry.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_id(config)
|
23
|
+
id = Nanoid.generate(size: config.length, alphabet: config.alphabet)
|
24
|
+
[config.prefix, id].reject(&:empty?).join(@separator)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
self.separator = "_"
|
29
|
+
|
30
|
+
class Registry
|
31
|
+
def initialize
|
32
|
+
@registry = {}
|
33
|
+
end
|
34
|
+
|
35
|
+
def register(prefix, model_class)
|
36
|
+
@registry[prefix] = model_class
|
37
|
+
end
|
38
|
+
|
39
|
+
def find_model(prefix)
|
40
|
+
@registry[prefix]
|
41
|
+
end
|
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)
|
47
|
+
end
|
48
|
+
|
49
|
+
def find_record!(id)
|
50
|
+
prefix, _ = id.split(CoolId.separator, 2)
|
51
|
+
model_class = find_model(prefix)
|
52
|
+
model_class&.find(id)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Config
|
57
|
+
attr_reader :prefix, :length, :alphabet
|
58
|
+
|
59
|
+
def initialize(prefix: "", length: 12, alphabet: "0123456789abcdefghijklmnopqrstuvwxyz")
|
60
|
+
@prefix = prefix
|
61
|
+
@length = length
|
62
|
+
self.alphabet = alphabet
|
63
|
+
end
|
64
|
+
|
65
|
+
def alphabet=(value)
|
66
|
+
validate_alphabet(value)
|
67
|
+
@alphabet = value
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def validate_alphabet(value)
|
73
|
+
if value.include?(CoolId.separator)
|
74
|
+
raise ArgumentError, "Alphabet cannot include the separator '#{CoolId.separator}'"
|
75
|
+
end
|
76
|
+
end
|
20
77
|
end
|
21
78
|
|
22
79
|
module Model
|
23
80
|
extend ActiveSupport::Concern
|
24
81
|
|
25
82
|
class_methods do
|
26
|
-
|
83
|
+
attr_reader :cool_id_config
|
84
|
+
|
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?
|
91
|
+
@cool_id_config = Config.new(**options)
|
92
|
+
CoolId.registry.register(options[:prefix], self)
|
93
|
+
end
|
27
94
|
|
28
95
|
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
|
-
)
|
96
|
+
CoolId.generate_id(@cool_id_config || Config.new)
|
35
97
|
end
|
36
98
|
end
|
37
99
|
|
@@ -45,4 +107,12 @@ module CoolId
|
|
45
107
|
end
|
46
108
|
end
|
47
109
|
end
|
110
|
+
|
111
|
+
def self.find(id)
|
112
|
+
registry.find_record(id)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.find!(id)
|
116
|
+
registry.find_record!(id)
|
117
|
+
end
|
48
118
|
end
|