string_pattern 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +4 -4
- data/LICENSE +21 -21
- data/README.md +421 -396
- data/data/english/adjs.json +19001 -0
- data/data/english/nouns.json +112986 -0
- data/data/spanish/palabras.json +1 -0
- data/lib/string/pattern/add_to_ruby.rb +294 -284
- data/lib/string_pattern.rb +1029 -935
- metadata +12 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b21b55691cd8a85f6a79e28901c853c8a9d8ea833ff563574a3dc710a2db6a0a
|
4
|
+
data.tar.gz: 5a318656e7b6d5574aa22eba229015d4aefcdd4c9a9e48cbb2485964ce01bf2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7951ad012431ec6bd808f864d2ec7bd384823b047978fb9d304ffcd6681adf22e65a74c007782e074f26152f605f2d5b09197e18591611e06cf17eb2e451f4cf
|
7
|
+
data.tar.gz: 871bd49b1de3a20b0622bff240e0ebbf34dea567fbb5c4901e493d188640c5c2680b3dfb71216829b338e234b3233695af3e02d19105983cb43e3e4d0c542773
|
data/.yardopts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
--readme README.md
|
2
|
-
--title 'string_pattern - You can easily generate strings supplying a very simple pattern. Also you can validate if a text fulfill an specific pattern or even generate a string following a pattern and returning wrong length, value... for testing your applications.'
|
3
|
-
--charset utf-8
|
4
|
-
--markup markdown
|
1
|
+
--readme README.md
|
2
|
+
--title 'string_pattern - You can easily generate strings supplying a very simple pattern. Also you can validate if a text fulfill an specific pattern or even generate a string following a pattern and returning wrong length, value... for testing your applications.'
|
3
|
+
--charset utf-8
|
4
|
+
--markup markdown
|
5
5
|
'lib/**/*.rb' - '*.md' - 'LICENSE'
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2017 Mario Ruiz
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Mario Ruiz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,396 +1,421 @@
|
|
1
|
-
# StringPattern
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/string_pattern.svg)](https://rubygems.org/gems/string_pattern)
|
4
|
-
|
5
|
-
With this gem, you can easily generate strings supplying a very simple pattern.
|
6
|
-
Also, you can validate if a text fulfills a specific pattern or even generate a string following a pattern and returning the wrong length, value... for testing your applications.
|
7
|
-
|
8
|
-
Also you can use regular expressions (Regexp) to generate strings: `/[a-z0-9]{2,5}\w+/.gen`
|
9
|
-
|
10
|
-
To do even more take a look at [nice_hash gem](https://github.com/MarioRuiz/nice_hash)
|
11
|
-
|
12
|
-
## Installation
|
13
|
-
|
14
|
-
Add this line to your application's Gemfile:
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
gem 'string_pattern'
|
18
|
-
```
|
19
|
-
|
20
|
-
And then execute:
|
21
|
-
|
22
|
-
$ bundle
|
23
|
-
|
24
|
-
Or install it yourself as:
|
25
|
-
|
26
|
-
$ gem install string_pattern
|
27
|
-
|
28
|
-
## Usage
|
29
|
-
|
30
|
-
### What is a string pattern?
|
31
|
-
|
32
|
-
A pattern is a string where we supply these elements "a-b:c" where a is min_length, b is max_length (optional) and c is a set of symbol_type
|
33
|
-
|
34
|
-
min_length: minimum length of the string
|
35
|
-
|
36
|
-
max_length (optional): maximum length of the string. If not provided, the result will be with the min_length provided
|
37
|
-
|
38
|
-
symbol_type: The type of the string we want.
|
39
|
-
x: from a to z (lowercase)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
p "
|
65
|
-
#>
|
66
|
-
|
67
|
-
#
|
68
|
-
p
|
69
|
-
#>
|
70
|
-
|
71
|
-
#
|
72
|
-
p
|
73
|
-
#>
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
p
|
79
|
-
#>
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
1000
|
89
|
-
|
90
|
-
|
91
|
-
StringPattern.
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
#>
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
#>
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
#
|
164
|
-
|
165
|
-
|
166
|
-
# '
|
167
|
-
|
168
|
-
|
169
|
-
#
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
```ruby
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
#
|
188
|
-
|
189
|
-
|
190
|
-
#
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
#
|
210
|
-
"4
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
```
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
"
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
Examples:
|
281
|
-
|
282
|
-
```ruby
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
"10
|
314
|
-
#>
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
"
|
339
|
-
#>
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
```
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
#>
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
["5:
|
383
|
-
#>
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
1
|
+
# StringPattern
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/string_pattern.svg)](https://rubygems.org/gems/string_pattern)
|
4
|
+
|
5
|
+
With this gem, you can easily generate strings supplying a very simple pattern.
|
6
|
+
Also, you can validate if a text fulfills a specific pattern or even generate a string following a pattern and returning the wrong length, value... for testing your applications. Perfect to be used in test data factories.
|
7
|
+
|
8
|
+
Also you can use regular expressions (Regexp) to generate strings: `/[a-z0-9]{2,5}\w+/.gen`
|
9
|
+
|
10
|
+
To do even more take a look at [nice_hash gem](https://github.com/MarioRuiz/nice_hash)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'string_pattern'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install string_pattern
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
### What is a string pattern?
|
31
|
+
|
32
|
+
A pattern is a string where we supply these elements "a-b:c" where a is min_length, b is max_length (optional) and c is a set of symbol_type
|
33
|
+
|
34
|
+
min_length: minimum length of the string
|
35
|
+
|
36
|
+
max_length (optional): maximum length of the string. If not provided, the result will be with the min_length provided
|
37
|
+
|
38
|
+
symbol_type: The type of the string we want.
|
39
|
+
x: from a to z (lowercase)
|
40
|
+
X: A to Z (capital letters)
|
41
|
+
L: A to Z and a to z
|
42
|
+
T: National characters defined on StringPattern.national_chars
|
43
|
+
n or N: for numbers. 0 to 9
|
44
|
+
$: special characters, $%&#... (includes blank space)
|
45
|
+
_: blank space
|
46
|
+
*: all characters
|
47
|
+
0: empty string will be accepted. It needs to be at the beginning of the symbol_type string
|
48
|
+
@: It will generate a valid email following the official algorithm. It cannot be used with other symbol_type
|
49
|
+
W: for English words, capital and lower. It cannot be used with other symbol_type
|
50
|
+
w: for English words only lower and words separated by underscore. It cannot be used with other symbol_type
|
51
|
+
P: for Spanish words, capital and lower. It cannot be used with other symbol_type
|
52
|
+
p: for Spanish words only lower and words separated by underscore. It cannot be used with other symbol_type
|
53
|
+
|
54
|
+
### How to generate a string following a pattern
|
55
|
+
|
56
|
+
To generate a string following a pattern you can do it using directly the StringPattern class or the generate method in the class, be aware you can always use also the alias method: gen
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'string_pattern'
|
60
|
+
|
61
|
+
#StringPattern class
|
62
|
+
p StringPattern.generate "10:N"
|
63
|
+
#>3448910834
|
64
|
+
p StringPattern.gen "5:X"
|
65
|
+
#>JDDDK
|
66
|
+
|
67
|
+
#String class
|
68
|
+
p "4:Nx".gen
|
69
|
+
#>xaa3
|
70
|
+
|
71
|
+
#Symbol class
|
72
|
+
p :"10:T".generate
|
73
|
+
#>AccBdjklñD
|
74
|
+
|
75
|
+
#Array class
|
76
|
+
p [:"3:N", "fixed", :"3:N"].gen
|
77
|
+
#>334fixed920
|
78
|
+
p "(,3:N,) ,3:N,-,2:N,-,2:N".split(',').generate
|
79
|
+
#>(937) 980-65-05
|
80
|
+
|
81
|
+
#Kernel
|
82
|
+
p gen "3:N"
|
83
|
+
#>443
|
84
|
+
```
|
85
|
+
|
86
|
+
#### Generating unique strings
|
87
|
+
|
88
|
+
If you want to generate for example 1000 strings and be sure all those strings are different you can use:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
StringPattern.dont_repeat = true #default: false
|
92
|
+
1000.times {
|
93
|
+
puts :"6-20:L/N/".gen
|
94
|
+
}
|
95
|
+
StringPattern.cache_values = Hash.new() #to clean the generated values from memory
|
96
|
+
```
|
97
|
+
|
98
|
+
Using dont_repeat all the generated string during the current run will be unique.
|
99
|
+
|
100
|
+
In case you just want one particular string to be unique but not the rest then add to the pattern just in the end the symbol: &
|
101
|
+
|
102
|
+
The pattern needs to be a symbol object.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
1000.times {
|
106
|
+
puts :"6-20:L/N/&".gen #will be unique
|
107
|
+
puts :"10:N".gen
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
#### Generate words randomly in English or Spanish
|
112
|
+
|
113
|
+
To generate a string of the length you want that will include only real words, use the symbol types:
|
114
|
+
* W: generates English words following CamelCase ('ExampleOutput')
|
115
|
+
* w: generates English words following snake_case ('example_output')
|
116
|
+
* P: generates Spanish words following CamelCase ('EjemploSalida')
|
117
|
+
* p: generates Spanish words following snake_case ('ejemplo_salida')
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
require 'string_pattern'
|
121
|
+
|
122
|
+
puts '10-30:W'.gen
|
123
|
+
#> FirstLieutenant
|
124
|
+
puts '10-30:w'.gen
|
125
|
+
#> paris_university
|
126
|
+
puts '10-30:P'.gen
|
127
|
+
#> SillaMetelizada
|
128
|
+
puts '10-30:p'.gen
|
129
|
+
#> despacho_grande
|
130
|
+
```
|
131
|
+
|
132
|
+
#### Generate strings using Regular Expressions (Regexp)
|
133
|
+
|
134
|
+
Take in consideration this feature is not supporting all possibilities for Regular expressions but it is fully functional. If you find any bug or limitation please add it to issues: https://github.com/MarioRuiz/string_pattern/issues
|
135
|
+
|
136
|
+
In case you want to change the default maximum for repetitions when using * or +: `StringPattern.default_infinite = 30` . By default is 10.
|
137
|
+
|
138
|
+
If you want to translate a regular expression into an StringPattern use the method we added to Regexp class: `to_sp`
|
139
|
+
|
140
|
+
Examples:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
/[a-z0-9]{2-5}\w+/.to_sp
|
144
|
+
#> ["2-5:nx", "1-10:Ln_"]
|
145
|
+
|
146
|
+
#regular expression for UUID v4
|
147
|
+
/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/.to_sp
|
148
|
+
#> ["8:n[ABCDEF]", "-", "4:n[ABCDEF]", "-4", "3:n[ABCDEF]", "-", "1:[89AB]", "3:n[ABCDEF]", "-", "12:n[ABCDEF]"]
|
149
|
+
```
|
150
|
+
|
151
|
+
If you want to generate a random string following the regular expression, you can do it like a normal string pattern:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
|
155
|
+
regexp = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/
|
156
|
+
|
157
|
+
# using StringPattern class
|
158
|
+
puts StringPattern.generate(regexp)
|
159
|
+
|
160
|
+
# using Kernel
|
161
|
+
puts generate(regexp)
|
162
|
+
|
163
|
+
# using generate method added to Regexp class
|
164
|
+
puts regexp.generate
|
165
|
+
|
166
|
+
#using the alias 'gen'
|
167
|
+
puts regexp.gen
|
168
|
+
|
169
|
+
# output:
|
170
|
+
#>7009574B-6F2F-436E-BB7A-EA5FDA6B4E47
|
171
|
+
#>5FB1718F-108A-4F62-8170-33C43FD86B1D
|
172
|
+
#>05745B6F-93BA-475F-8118-DD56E5EAC4D1
|
173
|
+
#>2D6FC189-8D50-45A8-B182-780193838502
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
### String patterns
|
178
|
+
|
179
|
+
#### How to generate one or another string
|
180
|
+
|
181
|
+
In case you need to specify that the string is generated selecting one or another fixed string or pattern, you can do it by using Array of patterns and in the position you want you can add an array with the possible values
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
p ["uno:", :"5:N", ['.red','.green', :'3:L'] ].gen
|
185
|
+
|
186
|
+
# first position a fixed string: "uno:"
|
187
|
+
# second position 5 random numbers
|
188
|
+
# third position one of these values: '.red', '.green' or 3 letters
|
189
|
+
|
190
|
+
# example output:
|
191
|
+
# 'uno:34322.red'
|
192
|
+
# 'uno:44432.green'
|
193
|
+
# 'uno:34322.red'
|
194
|
+
# 'uno:28795xAB'
|
195
|
+
|
196
|
+
```
|
197
|
+
|
198
|
+
Take in consideration that this is only available to generate successful strings but not for validation
|
199
|
+
|
200
|
+
#### Custom characters
|
201
|
+
|
202
|
+
Also, it's possible to provide the characters we want. To do that we'll use the symbol_type [characters]
|
203
|
+
|
204
|
+
If we want to add the character ] we have to write ]]
|
205
|
+
|
206
|
+
Examples
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
# four chars from the ones provided: asDF9
|
210
|
+
p "4:[asDF9]".gen #> aaaa, asFF, 9sFD
|
211
|
+
|
212
|
+
# from 2 to 20 chars, capital and lower chars (Xx) and also valid the characters $#6
|
213
|
+
p "2-20:[$#6]Xx".gen #> aaaa, asFF, 66, B$DkKL#9aDD
|
214
|
+
|
215
|
+
# four chars from these: asDF]9
|
216
|
+
p "4:[asDF]]9]".gen #> aa]a, asFF, 9s]D
|
217
|
+
```
|
218
|
+
|
219
|
+
#### Required characters or symbol types
|
220
|
+
|
221
|
+
We'll use the symbol / to specify which characters or symbols we want to be included on the resulting string as required values /symbols or characters/
|
222
|
+
|
223
|
+
If we need to add the character / we'll use //
|
224
|
+
|
225
|
+
Examples:
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
# four characters. optional: capitals and numbers, required: lower
|
229
|
+
"4:XN/x/".gen # aaaa, FF9b, j4em, asdf, ADFt
|
230
|
+
|
231
|
+
# from 6 to 15 chars. optional: numbers, capitals and the chars $ and Æ. required the chars: 23abCD
|
232
|
+
"6-15:[/23abCD/$Æ]NX".gen # bCa$D32, 32DJIOKLaCb, b23aD568C
|
233
|
+
|
234
|
+
# from 4 to 9 chars. optional: numbers and capitals. required: lowers and the characters $ and 5
|
235
|
+
"4-9:[/$5/]XN/x/".generate # aa5$, F5$F9b, j$4em5, a5sdf$, $ADFt5
|
236
|
+
```
|
237
|
+
|
238
|
+
#### Excluded characters
|
239
|
+
|
240
|
+
If we want to exclude a few characters in the result, we'll use the symbol %characters%
|
241
|
+
|
242
|
+
If you need to exclude the character %, you should use %%
|
243
|
+
|
244
|
+
Examples:
|
245
|
+
|
246
|
+
```ruby
|
247
|
+
# from 2 to 20 characters. optional: Numbers and characters A, B and C. excluded: the characters 8 and 3
|
248
|
+
"2-20:[%83%ABC]N".gen # B49, 22900, 9CAB, 22, 11CB6270C26C4572A50C
|
249
|
+
|
250
|
+
# 10 chars. optional: Letters (capital and lower). required: numbers. excluded: the characters 0 and WXYzZ
|
251
|
+
"10:L/n/[%0WXYzZ%]".gen # GoO2ukCt4l, Q1Je2remFL, qPg1T92T2H, 4445556781
|
252
|
+
```
|
253
|
+
|
254
|
+
#### Not fulfilling a pattern
|
255
|
+
|
256
|
+
If we want our resulting string doesn't fulfill the pattern we supply, then we'll use the symbol ! at the beginning
|
257
|
+
|
258
|
+
Examples:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
"!4:XN/x/".gen # a$aaa, FF9B, j4DDDem, as, 2345
|
262
|
+
|
263
|
+
"!10:N".gen # 123, 34899Add34, 3434234234234008, AAFj#kd2x
|
264
|
+
```
|
265
|
+
|
266
|
+
### Generate a string with specific expected errors
|
267
|
+
|
268
|
+
Usually, for testing purposes you need to generate strings that don't fulfill a specific pattern, then you can supply as a parameter expected_errors (alias: errors)
|
269
|
+
|
270
|
+
The possible values you can specify is one or more of these ones: :length, :min_length, :max_length, :value, :required_data, :excluded_data, :string_set_not_allowed
|
271
|
+
|
272
|
+
:length: wrong length, minimum or maximum
|
273
|
+
:min_length: wrong minimum length
|
274
|
+
:max_length: wrong maximum length
|
275
|
+
:value: wrong resultant value
|
276
|
+
:required_data: the output string won't include all necessary required data. It works only if required data supplied on the pattern.
|
277
|
+
:excluded_data: the resultant string will include one or more characters that should be excluded. It works only if excluded data supplied on the pattern.
|
278
|
+
:string_set_not_allowed: it will include one or more characters that are not supposed to be on the string.
|
279
|
+
|
280
|
+
Examples:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
"10-20:N".gen errors: [:min_length]
|
284
|
+
#> 627, 098262, 3408
|
285
|
+
|
286
|
+
"20:N".gen errors: [:length, :value]
|
287
|
+
#> |13, tS1b)r-1)<RT65202eTo6bV0g~, 021400323<2ahL0NP86a698063*56076
|
288
|
+
|
289
|
+
"10:L/n/".gen errors: [:value]
|
290
|
+
#> 1hwIw;v{KQ, mpk*l]!7:!, wocipgZt8@
|
291
|
+
|
292
|
+
```
|
293
|
+
|
294
|
+
### Validate if a string is following a pattern
|
295
|
+
|
296
|
+
If you need to validate if a specific text is fulfilling the pattern you can use the validate method.
|
297
|
+
|
298
|
+
If a string pattern supplied and no other parameters supplied the output will be an array with the errors detected.
|
299
|
+
|
300
|
+
|
301
|
+
Possible output values, empty array (validation without errors detected) or one or more of: :min_length, :max_length, :length, :value, :string_set_not_allowed, :required_data, :excluded_data
|
302
|
+
|
303
|
+
In case an array of patterns supplied it will return only true or false
|
304
|
+
|
305
|
+
Examples:
|
306
|
+
|
307
|
+
```ruby
|
308
|
+
#StringPattern class
|
309
|
+
StringPattern.validate((text: "This text will be validated", pattern: :"10-20:Xn")
|
310
|
+
#> [:max_length, :length, :value, :string_set_not_allowed]
|
311
|
+
|
312
|
+
#String class
|
313
|
+
"10:N".validate "333444"
|
314
|
+
#> [:min_length, :length]
|
315
|
+
|
316
|
+
#Symbol class
|
317
|
+
:"10:N".validate("333444")
|
318
|
+
#> [:min_length, :length]
|
319
|
+
|
320
|
+
#Array class
|
321
|
+
["5:L","3:xn","4-10:n"].validate "DjkljFFc343444390"
|
322
|
+
#> false
|
323
|
+
```
|
324
|
+
|
325
|
+
If we want to validate a string with a pattern and we are expecting to get specific errors, you can supply the parameter expected_errors (alias: errors) or not_expected_errors (aliases: non_expected_errors, not_errors).
|
326
|
+
|
327
|
+
In this case, the validate method will return true or false.
|
328
|
+
|
329
|
+
Examples:
|
330
|
+
|
331
|
+
```ruby
|
332
|
+
"10:N".val "3445", errors: [:min_length]
|
333
|
+
#> true
|
334
|
+
|
335
|
+
"10:N/[09]/".validate "4434039440", errors: [:value]
|
336
|
+
#> false
|
337
|
+
|
338
|
+
"10-12:XN/x/".validate "FDDDDDAA343434", errors: [:max_length, :required_data]
|
339
|
+
#> true
|
340
|
+
```
|
341
|
+
|
342
|
+
### Configure
|
343
|
+
|
344
|
+
#### SP_ADD_TO_RUBY
|
345
|
+
|
346
|
+
This gem adds the methods generate (alias: gen) and validate (alias: val) to the Ruby classes: String, Array, and Symbol.
|
347
|
+
|
348
|
+
Also adds the method generate (alias: gen) to Kernel. By default (true) it is always added.
|
349
|
+
|
350
|
+
In case you don't want to be added, just before requiring the library set:
|
351
|
+
|
352
|
+
```ruby
|
353
|
+
SP_ADD_TO_RUBY = false
|
354
|
+
require 'string_pattern'
|
355
|
+
```
|
356
|
+
|
357
|
+
In case it is set to true (default) then you will be able to use:
|
358
|
+
|
359
|
+
```ruby
|
360
|
+
require 'string_pattern'
|
361
|
+
|
362
|
+
#String object
|
363
|
+
"20-30:@".gen
|
364
|
+
#>dkj34MljjJD-df@jfdluul.dfu
|
365
|
+
|
366
|
+
"10:L/N/[/-./%d%]".validate("12ds6f--.s")
|
367
|
+
#>[:value, :string_set_not_allowed]
|
368
|
+
|
369
|
+
"20-40:@".validate(my_email)
|
370
|
+
|
371
|
+
#Kernel
|
372
|
+
gen "10:N"
|
373
|
+
#>3433409877
|
374
|
+
|
375
|
+
#Array object
|
376
|
+
"(,3:N,) ,3:N,-,2:N,-,2:N".split(",").generate
|
377
|
+
#>(937) 980-65-05
|
378
|
+
|
379
|
+
%w{( 3:N ) 1:_ 3:N - 2:N - 2:N}.gen
|
380
|
+
#>(045) 448-63-09
|
381
|
+
|
382
|
+
["1:L", "5-10:LN", "-", "3:N"].gen
|
383
|
+
#>zqWihV-746
|
384
|
+
```
|
385
|
+
|
386
|
+
#### national_chars
|
387
|
+
|
388
|
+
To specify which national characters will be used when using the symbol type: T, you use StringPattern.national_chars, by default is the English alphabet
|
389
|
+
|
390
|
+
```ruby
|
391
|
+
StringPattern.national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join + "áéíóúÁÉÍÓÚüÜñÑ"
|
392
|
+
"10-20:Tn".gen #>AAñ34Ef99éNOP
|
393
|
+
```
|
394
|
+
|
395
|
+
#### optimistic
|
396
|
+
|
397
|
+
If true it will check on the strings of the array positions supplied if they have the pattern format and assume in that case that is a pattern. If not it will assume the patterns on the array will be supplied as symbols. By default is set to true.
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
StringPattern.optimistic = false
|
401
|
+
["5:X","fixedtext", "3:N"].generate
|
402
|
+
#>5:Xfixedtext3:N
|
403
|
+
[:"5:X","fixedtext", :"3:N"].generate
|
404
|
+
#>AUJKJfixedtext454
|
405
|
+
|
406
|
+
StringPattern.optimistic = true
|
407
|
+
["5:X","fixedtext", "3:N"].generate
|
408
|
+
#>KKDMEfixedtext344
|
409
|
+
[:"5:X","fixedtext", :"3:N"].generate
|
410
|
+
#>SAAERfixedtext988
|
411
|
+
```
|
412
|
+
|
413
|
+
## Contributing
|
414
|
+
|
415
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.
|
416
|
+
|
417
|
+
|
418
|
+
## License
|
419
|
+
|
420
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
421
|
+
|