aegis_support 0.0.2 → 0.0.3

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: 658fe7dd0b5571c490a5ca53060712b028da1d77cd67daeebfbbd71b6ea2da65
4
- data.tar.gz: b3f6a4c52809eb5b1b03a05821af9f1eeaf0535599b02976ef1616285151235f
3
+ metadata.gz: 8c24d3a8cb0e54f8593a3e16b3354602fea04602286271804dbf251dca661c0f
4
+ data.tar.gz: d48a250d22d2cd520406069b764ed409bfb3399de0445e5582478f2d62e8ca70
5
5
  SHA512:
6
- metadata.gz: 6546f8565b05cec91f27e43c5f98d4569a34e00efdd0359fa332b134e58a861633e3238022efcf92724de00f3ff329211291248576b2cbbbe4ba8764056d66de
7
- data.tar.gz: 1cb35f1f20f90590e7cc392a97c2ff6062881c080b5eac56861205d021572802c7f74065c0fdd671d0a1970986e87487d783c5c12c291ac1704f98d7337f624e
6
+ metadata.gz: 7b274e4cc77fef9c30603af94b5c9c26d22a6b800ac2c6f97fc059b61a476b62079acb9bbe1d4da79850a304c5c0c9b7e3a15297c9db43a55cfc1fb22b65d514
7
+ data.tar.gz: 35687085418c783aa5109adce4853d31bb8fe8d31206c571c932ae87621e3b5f8a76b7e0c32777c05ab90324f99ba14dae10ba802e1f391a479d68237bef63fb
data/README.md CHANGED
@@ -1,8 +1,36 @@
1
- # Aeg*i*sSupport
2
- Short description and motivation.
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
- How to use my plugin.
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
- # use "4.gigabytes ** 2" just want to generate a random number that maximum length is 20
17
- secure_number = SecureRandom.random_number(4.gigabytes ** 2).to_s.rjust(20, "0")
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
@@ -1,3 +1,3 @@
1
1
  module AegisSupport
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aegis_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian