aegis_support 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -3
- data/lib/aegis_support/secure_number.rb +3 -2
- data/lib/aegis_support/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c24d3a8cb0e54f8593a3e16b3354602fea04602286271804dbf251dca661c0f
|
4
|
+
data.tar.gz: d48a250d22d2cd520406069b764ed409bfb3399de0445e5582478f2d62e8ca70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b274e4cc77fef9c30603af94b5c9c26d22a6b800ac2c6f97fc059b61a476b62079acb9bbe1d4da79850a304c5c0c9b7e3a15297c9db43a55cfc1fb22b65d514
|
7
|
+
data.tar.gz: 35687085418c783aa5109adce4853d31bb8fe8d31206c571c932ae87621e3b5f8a76b7e0c32777c05ab90324f99ba14dae10ba802e1f391a479d68237bef63fb
|
data/README.md
CHANGED
@@ -1,8 +1,36 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# AegisSupport
|
2
|
+
Aegis Support is a collection of utility classes and library extensions that were found useful for the Rails framework
|
3
3
|
|
4
4
|
## Usage
|
5
|
-
|
5
|
+
|
6
|
+
### AegisSupport::SecureNumber
|
7
|
+
A module with `has_secure_number` method for generate unique numbers,
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# Schema: Order(order_no:string)
|
11
|
+
class Order < ActiveRecord::Base
|
12
|
+
include AegisSupport::SecureNumber
|
13
|
+
|
14
|
+
# replace `order_no` with what column your want
|
15
|
+
has_secure_number :order_no
|
16
|
+
end
|
17
|
+
|
18
|
+
order = Order.new
|
19
|
+
order.save
|
20
|
+
|
21
|
+
order.order_no # 15103771760966048900
|
22
|
+
```
|
23
|
+
|
24
|
+
The generate algorithm is:
|
25
|
+
```ruby
|
26
|
+
maximum_number = 99999_99999_99999_99999 # 20 characters long
|
27
|
+
secure_number = SecureRandom.random_number(maximum_number).to_s.rjust(20, "0")
|
28
|
+
```
|
29
|
+
|
30
|
+
Thus it will generate a random numeric string of 20 characters long, so collisions are highly unlikely.
|
31
|
+
like the `has_secure_token` in ActiveRecord, it's still possible generate a race condition in the database
|
32
|
+
in the same way that `validates_uniqueness_of` can. You're encouraged to add a unique index in the database
|
33
|
+
to deal with this even more unlikely scenario
|
6
34
|
|
7
35
|
## Installation
|
8
36
|
Add this line to your application's Gemfile:
|
@@ -13,8 +13,9 @@ module AegisSupport
|
|
13
13
|
|
14
14
|
def generate_secure_number(attribute)
|
15
15
|
10.times do |i|
|
16
|
-
#
|
17
|
-
|
16
|
+
# generate random number of 20 characters long
|
17
|
+
maximum_number = 99999_99999_99999_99999
|
18
|
+
secure_number = SecureRandom.random_number(maximum_number).to_s.rjust(20, "0")
|
18
19
|
|
19
20
|
if exists?(attribute => secure_number)
|
20
21
|
raise "Couldn't generate a unique number in 10 attempts!" if i == 9
|