aegis_support 0.0.3 → 0.0.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 +22 -12
- data/lib/aegis_support/secure_number.rb +20 -8
- data/lib/aegis_support/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3983471972c6f92a26bb79c686b0c040cc3f2bab58e1460a36c8ec227e4a1d98
|
4
|
+
data.tar.gz: c9726be24cd6ecaa35e6ce3876d42c3c2786e7711eb5141a1d0afae92295e3ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9bfa5278a266c1926c381638068bd5e603ba9e92fcd14c613b6966747adfe6f90d32940627f71e648212ec7fa4ac62dff9afdcc84feae37d59911f29199c4221
|
7
|
+
data.tar.gz: 96f1c99ffaf2f0e5ac3abb0fbee5f2ad2eeb922e3b0ee3843dffb077a7a1de9aa4b01ab5dac08d4b25563da649b6c128a0892ab29cd846a5ac1238ca32bd8ca1
|
data/README.md
CHANGED
@@ -4,33 +4,43 @@ Aegis Support is a collection of utility classes and library extensions that wer
|
|
4
4
|
## Usage
|
5
5
|
|
6
6
|
### AegisSupport::SecureNumber
|
7
|
-
A module with `has_secure_number` method for generate unique numbers
|
7
|
+
A module with `has_secure_number` method for generate unique numbers
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
# Schema:
|
10
|
+
# Database Schema:
|
11
|
+
#
|
12
|
+
# Order(
|
13
|
+
# order_no: string,
|
14
|
+
# order_no_with_prefix: string,
|
15
|
+
# order_no_with_suffix: string,
|
16
|
+
# order_no_with_specific_length: string
|
17
|
+
# )
|
11
18
|
class Order < ActiveRecord::Base
|
12
19
|
include AegisSupport::SecureNumber
|
13
20
|
|
14
|
-
# replace
|
21
|
+
# replace above column with what your want
|
15
22
|
has_secure_number :order_no
|
23
|
+
has_secure_number :order_no_with_prefix, prefix: "PN_"
|
24
|
+
has_secure_number :order_no_with_suffix, prefix: "_SN"
|
25
|
+
has_secure_number :order_no_with_specific_length, length: 16
|
16
26
|
end
|
17
27
|
|
18
28
|
order = Order.new
|
19
29
|
order.save
|
20
30
|
|
21
31
|
order.order_no # 15103771760966048900
|
32
|
+
order.order_no_with_prefix # PN_93550490115056646751
|
33
|
+
order.order_no_with_suffix # 26931151941756843999_SN
|
34
|
+
order.order_no_with_specific_length # 9124679044244949
|
22
35
|
```
|
23
36
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
secure_number = SecureRandom.random_number(maximum_number).to_s.rjust(20, "0")
|
28
|
-
```
|
37
|
+
`SecureRandom.random_number` is used to generate the random numeric string. when the result's length is less than the
|
38
|
+
specific(default is 20), some leading zeros will be added to the number to make its width equal to the length. `prefix`
|
39
|
+
and `suffix` are not included in the calculation of length.
|
29
40
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
to deal with this even more unlikely scenario
|
41
|
+
when the length is long enough, The collisions are highly unlikely. like the `has_secure_token` in ActiveRecord,
|
42
|
+
it's still possible generate a race condition in the database in the same way that `validates_uniqueness_of` can.
|
43
|
+
You're encouraged to add a unique index in the database to deal with this even more unlikely scenario
|
34
44
|
|
35
45
|
## Installation
|
36
46
|
Add this line to your application's Gemfile:
|
@@ -1,21 +1,22 @@
|
|
1
|
-
|
2
1
|
module AegisSupport
|
3
2
|
module SecureNumber
|
4
3
|
extend ActiveSupport::Concern
|
5
4
|
|
6
5
|
module ClassMethods
|
7
|
-
|
6
|
+
DEFAULT_MAXIMUM_NUMBER_LENGTH = 20
|
7
|
+
|
8
|
+
def has_secure_number(attribute, **options)
|
8
9
|
require "securerandom"
|
9
10
|
|
10
|
-
define_method("regenerate_#{attribute}"){ update! attribute => self.class.generate_secure_number(attribute) }
|
11
|
-
before_create{ self.send("#{attribute}=", self.class.generate_secure_number(attribute)) }
|
11
|
+
define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_secure_number(attribute, **options) }
|
12
|
+
before_create { self.send("#{attribute}=", self.class.generate_secure_number(attribute, **options)) }
|
12
13
|
end
|
13
14
|
|
14
|
-
def generate_secure_number(attribute)
|
15
|
+
def generate_secure_number(attribute, **options)
|
16
|
+
options.symbolize_keys!
|
17
|
+
|
15
18
|
10.times do |i|
|
16
|
-
|
17
|
-
maximum_number = 99999_99999_99999_99999
|
18
|
-
secure_number = SecureRandom.random_number(maximum_number).to_s.rjust(20, "0")
|
19
|
+
secure_number = options[:prefix].to_s + random_string(options[:length]) + options[:suffix].to_s
|
19
20
|
|
20
21
|
if exists?(attribute => secure_number)
|
21
22
|
raise "Couldn't generate a unique number in 10 attempts!" if i == 9
|
@@ -24,6 +25,17 @@ module AegisSupport
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def random_string(length)
|
32
|
+
length = length.present? ? Integer(length) : DEFAULT_MAXIMUM_NUMBER_LENGTH
|
33
|
+
maximum_number = ("9" * length).to_i
|
34
|
+
|
35
|
+
SecureRandom.random_number(maximum_number)
|
36
|
+
.to_s
|
37
|
+
.rjust(length, "0")
|
38
|
+
end
|
27
39
|
end
|
28
40
|
end
|
29
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aegis_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|