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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50941b62619d0fa3829df18f02e97e46d02e7b35f052db3c64d9c7b821699826
4
- data.tar.gz: 728fbeb1a33e0dd5f5f734f5a1e18821320df7118e79717b5bc543c9fceacec4
3
+ metadata.gz: 1d0ef5e8642b9ffa1aaf5dfa6e73a6d3b76885d08c90c8d6b74799c650b9ad86
4
+ data.tar.gz: 6637b7954d14b63f122ba48ba168fcdd6c8963e85188336d113b3d9c94c34a54
5
5
  SHA512:
6
- metadata.gz: d323726b48026b4bb396a8837d47f1c45b6f3fded3972c3c7a51e7ca3bccbfdcfa11f33ba895dd081b21a2c3b79327f9348ad6987a0bff2695c32b868c6c1fd2
7
- data.tar.gz: 6fba6c6ade83590f4c91909298da3133d01665014c187d12324d129f8d03a992cfaab01cab7693d869c124ec233e8e485de17ca7a3175e15b2c7aed6be9abbfe
6
+ metadata.gz: 53908ce8a218741b7a2466f77e99b6fb80ef22460d4d920f43585038d92913a573dc9142c42d3e227e93c8771d869d0467b4a80cdb33ed207e9c0dee18e32a2b
7
+ data.tar.gz: 21eb32be02eb86a40a84a00691800f48cb80831904afa6606ede41c1c985cb03cee67b134d083611b7b557ada0ea7697dcc3f5b0e1ab6674e15d08792b860197
data/README.md CHANGED
@@ -2,33 +2,35 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/acts_as_identifier.svg)](https://badge.fury.io/rb/acts_as_identifier)
4
4
 
5
- Automatically generate unique secure random string for one or more columns of ActiveRecord.
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 `before create`
9
+ > `ActsAsIdentifier` only generate identifier `before_commit`
10
10
 
11
11
  ```ruby
12
12
  class Account < ActiveRecord::Base
13
+ include ActsAsIdentifier
13
14
  #
14
- # Note: without Rails, should include ActsAsIdentifier
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
- # extra column
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, identifier: string, slug: 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, identifier: "PWbYHd", slug: "5fabb1e7">
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
- ## License
45
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
50
+ ## Testing
51
+
52
+ ```shell
53
+ bundle exec rspec
54
+ ```
@@ -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 secure random string
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
- # @param case_sensitive [Boolean] Case-sensitive? default: true
20
- # @param max_loop [Integer] max loop count to generate unique identifier, in case of running endless loop, default: 100
21
- # @param scope [Array<Symbol,String>] scope of identifier, default: []
22
- # @params prefix [String, Symbol] add prefix to value, default: ''
23
- def acts_as_identifier(attr = nil, length: 6, case_sensitive: true, max_loop: 100, scope: [], prefix: '')
24
- attr ||= :identifier
25
- scope = Array(scope)
26
- method = case_sensitive ? :alphanumeric : :hex
27
- length /= 2 if method == :hex
28
-
29
- before_create do
30
- query = self.class.unscoped.where(scope.inject({}) { |memo, i| memo.merge(i => send(i)) })
31
- n = 0
32
- loop do
33
- n += 1
34
- value = send("#{attr}=", "#{prefix}#{SecureRandom.send(method, length)}")
35
- break unless query.where(attr => value).exists?
36
- raise LoopTooManyTimesError if n > max_loop
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsIdentifier
4
- VERSION = '0.2.0'
4
+ VERSION = '1.1.2'
5
5
  end
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: 0.2.0
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: 2019-12-12 00:00:00.000000000 Z
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.6
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: []
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActsAsIdentifier
4
- class Railtie < ::Rails::Railtie
5
- initializer 'acts_as_identifier' do
6
- ActiveSupport.on_load(:active_record) do
7
- include ActsAsIdentifier
8
- end
9
- end
10
- end
11
- end