acts_as_identifier 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 56a684b60d5b16c517e29049493feee1bed693c74b5de33fd18100454fa53547
4
+ data.tar.gz: 81c9a2c9b6a3c7cc7fd87ef3123f595cab818783018e22b9423ed6aee085fd2c
5
+ SHA512:
6
+ metadata.gz: 178d1de2dd01dce8afdeb933cc9bec7a18b38132e8ead74dc1271d5d33eb6e7ee37f72c4bf0c8142fc9d7e7b4c1f2bd5136cd2f9fbbd74ada4be9d7e854573af
7
+ data.tar.gz: c8c1d26f4ec778832a14dd1bae837f6c90786098302dccf0ed3938e82930d54aab4f26d59cf17b1815a03242ebeba3f4c8174dc01913ddc2cd8f4d3c8263a8d8
@@ -0,0 +1,20 @@
1
+ Copyright 2019
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # ActsAsIdentifier
2
+
3
+ Automatically generate unique secure random string for one or more columns of ActiveRecord.
4
+
5
+ ## Usage
6
+
7
+ > `ActsAsIdentifier` only generate identifier `before create`
8
+
9
+ ```ruby
10
+ class Account < ActiveRecord::Base
11
+ #
12
+ # Note: without Rails, should include ActsAsIdentifier
13
+ #
14
+
15
+ # with default options:
16
+ # column: :identifier
17
+ # length: 6
18
+ # case sensitive: true
19
+ # max_loop: 100
20
+ # scope: []
21
+ acts_as_identifier
22
+
23
+ # extra column
24
+ acts_as_identifier :slug, length: 8, case_sensitive: false, max_loop: 1000, scope: [:tenant_id]
25
+ end
26
+ # => [Account(id: integer, name: string, tenant_id: string, identifier: string, slug: string)]
27
+
28
+ Account.create
29
+ # => #<Account:0x00007fcdb90830c0 id: 1, name: nil, tenant_id: nil, identifier: "PWbYHd", slug: "5fabb1e7">
30
+
31
+ ```
32
+
33
+ ## Installation
34
+
35
+ ```ruby
36
+ bundle add acts_as_identifier
37
+ ```
38
+
39
+ ## Contributing
40
+ Contribution directions go here.
41
+
42
+ ## License
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+ require 'acts_as_identifier/version'
5
+
6
+ module ActsAsIdentifier
7
+ LoopTooManyTimesError = Class.new(StandardError)
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ #
15
+ # == Automatically generate unique secure random string
16
+ #
17
+ # @param attr [String, Symbol] column name, default: :identifier
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
+ def acts_as_identifier(attr = nil, length: 6, case_sensitive: true, max_loop: 100, scope: [])
23
+ attr ||= :identifier
24
+ scope = Array(scope)
25
+ method = case_sensitive ? :alphanumeric : :hex
26
+ length /= 2 if method == :hex
27
+
28
+ before_create do
29
+ query = self.class.unscoped.where(scope.inject({}) { |memo, i| memo.merge(i => send(i)) })
30
+ n = 0
31
+ loop do
32
+ n += 1
33
+ value = send("#{attr}=", SecureRandom.send(method, length))
34
+ break unless query.where(attr => value).exists?
35
+ raise LoopTooManyTimesError if n > max_loop
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ require 'acts_as_identifier/railtie' if defined?(Rails)
@@ -0,0 +1,11 @@
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActsAsIdentifier
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_identifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xiaohui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - xiaohui@tanmer.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - lib/acts_as_identifier.rb
51
+ - lib/acts_as_identifier/railtie.rb
52
+ - lib/acts_as_identifier/version.rb
53
+ homepage: https://github.com/xiaohui-zhangxh/acts_as_identifier
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.0.6
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Auto-generate unique identifier value for Active Record
76
+ test_files: []