|
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+)?)?)/ |
16 | 17 |
|
17 | 18 | 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 |
| 19 | + pattern.gsub REPLACE_PATTERN do |
| 20 | + match = $~ |
| 21 | + type = match[:character] |
| 22 | + length = [match[:length].to_i, 1].max |
22 | 23 |
|
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 |
| 24 | + type_for(type, length) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def self.type_for(type, length) |
| 29 | + case type |
| 30 | + when "c" then down_character(length) |
| 31 | + when "C" then up_character(length) |
| 32 | + when "d" then digits(length) |
| 33 | + when "D" then integer(length) |
| 34 | + when "h" then digits(length, 16) |
| 35 | + when "H" then integer(length, 16) |
| 36 | + when "s" then alphanumeric(length) |
| 37 | + when "w" then alpha(length) |
| 38 | + when "p" then "-" |
43 | 39 | end |
44 | 40 | end |
45 | 41 |
|
46 | | - private |
47 | 42 | def self.rand_string_from_chars(chars, length = 1) |
48 | | - Array.new(length).map{ chars.sample }.join |
| 43 | + Array.new(length).map { chars.sample }.join |
49 | 44 | end |
50 | 45 |
|
51 | 46 | def self.down_character(length = 1) |
52 | | - self.rand_string_from_chars ('a'..'z').to_a, length |
| 47 | + rand_string_from_chars ("a".."z").to_a, length |
53 | 48 | end |
54 | 49 |
|
55 | 50 | def self.up_character(length = 1) |
56 | | - self.rand_string_from_chars ('A'..'Z').to_a, length |
| 51 | + rand_string_from_chars ("A".."Z").to_a, length |
57 | 52 | end |
58 | 53 |
|
59 | 54 | def self.integer(length = 1, base = 10) |
60 | | - (rand(base**length - base**(length-1)) + base**(length-1)).to_s(base) |
| 55 | + (rand(base**length - base**(length - 1)) + base**(length - 1)). |
| 56 | + to_s(base) |
61 | 57 | end |
62 | 58 |
|
63 | 59 | def self.digits(length = 1, base = 10) |
64 | 60 | rand(base**length).to_s(base).rjust(length, "0") |
65 | 61 | end |
66 | 62 |
|
67 | 63 | def self.alpha(length = 1) |
68 | | - self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length |
| 64 | + rand_string_from_chars (("A".."Z").to_a + ("a".."z").to_a), length |
69 | 65 | end |
70 | 66 |
|
71 | 67 | def self.alphanumeric(length = 1) |
72 | | - (1..length).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join |
| 68 | + (1..length).map do |
| 69 | + i = Kernel.rand(62) |
| 70 | + if i < 10 |
| 71 | + i + 48 |
| 72 | + else |
| 73 | + i + (i < 36 ? 55 : 61) |
| 74 | + end.chr |
| 75 | + end.join |
73 | 76 | end |
74 | 77 |
|
75 | 78 | def self.punctuation(length = 1) |
76 | | - self.rand_string_from_chars ['.','-','_','=','+','$'], length |
| 79 | + rand_string_from_chars %w[. - _ = + $], length |
77 | 80 | end |
78 | 81 | end |
79 | 82 | end |
|
0 commit comments