exid 0.1.3 → 0.2.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.
- checksums.yaml +4 -4
- data/.standard.yml +2 -0
- data/README.md +14 -1
- data/docker-compose.yml +0 -6
- data/lib/exid/configuration.rb +39 -0
- data/lib/exid/record.rb +2 -2
- data/lib/exid/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2b2ce981986763c3197368b44dbc4af5708092377782740f88f4dd330d7f8dea
|
|
4
|
+
data.tar.gz: dc1645db5ca8b3cf7aa70024f5bccb9257691e274d171d1e8f57bf7d9d2ad73d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a9c44e59f3281cf590625dce868f9e645db4801af267a02ffe67e6056baa9da24764be97a19f2c3845f2683a4b014330c7d3b8135c7b684284be4d3f1d82424c
|
|
7
|
+
data.tar.gz: 285dd3db1dc491bbb075e6201be7f21e10c07b8d74b952603aa00288d5aece61db4b59d7bae731209d347b4bd78318371ae8547df53085496486a4786b74bbe9
|
data/.standard.yml
ADDED
data/README.md
CHANGED
|
@@ -82,6 +82,19 @@ user.exid_handle # => "OBtqZqRhLm"
|
|
|
82
82
|
user.exid_handle(6) # => "ZqRhLm"
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
### Configuration
|
|
86
|
+
|
|
87
|
+
You can configure Exid's prefix validation at the application level. Create an initializer (e.g., `config/initializers/exid.rb` in Rails):
|
|
88
|
+
|
|
89
|
+
By default, prefixes are limited to 4 characters. You can provide a custom validator proc that returns `true` if the prefix is valid, `false` otherwise:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
Exid.configure do |config|
|
|
93
|
+
config.prefix_validator = proc { it.match?(/\A[a-z]{2,6}\z/) }
|
|
94
|
+
end
|
|
95
|
+
```
|
|
96
|
+
**Note**: When you set a custom `prefix_validator`, it replaces the default 4-character length validation. Your validator has full control over validation logic.
|
|
97
|
+
|
|
85
98
|
### Loading Records by External ID
|
|
86
99
|
|
|
87
100
|
Use the class method `exid_loader` to load a record using its external ID:
|
|
@@ -96,7 +109,7 @@ The `Exid::Record` module also provides global loading methods that mimic Rails
|
|
|
96
109
|
|
|
97
110
|
```ruby
|
|
98
111
|
Exid::Record.fetch!("pref_02WoeojY8dqVYcAhs321rm") # Raises exception if not found
|
|
99
|
-
Exid::Record.fetch("pref_02WoeojY8dqVYcAhs321rm")
|
|
112
|
+
Exid::Record.fetch("pref_02WoeojY8dqVYcAhs321rm") # Returns `nil` if not found
|
|
100
113
|
```
|
|
101
114
|
|
|
102
115
|
> **⚠️ Security Warning**: Exercise caution when using global loading methods with user-supplied identifiers, as this could lead to unexpected results or security issues if users substitute identifiers.
|
data/docker-compose.yml
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Exid
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :prefix_validator
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@prefix_validator = default_validator
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def validate_prefix(prefix)
|
|
12
|
+
return if prefix_validator.call(prefix)
|
|
13
|
+
|
|
14
|
+
raise Error, "Prefix validation failed for: #{prefix}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def default_validator
|
|
20
|
+
proc { |prefix| prefix.length <= 4 }
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class << self
|
|
25
|
+
attr_writer :configuration
|
|
26
|
+
|
|
27
|
+
def configuration
|
|
28
|
+
@_configuration ||= Configuration.new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def configure
|
|
32
|
+
yield(configuration)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def reset_configuration!
|
|
36
|
+
@configuration = Configuration.new
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/exid/record.rb
CHANGED
|
@@ -29,7 +29,7 @@ module Exid
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def initialize(prefix, field)
|
|
32
|
-
|
|
32
|
+
Exid.configuration.validate_prefix(prefix)
|
|
33
33
|
|
|
34
34
|
@module_static = build_module_static(prefix, field)
|
|
35
35
|
@module_value = build_module_value(prefix, field)
|
|
@@ -63,7 +63,7 @@ module Exid
|
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def self.find_module(prefix)
|
|
66
|
-
registered_modules.detect {
|
|
66
|
+
registered_modules.detect { _1.prefix == prefix } or
|
|
67
67
|
raise Error, "Model for \"#{prefix}\" not found"
|
|
68
68
|
end
|
|
69
69
|
|
data/lib/exid/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: exid
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Doe
|
|
@@ -32,6 +32,7 @@ extra_rdoc_files: []
|
|
|
32
32
|
files:
|
|
33
33
|
- ".rspec"
|
|
34
34
|
- ".ruby-version"
|
|
35
|
+
- ".standard.yml"
|
|
35
36
|
- Dockerfile
|
|
36
37
|
- LICENSE.txt
|
|
37
38
|
- README.md
|
|
@@ -40,6 +41,7 @@ files:
|
|
|
40
41
|
- lib/exid.rb
|
|
41
42
|
- lib/exid/base62.rb
|
|
42
43
|
- lib/exid/coder.rb
|
|
44
|
+
- lib/exid/configuration.rb
|
|
43
45
|
- lib/exid/error.rb
|
|
44
46
|
- lib/exid/record.rb
|
|
45
47
|
- lib/exid/version.rb
|
|
@@ -63,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
63
65
|
- !ruby/object:Gem::Version
|
|
64
66
|
version: '0'
|
|
65
67
|
requirements: []
|
|
66
|
-
rubygems_version: 3.
|
|
68
|
+
rubygems_version: 3.7.2
|
|
67
69
|
specification_version: 4
|
|
68
70
|
summary: Easy External identifier management for models
|
|
69
71
|
test_files: []
|