acts_as_identifier 1.0.0 → 1.1.0
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 +15 -12
- data/lib/acts_as_identifier.rb +10 -16
- data/lib/acts_as_identifier/version.rb +1 -1
- metadata +16 -5
- data/lib/acts_as_identifier/encoder.rb +0 -36
- data/lib/acts_as_identifier/railtie.rb +0 -11
- data/lib/acts_as_identifier/xbase_integer.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e24cb96573ceb1e6439218ab0791c25fca1ff4de0e5320019039a2f1e56e0ce
|
4
|
+
data.tar.gz: 1958d5829d519f497c3c215183f7d071d7e68ccfb66521d2a14db3da0a26e3ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed7ff77501adb90ca3b030cda300d974cc92c577863589316a3e4a34f0d14dbf84c2f57e167af13f816ef0ddb9f946b754e3a6d5e3e454f4f42e3ad85180b8d7
|
7
|
+
data.tar.gz: fab6c448257751fd41b08af088f59753d3154ef64bd506604e23881122221bb9338a824bfc0b4f19b697fb2e0762b03d313187dbf56e1d6141db080b87eaf2d7
|
data/README.md
CHANGED
@@ -2,23 +2,24 @@
|
|
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
|
15
16
|
#
|
16
17
|
# def acts_as_identifier(attr = :identifier,
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
18
|
+
# seed: 1,
|
19
|
+
# length: 6,
|
20
|
+
# prefix: '',
|
21
|
+
# id_column: :id,
|
22
|
+
# chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
22
23
|
#
|
23
24
|
acts_as_identifier
|
24
25
|
# or customize options:
|
@@ -27,9 +28,9 @@ end
|
|
27
28
|
# => [Account(id: integer, name: string, tenant_id: string, slug: string)]
|
28
29
|
|
29
30
|
Account.create
|
30
|
-
# => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil, slug: "s-
|
31
|
+
# => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil, slug: "s-EPaPaP">
|
31
32
|
Account.create
|
32
|
-
# => #<Account:0x00007fcdb90830c0 id: 2, name: nil, tenant_id: nil, slug: "s-
|
33
|
+
# => #<Account:0x00007fcdb90830c0 id: 2, name: nil, tenant_id: nil, slug: "s-HSo0u4">
|
33
34
|
|
34
35
|
```
|
35
36
|
|
@@ -39,6 +40,10 @@ Account.create
|
|
39
40
|
bundle add acts_as_identifier
|
40
41
|
```
|
41
42
|
|
43
|
+
## Requirements
|
44
|
+
|
45
|
+
Use gem [`Xencoder`](https://github.com/xiaohui-zhangxh/xencoder/) to encode sequence number to fixed-length string.
|
46
|
+
|
42
47
|
## Contributing
|
43
48
|
Contribution directions go here.
|
44
49
|
|
@@ -46,6 +51,4 @@ Contribution directions go here.
|
|
46
51
|
|
47
52
|
```shell
|
48
53
|
bundle exec rspec
|
49
|
-
# or test specific range
|
50
|
-
EXTRA_TEST=100000,1000000 bundle exec rspec
|
51
54
|
```
|
data/lib/acts_as_identifier.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'securerandom'
|
4
3
|
require 'acts_as_identifier/version'
|
5
|
-
require '
|
6
|
-
require 'acts_as_identifier/encoder'
|
4
|
+
require 'xencoder'
|
7
5
|
|
8
6
|
module ActsAsIdentifier
|
9
|
-
LoopTooManyTimesError = Class.new(StandardError)
|
10
7
|
|
11
8
|
def self.included(base)
|
12
9
|
base.extend ClassMethods
|
@@ -23,25 +20,24 @@ module ActsAsIdentifier
|
|
23
20
|
# @params chars [Array<String>] chars
|
24
21
|
# @params mappings [Array<String>] mappings must have the same characters as chars
|
25
22
|
def acts_as_identifier(attr = :identifier,
|
23
|
+
seed: 1,
|
26
24
|
length: 6,
|
27
25
|
prefix: '',
|
28
26
|
id_column: :id,
|
29
|
-
chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
unless (chars - mappings).empty? && (mappings - chars).empty?
|
34
|
-
raise 'chars and mappings must have the same characters'
|
35
|
-
end
|
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)
|
36
31
|
|
37
|
-
|
32
|
+
instance_variable_set(vname, Xencoder.new(chars, length: length, seed: seed))
|
33
|
+
end
|
38
34
|
|
39
35
|
define_singleton_method "decode_#{attr}" do |str|
|
40
|
-
|
36
|
+
send("#{attr}_encoder").decode(str[prefix.length..-1])
|
41
37
|
end
|
42
38
|
|
43
39
|
define_singleton_method "encode_#{attr}" do |num|
|
44
|
-
"#{prefix}#{
|
40
|
+
"#{prefix}#{send("#{attr}_encoder").encode(num)}"
|
45
41
|
end
|
46
42
|
|
47
43
|
before_commit do |record|
|
@@ -52,5 +48,3 @@ module ActsAsIdentifier
|
|
52
48
|
end
|
53
49
|
end
|
54
50
|
end
|
55
|
-
|
56
|
-
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: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xiaohui
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-27 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
|
@@ -48,10 +62,7 @@ files:
|
|
48
62
|
- MIT-LICENSE
|
49
63
|
- README.md
|
50
64
|
- lib/acts_as_identifier.rb
|
51
|
-
- lib/acts_as_identifier/encoder.rb
|
52
|
-
- lib/acts_as_identifier/railtie.rb
|
53
65
|
- lib/acts_as_identifier/version.rb
|
54
|
-
- lib/acts_as_identifier/xbase_integer.rb
|
55
66
|
homepage: https://github.com/xiaohui-zhangxh/acts_as_identifier
|
56
67
|
licenses:
|
57
68
|
- MIT
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module ActsAsIdentifier
|
2
|
-
class Encoder
|
3
|
-
attr_reader :max, :base, :chars, :mappings, :length
|
4
|
-
|
5
|
-
def initialize(chars:, mappings:, length:)
|
6
|
-
@chars = chars.dup
|
7
|
-
@mappings = mappings.dup
|
8
|
-
@length = length
|
9
|
-
@xbase_integer = XbaseInteger.new(chars)
|
10
|
-
@base = @xbase_integer.to_i("#{chars[1]}#{chars[0] * (length - 1)}")
|
11
|
-
@max = @xbase_integer.to_i(chars[-1] * length) - @base
|
12
|
-
end
|
13
|
-
|
14
|
-
def encode(num)
|
15
|
-
str = @xbase_integer.to_x(num + base)
|
16
|
-
(str.length - 1).downto(1).each do |i|
|
17
|
-
idx = @mappings.index(str[i])
|
18
|
-
idx2 = (@mappings.index(str[i - 1]) + idx) % @mappings.size
|
19
|
-
str[i] = @chars.at(idx)
|
20
|
-
str[i - 1] = @chars.at(idx2)
|
21
|
-
end
|
22
|
-
str
|
23
|
-
end
|
24
|
-
|
25
|
-
def decode(str)
|
26
|
-
str = str.dup
|
27
|
-
0.upto(str.length - 2).each do |i|
|
28
|
-
idx = @chars.index(str[i + 1])
|
29
|
-
idx2 = (@chars.index(str[i]) - idx + @chars.size) % @chars.size
|
30
|
-
str[i] = @mappings.at(idx2)
|
31
|
-
str[i + 1] = @mappings.at(idx)
|
32
|
-
end
|
33
|
-
@xbase_integer.to_i(str) - base
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module ActsAsIdentifier
|
2
|
-
class XbaseInteger
|
3
|
-
attr_reader :chars
|
4
|
-
def initialize(chars)
|
5
|
-
@chars = chars.dup
|
6
|
-
end
|
7
|
-
|
8
|
-
def to_i(str)
|
9
|
-
sum = 0
|
10
|
-
str.chars.reverse.each_with_index do |char, i|
|
11
|
-
sum += chars.index(char) * chars.size**i
|
12
|
-
end
|
13
|
-
sum
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_x(num)
|
17
|
-
str = chars.at(num % chars.size)
|
18
|
-
num /= chars.size
|
19
|
-
str = "#{to_x(num)}#{str}" if num > 0
|
20
|
-
str
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|