prefixed_ids 1.2.0 → 1.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/lib/prefixed_ids.rb +4 -6
- data/lib/prefixed_ids/prefix_id.rb +18 -9
- data/lib/prefixed_ids/version.rb +1 -1
- 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: e542fdd356a7d0a1ccc8ad8ca90cc2d218dd4d22e01b2cd82c07ab9e36c52141
|
4
|
+
data.tar.gz: 9c788fc9bf605656ddcfc1c3c3c854eb9fbef1068b1e9b963b6dfa4f6bb72cbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 933bfccd3abb8e3f1c1dd4df15a4babe8e530fce7c3afab6e998db177c61e433fca20bafb8849b73795a961977e4a8e23fd9580f2aec80c8fc868e063e1275f2
|
7
|
+
data.tar.gz: d1c9e97f317478f2d2f2b4d63f3f5606f5d5eb9c16548d9404bd62c984c40d4bae4db6e544ac73917a8795a82f72e1625825741f5b502650f05979e406a9b3b6
|
data/lib/prefixed_ids.rb
CHANGED
@@ -8,9 +8,7 @@ module PrefixedIds
|
|
8
8
|
|
9
9
|
autoload :PrefixId, "prefixed_ids/prefix_id"
|
10
10
|
|
11
|
-
|
12
|
-
DELIMITER = "_"
|
13
|
-
|
11
|
+
mattr_accessor :delimiter, default: "_"
|
14
12
|
mattr_accessor :alphabet, default: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
15
13
|
mattr_accessor :minimum_length, default: 24
|
16
14
|
|
@@ -24,8 +22,8 @@ module PrefixedIds
|
|
24
22
|
end
|
25
23
|
|
26
24
|
# Splits a prefixed ID into its prefix and ID
|
27
|
-
def self.split_id(prefix_id)
|
28
|
-
prefix, _, id = prefix_id.to_s.rpartition(
|
25
|
+
def self.split_id(prefix_id, delimiter = PrefixedIds.delimiter)
|
26
|
+
prefix, _, id = prefix_id.to_s.rpartition(delimiter)
|
29
27
|
[prefix, id]
|
30
28
|
end
|
31
29
|
|
@@ -74,7 +72,7 @@ module PrefixedIds
|
|
74
72
|
|
75
73
|
class_methods do
|
76
74
|
def find(*ids)
|
77
|
-
super(*ids.map { |id| _prefix_id.decode(id) })
|
75
|
+
super(*ids.map { |id| _prefix_id.decode(id, fallback: true) })
|
78
76
|
end
|
79
77
|
end
|
80
78
|
end
|
@@ -1,24 +1,33 @@
|
|
1
1
|
module PrefixedIds
|
2
2
|
class PrefixId
|
3
|
-
attr_reader :hashids, :
|
3
|
+
attr_reader :hashids, :prefix
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
TOKEN = 123
|
6
|
+
|
7
|
+
def initialize(model, prefix, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
|
8
8
|
@prefix = prefix.to_s
|
9
|
-
@
|
9
|
+
@delimiter = delimiter.to_s
|
10
|
+
@hashids = Hashids.new(model.table_name, minimum_length, alphabet)
|
10
11
|
end
|
11
12
|
|
12
13
|
def encode(id)
|
13
|
-
prefix +
|
14
|
+
@prefix + @delimiter + @hashids.encode(TOKEN, id)
|
14
15
|
end
|
15
16
|
|
16
17
|
# decode returns an array
|
17
18
|
def decode(id, fallback: false)
|
18
19
|
fallback_value = fallback ? id : nil
|
19
|
-
_, id_without_prefix = PrefixedIds.split_id(id)
|
20
|
-
|
21
|
-
|
20
|
+
_, id_without_prefix = PrefixedIds.split_id(id, @delimiter)
|
21
|
+
decoded_hashid = @hashids.decode(id_without_prefix)
|
22
|
+
return fallback_value unless valid?(decoded_hashid)
|
23
|
+
|
24
|
+
decoded_hashid.last || fallback_value
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def valid?(decoded_hashid)
|
30
|
+
decoded_hashid.size == 2 && decoded_hashid.first == TOKEN
|
22
31
|
end
|
23
32
|
end
|
24
33
|
end
|
data/lib/prefixed_ids/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prefixed_ids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|