code_generator 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.
- data/README.md +6 -5
- data/lib/code_generator/configure.rb +3 -0
- data/lib/code_generator/generator.rb +19 -10
- data/lib/code_generator/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -21,13 +21,13 @@ Or install it yourself as:
|
|
21
21
|
Really simple:
|
22
22
|
|
23
23
|
# creating random token
|
24
|
-
token = CodeGenerator.generate
|
24
|
+
token = CodeGenerator::Generate.generate
|
25
25
|
|
26
26
|
# specific length
|
27
|
-
token = CodeGenerator.generate(length: 10)
|
27
|
+
token = CodeGenerator::Generate.generate(length: 10)
|
28
28
|
|
29
29
|
# unique for particular field in a model
|
30
|
-
token = CodeGenerator.
|
30
|
+
token = CodeGenerator::Generate.generate(uniqueness: { model: :user, field: :confirmation_code })
|
31
31
|
|
32
32
|
## Configuration
|
33
33
|
|
@@ -35,10 +35,11 @@ You can configure the tokens to be generated. Simply create a new code_generator
|
|
35
35
|
|
36
36
|
CodeGenerator.configure do |code|
|
37
37
|
code.length = 6 # length of the tokens to be generated.
|
38
|
-
code.use_chars = :numeric # type of token to be generated.
|
38
|
+
code.use_chars = :numeric # type of token to be generated. (default: :alpha_numeric)
|
39
39
|
code.invalid_chars = [ 0, 1, 7 ] # do not use these characters.
|
40
40
|
code.valid_chars = [ 3, 4, 5, 6 ] # use only these characters (ignores :use_chars).
|
41
|
-
code.include_chars = [ "$", "%" ] # use these characters as well (
|
41
|
+
code.include_chars = [ "$", "%" ] # use these characters as well (does not ignore :use_chars).
|
42
|
+
code.repeat_chars = false # each character to be used once. (default: true)
|
42
43
|
end
|
43
44
|
|
44
45
|
You can use :alpha, :lower_alpha, :upper_alpha, :numeric, :lower_alpha_numeric, :upper_alpha_numeric, :alpha_numeric as various token types. Default is :alpha_numeric.
|
@@ -6,25 +6,34 @@ module CodeGenerator
|
|
6
6
|
|
7
7
|
class Generator
|
8
8
|
|
9
|
-
attr_reader :
|
9
|
+
attr_reader :model
|
10
10
|
attr_accessor :length, :uniqueness
|
11
11
|
|
12
12
|
def initialize(opts = {})
|
13
13
|
opts.symbolize_keys!
|
14
14
|
@length = opts.delete(:length) || CodeGenerator.length
|
15
15
|
@uniqueness = opts.delete(:uniqueness)
|
16
|
-
|
16
|
+
set_model_and_field! if uniqueness
|
17
17
|
end
|
18
18
|
|
19
19
|
def unique?
|
20
|
-
@
|
20
|
+
@model.send(:where, {@field_name => @code}).empty?
|
21
21
|
end
|
22
22
|
|
23
23
|
def random_string
|
24
|
-
chars = CodeGenerator.valid_characters
|
25
|
-
|
24
|
+
chars = CodeGenerator.valid_characters.dup
|
25
|
+
generator = if CodeGenerator.repeat_chars
|
26
|
+
lambda{ chars.sample() }
|
27
|
+
else
|
28
|
+
lambda{ chars.delete_at(rand(chars.length)) }
|
29
|
+
end
|
30
|
+
randomized_array &generator
|
26
31
|
end
|
27
32
|
|
33
|
+
def randomized_array(&block)
|
34
|
+
Array.new(length){ block.call }.join
|
35
|
+
end
|
36
|
+
|
28
37
|
def generate_code
|
29
38
|
@code = random_string
|
30
39
|
if generate_unique?
|
@@ -42,16 +51,16 @@ module CodeGenerator
|
|
42
51
|
|
43
52
|
private
|
44
53
|
|
45
|
-
def
|
46
|
-
uniqueness[:
|
54
|
+
def get_model
|
55
|
+
uniqueness[:model].to_s.classify.constantize
|
47
56
|
rescue
|
48
57
|
raise CodeGenerator::ClassNotFoundError
|
49
58
|
end
|
50
59
|
|
51
|
-
def
|
52
|
-
raise CodeGenerator::ScopeNotSpecifiedError if uniqueness[:
|
60
|
+
def set_model_and_field!
|
61
|
+
raise CodeGenerator::ScopeNotSpecifiedError if uniqueness[:model].blank?
|
53
62
|
raise CodeGenerator::FieldNotSpecifiedError if uniqueness[:field].blank?
|
54
|
-
@
|
63
|
+
@model = get_model
|
55
64
|
@field_name = uniqueness[:field]
|
56
65
|
#TODO - raise an error if field not found for the model. Needs to be indepedent for ActiveRecord & Mongoid.
|
57
66
|
end
|