acts_as_identifier 0.2.0 → 1.1.2
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/README.md +26 -17
- data/lib/acts_as_identifier.rb +36 -23
- data/lib/acts_as_identifier/version.rb +1 -1
- metadata +21 -8
- data/lib/acts_as_identifier/railtie.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d0ef5e8642b9ffa1aaf5dfa6e73a6d3b76885d08c90c8d6b74799c650b9ad86
|
4
|
+
data.tar.gz: 6637b7954d14b63f122ba48ba168fcdd6c8963e85188336d113b3d9c94c34a54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53908ce8a218741b7a2466f77e99b6fb80ef22460d4d920f43585038d92913a573dc9142c42d3e227e93c8771d869d0467b4a80cdb33ed207e9c0dee18e32a2b
|
7
|
+
data.tar.gz: 21eb32be02eb86a40a84a00691800f48cb80831904afa6606ede41c1c985cb03cee67b134d083611b7b557ada0ea7697dcc3f5b0e1ab6674e15d08792b860197
|
data/README.md
CHANGED
@@ -2,33 +2,35 @@
|
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/acts_as_identifier)
|
4
4
|
|
5
|
-
Automatically generate unique
|
5
|
+
Automatically generate unique fixed-length string for one or more columns of ActiveRecord based on sequence column
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
|
-
> `ActsAsIdentifier` only generate identifier `
|
9
|
+
> `ActsAsIdentifier` only generate identifier `before_commit`
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
class Account < ActiveRecord::Base
|
13
|
+
include ActsAsIdentifier
|
13
14
|
#
|
14
|
-
#
|
15
|
+
# == default options
|
16
|
+
#
|
17
|
+
# attr: :identifier,
|
18
|
+
# seed: 1,
|
19
|
+
# length: 6,
|
20
|
+
# prefix: nil,
|
21
|
+
# id_column: :id,
|
22
|
+
# chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
15
23
|
#
|
16
|
-
|
17
|
-
# with default options:
|
18
|
-
# column: :identifier
|
19
|
-
# length: 6
|
20
|
-
# case sensitive: true
|
21
|
-
# max_loop: 100
|
22
|
-
# scope: []
|
23
24
|
acts_as_identifier
|
24
|
-
|
25
|
-
|
26
|
-
acts_as_identifier :slug, length: 8, case_sensitive: false, max_loop: 1000, scope: [:tenant_id]
|
25
|
+
# or customize options:
|
26
|
+
acts_as_identifier :slug, length: 6, prefix: 's-'
|
27
27
|
end
|
28
|
-
# => [Account(id: integer, name: string, tenant_id: string,
|
28
|
+
# => [Account(id: integer, name: string, tenant_id: string, slug: string)]
|
29
29
|
|
30
30
|
Account.create
|
31
|
-
# => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil,
|
31
|
+
# => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil, slug: "s-EPaPaP">
|
32
|
+
Account.create
|
33
|
+
# => #<Account:0x00007fcdb90830c0 id: 2, name: nil, tenant_id: nil, slug: "s-HSo0u4">
|
32
34
|
|
33
35
|
```
|
34
36
|
|
@@ -38,8 +40,15 @@ Account.create
|
|
38
40
|
bundle add acts_as_identifier
|
39
41
|
```
|
40
42
|
|
43
|
+
## Requirements
|
44
|
+
|
45
|
+
Use gem [`Xencoder`](https://github.com/xiaohui-zhangxh/xencoder/) to encode sequence number to fixed-length string.
|
46
|
+
|
41
47
|
## Contributing
|
42
48
|
Contribution directions go here.
|
43
49
|
|
44
|
-
##
|
45
|
-
|
50
|
+
## Testing
|
51
|
+
|
52
|
+
```shell
|
53
|
+
bundle exec rspec
|
54
|
+
```
|
data/lib/acts_as_identifier.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'securerandom'
|
4
3
|
require 'acts_as_identifier/version'
|
4
|
+
require 'xencoder'
|
5
5
|
|
6
6
|
module ActsAsIdentifier
|
7
|
-
LoopTooManyTimesError = Class.new(StandardError)
|
8
7
|
|
9
8
|
def self.included(base)
|
10
9
|
base.extend ClassMethods
|
@@ -12,32 +11,46 @@ module ActsAsIdentifier
|
|
12
11
|
|
13
12
|
module ClassMethods
|
14
13
|
#
|
15
|
-
# == Automatically generate unique
|
14
|
+
# == Automatically generate unique string based on id
|
16
15
|
#
|
17
16
|
# @param attr [String, Symbol] column name, default: :identifier
|
17
|
+
# @param seed [Integer] Random seed, default: 1
|
18
18
|
# @param length [Integer] length of identifier, default: 6
|
19
|
-
# @
|
20
|
-
# @
|
21
|
-
# @
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
19
|
+
# @params prefix [String, Symbol] add prefix to value, default: nil
|
20
|
+
# @params id_column [String, Symbol] column name of id, default: :id
|
21
|
+
# @params chars [String] chars for generating identifier
|
22
|
+
def acts_as_identifier(attr = :identifier,
|
23
|
+
seed: 1,
|
24
|
+
length: 6,
|
25
|
+
prefix: nil,
|
26
|
+
id_column: :id,
|
27
|
+
chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
28
|
+
define_singleton_method "#{attr}_encoder" do
|
29
|
+
vname = "@#{attr}_encoder"
|
30
|
+
return instance_variable_get(vname) if instance_variable_defined?(vname)
|
31
|
+
|
32
|
+
instance_variable_set(vname, Xencoder.new(chars, length: length, seed: seed))
|
33
|
+
end
|
34
|
+
|
35
|
+
define_singleton_method "decode_#{attr}" do |str|
|
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)
|
41
|
+
end
|
42
|
+
|
43
|
+
define_singleton_method "encode_#{attr}" do |num|
|
44
|
+
"#{prefix}#{send("#{attr}_encoder").encode(num)}"
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method "update_#{attr}_before_commit" do
|
48
|
+
if previous_changes.key?(id_column.to_s) && !destroyed?
|
49
|
+
update_column attr, self.class.send("encode_#{attr}", send(id_column))
|
37
50
|
end
|
38
51
|
end
|
52
|
+
|
53
|
+
before_commit :"update_#{attr}_before_commit"
|
39
54
|
end
|
40
55
|
end
|
41
56
|
end
|
42
|
-
|
43
|
-
require 'acts_as_identifier/railtie' if defined?(Rails)
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_identifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.2
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: xencoder
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.0
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rails
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,7 +52,7 @@ dependencies:
|
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
|
-
description:
|
55
|
+
description:
|
42
56
|
email:
|
43
57
|
- xiaohui@tanmer.com
|
44
58
|
executables: []
|
@@ -48,13 +62,12 @@ files:
|
|
48
62
|
- MIT-LICENSE
|
49
63
|
- README.md
|
50
64
|
- lib/acts_as_identifier.rb
|
51
|
-
- lib/acts_as_identifier/railtie.rb
|
52
65
|
- lib/acts_as_identifier/version.rb
|
53
66
|
homepage: https://github.com/xiaohui-zhangxh/acts_as_identifier
|
54
67
|
licenses:
|
55
68
|
- MIT
|
56
69
|
metadata: {}
|
57
|
-
post_install_message:
|
70
|
+
post_install_message:
|
58
71
|
rdoc_options: []
|
59
72
|
require_paths:
|
60
73
|
- lib
|
@@ -69,8 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
82
|
- !ruby/object:Gem::Version
|
70
83
|
version: '0'
|
71
84
|
requirements: []
|
72
|
-
rubygems_version: 3.0.
|
73
|
-
signing_key:
|
85
|
+
rubygems_version: 3.0.9
|
86
|
+
signing_key:
|
74
87
|
specification_version: 4
|
75
88
|
summary: Auto-generate unique identifier value for Active Record
|
76
89
|
test_files: []
|