prefixed_ids 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3bad6535e46cad00a977806102164ad2f8d79dae1f9d24beb50c12888829c5d6
4
- data.tar.gz: a50ee9b602318c9a412887ddfd28274848eb5cdd2e5465e40ef7357bb11b2bd1
3
+ metadata.gz: e542fdd356a7d0a1ccc8ad8ca90cc2d218dd4d22e01b2cd82c07ab9e36c52141
4
+ data.tar.gz: 9c788fc9bf605656ddcfc1c3c3c854eb9fbef1068b1e9b963b6dfa4f6bb72cbe
5
5
  SHA512:
6
- metadata.gz: 6b0f42366a454e83682535538d072ae2bd1bf166386dcdfe767d75f40046cdfa466c0b763b86d6a1e5475fcff9c5b42970a5e1f5ae876ba56b1d55ca034713b5
7
- data.tar.gz: 66d904378b4db97609cdd8db76c23fe765a30699c8e4f65b0cb85c656eaff486ab3efa05f74908e7694b7840000682c58e8bd1a96ff6da4437b1f6566c29ada7
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
- TOKEN = 123
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(DELIMITER)
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, :model, :prefix
3
+ attr_reader :hashids, :prefix
4
4
 
5
- def initialize(model, prefix, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, **options)
6
- @alphabet = alphabet
7
- @model = model
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
- @hashids = Hashids.new(model.table_name, minimum_length)
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 + "_" + @hashids.encode(TOKEN, id)
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
- decoded_id = @hashids.decode(id_without_prefix).last
21
- decoded_id || fallback_value
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
@@ -1,3 +1,3 @@
1
1
  module PrefixedIds
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
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.0
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-02-22 00:00:00.000000000 Z
11
+ date: 2021-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails