|
7 | 7 | # %w - upper and lower alpha character |
8 | 8 | # %p - URL-safe punctuation |
9 | 9 | # |
10 | | -# Any pattern can be followed by a number, representing how many of that type to generate |
| 10 | +# Any pattern can be followed by a number, representing how many of that type |
| 11 | +# to generate |
11 | 12 |
|
12 | 13 | module Mongoid |
13 | 14 | module Token |
14 | 15 | module Generator |
15 | 16 | REPLACE_PATTERN = /%((?<character>[cCdDhHpsw]{1})(?<length>\d+(,\d+)?)?)/ |
| 17 | + TYPES = { |
| 18 | + c: ->(length) { down_character(length) }, |
| 19 | + C: ->(length) { up_character(length) }, |
| 20 | + d: ->(length) { digits(length) }, |
| 21 | + D: ->(length) { integer(length) }, |
| 22 | + h: ->(length) { digits(length, 16) }, |
| 23 | + H: ->(length) { integer(length, 16) }, |
| 24 | + s: ->(length) { alphanumeric(length) }, |
| 25 | + w: ->(length) { alpha(length) }, |
| 26 | + p: ->(_length) { "-" } |
| 27 | + }.freeze |
16 | 28 |
|
17 | 29 | def self.generate(pattern) |
18 | | - pattern.gsub REPLACE_PATTERN do |match| |
19 | | - match_data = $~ |
20 | | - type = match_data[:character] |
21 | | - length = [match_data[:length].to_i, 1].max |
| 30 | + pattern.gsub REPLACE_PATTERN do |
| 31 | + match = $~ |
| 32 | + type = match[:character] |
| 33 | + length = [match[:length].to_i, 1].max |
22 | 34 |
|
23 | | - case type |
24 | | - when 'c' |
25 | | - down_character(length) |
26 | | - when 'C' |
27 | | - up_character(length) |
28 | | - when 'd' |
29 | | - digits(length) |
30 | | - when 'D' |
31 | | - integer(length) |
32 | | - when 'h' |
33 | | - digits(length, 16) |
34 | | - when 'H' |
35 | | - integer(length, 16) |
36 | | - when 's' |
37 | | - alphanumeric(length) |
38 | | - when 'w' |
39 | | - alpha(length) |
40 | | - when 'p' |
41 | | - "-" |
42 | | - end |
| 35 | + TYPES[type.to_sym].call(length) |
43 | 36 | end |
44 | 37 | end |
45 | 38 |
|
46 | | - private |
47 | 39 | def self.rand_string_from_chars(chars, length = 1) |
48 | | - Array.new(length).map{ chars.sample }.join |
| 40 | + Array.new(length).map { chars.sample }.join |
49 | 41 | end |
50 | 42 |
|
51 | 43 | def self.down_character(length = 1) |
52 | | - self.rand_string_from_chars ('a'..'z').to_a, length |
| 44 | + rand_string_from_chars ("a".."z").to_a, length |
53 | 45 | end |
54 | 46 |
|
55 | 47 | def self.up_character(length = 1) |
56 | | - self.rand_string_from_chars ('A'..'Z').to_a, length |
| 48 | + rand_string_from_chars ("A".."Z").to_a, length |
57 | 49 | end |
58 | 50 |
|
59 | 51 | def self.integer(length = 1, base = 10) |
60 | | - (rand(base**length - base**(length-1)) + base**(length-1)).to_s(base) |
| 52 | + (rand(base**length - base**(length - 1)) + base**(length - 1)). |
| 53 | + to_s(base) |
61 | 54 | end |
62 | 55 |
|
63 | 56 | def self.digits(length = 1, base = 10) |
64 | 57 | rand(base**length).to_s(base).rjust(length, "0") |
65 | 58 | end |
66 | 59 |
|
67 | 60 | def self.alpha(length = 1) |
68 | | - self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length |
| 61 | + rand_string_from_chars (("A".."Z").to_a + ("a".."z").to_a), length |
69 | 62 | end |
70 | 63 |
|
71 | 64 | def self.alphanumeric(length = 1) |
72 | | - (1..length).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join |
| 65 | + (1..length).map do |
| 66 | + i = Kernel.rand(62) |
| 67 | + if i < 10 |
| 68 | + i + 48 |
| 69 | + else |
| 70 | + i + (i < 36 ? 55 : 61) |
| 71 | + end.chr |
| 72 | + end.join |
73 | 73 | end |
74 | 74 |
|
75 | 75 | def self.punctuation(length = 1) |
76 | | - self.rand_string_from_chars ['.','-','_','=','+','$'], length |
| 76 | + rand_string_from_chars %w[. - _ = + $], length |
77 | 77 | end |
78 | 78 | end |
79 | 79 | end |
|
0 commit comments