cool_id 0.1.0 → 0.1.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cool_id/version.rb +1 -1
  3. data/lib/cool_id.rb +86 -16
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31e57670575a5ab4ca42bdd225ada7b79aad7ff9bbd5ad9ffd72374cd121bf07
4
- data.tar.gz: 02e56f84df9d458da1e7e93dc7ec8ff5bcfb33fdfd399f58fed40b36cd3995ea
3
+ metadata.gz: 7b8bf372b8ba519b55df6abcae1fa6de344364a77ca7c9d6f69e86257debac10
4
+ data.tar.gz: bfe555391aca0355a559243dc3e9ebd96bf02ee39ab141152941ee613d0cbdce
5
5
  SHA512:
6
- metadata.gz: 91c18c5d5a6be61f1f6ece0ddb1b7625c3d0c4675ec690550785221244a889b808c8032b22b79e8987d46cf5e5435f3f11b332298edc57a149348bf6f6f312f4
7
- data.tar.gz: 138806308d00d10e64387254bfaef9650fbfe48e89d2201d301b385f26b9713120ee86ec8b0ed750e138accfa30ae1dfd7f15d3604b1b56275d5d3e40955f825
6
+ metadata.gz: e0d61a8a41f45d8e204a5690e07f8be25a05463783d52ef289e3bfe4f98614159e682d79d2fc889719efe983a2f7cc57db901f3111d89005bfdf50f3322dcc04
7
+ data.tar.gz: ea9cf71b9c7fc2296eb48e1bf7c80f278ddbcf5113d08e6e1e7de113b1419a7889af698fe9a19427bf22ca89762ece47295f89ad3dc0b31d2affa5e6882ce578
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoolId
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
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
- # 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)
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
- attr_accessor :cool_id_prefix, :cool_id_separator, :cool_id_alphabet, :cool_id_length
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
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Schilling