code_generator 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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.generator(uniqueness: { scope: :user, field: :confirmation_code })
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 (do not ignore :use_chars).
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.
@@ -21,6 +21,9 @@ module CodeGenerator
21
21
  @@invalid_chars = Array.new
22
22
 
23
23
  mattr_accessor :valid_chars
24
+
25
+ mattr_accessor :repeat_chars
26
+ @@repeat_chars = true
24
27
 
25
28
  class << self
26
29
  def configure
@@ -6,25 +6,34 @@ module CodeGenerator
6
6
 
7
7
  class Generator
8
8
 
9
- attr_reader :scoped_class
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
- set_scoped_class_and_field! if uniqueness
16
+ set_model_and_field! if uniqueness
17
17
  end
18
18
 
19
19
  def unique?
20
- @scoped_class.send(:where, {@field_name => @code}).empty?
20
+ @model.send(:where, {@field_name => @code}).empty?
21
21
  end
22
22
 
23
23
  def random_string
24
- chars = CodeGenerator.valid_characters
25
- Array.new(length){ chars.sample() }.join
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 get_scoped_class
46
- uniqueness[:scope].to_s.classify.constantize
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 set_scoped_class_and_field!
52
- raise CodeGenerator::ScopeNotSpecifiedError if uniqueness[:scope].blank?
60
+ def set_model_and_field!
61
+ raise CodeGenerator::ScopeNotSpecifiedError if uniqueness[:model].blank?
53
62
  raise CodeGenerator::FieldNotSpecifiedError if uniqueness[:field].blank?
54
- @scoped_class = get_scoped_class
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
@@ -1,5 +1,5 @@
1
1
  module CodeGenerator
2
2
 
3
- VERSION = "0.0.2".freeze
3
+ VERSION = "0.0.3".freeze
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: