aegis_support 0.0.1 → 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 +41 -3
- data/lib/aegis_support/secure_number.rb +41 -0
- data/lib/aegis_support/version.rb +1 -1
- data/lib/aegis_support.rb +1 -0
- metadata +17 -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
@@ -1,8 +1,46 @@
|
|
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
|
+
# 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
|
+
# )
|
18
|
+
class Order < ActiveRecord::Base
|
19
|
+
include AegisSupport::SecureNumber
|
20
|
+
|
21
|
+
# replace above column with what your want
|
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
|
26
|
+
end
|
27
|
+
|
28
|
+
order = Order.new
|
29
|
+
order.save
|
30
|
+
|
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
|
35
|
+
```
|
36
|
+
|
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.
|
40
|
+
|
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
|
6
44
|
|
7
45
|
## Installation
|
8
46
|
Add this line to your application's Gemfile:
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module AegisSupport
|
2
|
+
module SecureNumber
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
DEFAULT_MAXIMUM_NUMBER_LENGTH = 20
|
7
|
+
|
8
|
+
def has_secure_number(attribute, **options)
|
9
|
+
require "securerandom"
|
10
|
+
|
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)) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_secure_number(attribute, **options)
|
16
|
+
options.symbolize_keys!
|
17
|
+
|
18
|
+
10.times do |i|
|
19
|
+
secure_number = options[:prefix].to_s + random_string(options[:length]) + options[:suffix].to_s
|
20
|
+
|
21
|
+
if exists?(attribute => secure_number)
|
22
|
+
raise "Couldn't generate a unique number in 10 attempts!" if i == 9
|
23
|
+
else
|
24
|
+
break secure_number
|
25
|
+
end
|
26
|
+
end
|
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
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/aegis_support.rb
CHANGED
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
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 7.0.2.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mocha
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.13.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.13.0
|
27
41
|
description: A helpful collection of utitility for rails application
|
28
42
|
email:
|
29
43
|
- ianlynxk@gmail.com
|
@@ -36,6 +50,7 @@ files:
|
|
36
50
|
- Rakefile
|
37
51
|
- lib/aegis_support.rb
|
38
52
|
- lib/aegis_support/railtie.rb
|
53
|
+
- lib/aegis_support/secure_number.rb
|
39
54
|
- lib/aegis_support/version.rb
|
40
55
|
- lib/tasks/aegis_support_tasks.rake
|
41
56
|
homepage: https://github.com/otorain/aegis_support
|