acts_as_identifier 1.1.0 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -7
- data/lib/acts_as_identifier.rb +21 -9
- data/lib/acts_as_identifier/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7efb2fc3ad0b08e22b058358c990f15bf0a516c34c95824a8b197f1a4518e74b
|
4
|
+
data.tar.gz: 45eba2d5385ea34bdd6fa355001644ead93d65071f1bdc2499d980985c891b69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2b83dc95625f42cab1b5b3e10e125e22c70f5fcbd06c4dc94a63f99916d5a4a0da14390bcb93314d96a8b01995a351b67141fc56692e8fabe718f447a732d00
|
7
|
+
data.tar.gz: 5dd8b25d169fe579cc3a6e48d34fa82483e37e882a57b5a1069543beaceb91103a47605b8a8cd86db98d3b7a10fb88d10a29522a54fac8b9a689bd9123180a7e
|
data/README.md
CHANGED
@@ -14,12 +14,12 @@ class Account < ActiveRecord::Base
|
|
14
14
|
#
|
15
15
|
# == default options
|
16
16
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
17
|
+
# attr: :identifier,
|
18
|
+
# seed: 1,
|
19
|
+
# length: 6,
|
20
|
+
# prefix: nil,
|
21
|
+
# id_column: :id,
|
22
|
+
# chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
23
23
|
#
|
24
24
|
acts_as_identifier
|
25
25
|
# or customize options:
|
@@ -31,9 +31,16 @@ Account.create
|
|
31
31
|
# => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil, slug: "s-EPaPaP">
|
32
32
|
Account.create
|
33
33
|
# => #<Account:0x00007fcdb90830c0 id: 2, name: nil, tenant_id: nil, slug: "s-HSo0u4">
|
34
|
-
|
35
34
|
```
|
36
35
|
|
36
|
+
## Features
|
37
|
+
|
38
|
+
- *`ActiveRecord`*.find_by_decoded_*`identifier`*('s-EPaPaP') => Actually find by id, id is decoded from identifier, with this method you don't need to add extra index for *`identifier`*
|
39
|
+
- *`ActiveRecord`*.find_by_decoded_*`identifier`*!('s-EPaPaP') => equal to find_by_xx!
|
40
|
+
- *`ActiveRecord`*.*decode_`identifier`*('s-EPaPaP') => decode identifier to id
|
41
|
+
- *`ActiveRecord`*.*encode_`identifier`*(1) => encode id to identifier
|
42
|
+
- *`ActiveRecord`*.*`identifier`*_encoder => encoder instance, see [xencoder](https://github.com/xiaohui-zhangxh/xencoder)
|
43
|
+
|
37
44
|
## Installation
|
38
45
|
|
39
46
|
```ruby
|
data/lib/acts_as_identifier.rb
CHANGED
@@ -14,15 +14,15 @@ module ActsAsIdentifier
|
|
14
14
|
# == Automatically generate unique string based on id
|
15
15
|
#
|
16
16
|
# @param attr [String, Symbol] column name, default: :identifier
|
17
|
+
# @param seed [Integer] Random seed, default: 1
|
17
18
|
# @param length [Integer] length of identifier, default: 6
|
18
|
-
# @params prefix [String, Symbol] add prefix to value, default:
|
19
|
+
# @params prefix [String, Symbol] add prefix to value, default: nil
|
19
20
|
# @params id_column [String, Symbol] column name of id, default: :id
|
20
|
-
# @params chars [
|
21
|
-
# @params mappings [Array<String>] mappings must have the same characters as chars
|
21
|
+
# @params chars [String] chars for generating identifier
|
22
22
|
def acts_as_identifier(attr = :identifier,
|
23
23
|
seed: 1,
|
24
24
|
length: 6,
|
25
|
-
prefix:
|
25
|
+
prefix: nil,
|
26
26
|
id_column: :id,
|
27
27
|
chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
28
28
|
define_singleton_method "#{attr}_encoder" do
|
@@ -33,18 +33,30 @@ module ActsAsIdentifier
|
|
33
33
|
end
|
34
34
|
|
35
35
|
define_singleton_method "decode_#{attr}" do |str|
|
36
|
-
|
36
|
+
if prefix
|
37
|
+
return nil unless str.to_s.start_with?(prefix)
|
38
|
+
str = str[prefix.length..-1]
|
39
|
+
end
|
40
|
+
str && send("#{attr}_encoder").decode(str)
|
37
41
|
end
|
38
42
|
|
39
43
|
define_singleton_method "encode_#{attr}" do |num|
|
40
44
|
"#{prefix}#{send("#{attr}_encoder").encode(num)}"
|
41
45
|
end
|
42
46
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
define_singleton_method "find_by_decoded_#{attr}" do |str|
|
48
|
+
find_by(id_column => public_send("decode_#{attr}", str))
|
49
|
+
end
|
50
|
+
|
51
|
+
define_singleton_method "find_by_decoded_#{attr}!" do |str|
|
52
|
+
find_by!(id_column => public_send("decode_#{attr}", str))
|
47
53
|
end
|
54
|
+
|
55
|
+
define_method "acts_as_identifier__update_#{attr}" do
|
56
|
+
update_column attr, self.class.send("encode_#{attr}", send(id_column))
|
57
|
+
end
|
58
|
+
|
59
|
+
before_commit :"acts_as_identifier__update_#{attr}", if: -> { previous_changes.key?(id_column.to_s) && !destroyed? }
|
48
60
|
end
|
49
61
|
end
|
50
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_identifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xiaohui
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: xencoder
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
55
|
+
description:
|
56
56
|
email:
|
57
57
|
- xiaohui@tanmer.com
|
58
58
|
executables: []
|
@@ -67,7 +67,7 @@ homepage: https://github.com/xiaohui-zhangxh/acts_as_identifier
|
|
67
67
|
licenses:
|
68
68
|
- MIT
|
69
69
|
metadata: {}
|
70
|
-
post_install_message:
|
70
|
+
post_install_message:
|
71
71
|
rdoc_options: []
|
72
72
|
require_paths:
|
73
73
|
- lib
|
@@ -82,8 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
requirements: []
|
85
|
-
rubygems_version: 3.0.
|
86
|
-
signing_key:
|
85
|
+
rubygems_version: 3.0.9
|
86
|
+
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: Auto-generate unique identifier value for Active Record
|
89
89
|
test_files: []
|