string_pattern 2.1.0 → 2.1.1

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 CHANGED
@@ -1,421 +1,423 @@
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
-
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. Even generate random words in English or Spanish.
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
+ #> SillaMetalizada
128
+ puts '10-30:p'.gen
129
+ #> despacho_grande
130
+ ```
131
+
132
+ The word list is loaded on the first request to generate words, after that the speed to generate words increases amazingly. 132000 English words and 250000 Spanish words. The vocabularies are public open sources, we only removed offensive words.
133
+
134
+ #### Generate strings using Regular Expressions (Regexp)
135
+
136
+ 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
137
+
138
+ In case you want to change the default maximum for repetitions when using * or +: `StringPattern.default_infinite = 30` . By default is 10.
139
+
140
+ If you want to translate a regular expression into an StringPattern use the method we added to Regexp class: `to_sp`
141
+
142
+ Examples:
143
+
144
+ ```ruby
145
+ /[a-z0-9]{2-5}\w+/.to_sp
146
+ #> ["2-5:nx", "1-10:Ln_"]
147
+
148
+ #regular expression for UUID v4
149
+ /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/.to_sp
150
+ #> ["8:n[ABCDEF]", "-", "4:n[ABCDEF]", "-4", "3:n[ABCDEF]", "-", "1:[89AB]", "3:n[ABCDEF]", "-", "12:n[ABCDEF]"]
151
+ ```
152
+
153
+ If you want to generate a random string following the regular expression, you can do it like a normal string pattern:
154
+
155
+ ```ruby
156
+
157
+ regexp = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/
158
+
159
+ # using StringPattern class
160
+ puts StringPattern.generate(regexp)
161
+
162
+ # using Kernel
163
+ puts generate(regexp)
164
+
165
+ # using generate method added to Regexp class
166
+ puts regexp.generate
167
+
168
+ #using the alias 'gen'
169
+ puts regexp.gen
170
+
171
+ # output:
172
+ #>7009574B-6F2F-436E-BB7A-EA5FDA6B4E47
173
+ #>5FB1718F-108A-4F62-8170-33C43FD86B1D
174
+ #>05745B6F-93BA-475F-8118-DD56E5EAC4D1
175
+ #>2D6FC189-8D50-45A8-B182-780193838502
176
+
177
+ ```
178
+
179
+ ### String patterns
180
+
181
+ #### How to generate one or another string
182
+
183
+ 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
184
+
185
+ ```ruby
186
+ p ["uno:", :"5:N", ['.red','.green', :'3:L'] ].gen
187
+
188
+ # first position a fixed string: "uno:"
189
+ # second position 5 random numbers
190
+ # third position one of these values: '.red', '.green' or 3 letters
191
+
192
+ # example output:
193
+ # 'uno:34322.red'
194
+ # 'uno:44432.green'
195
+ # 'uno:34322.red'
196
+ # 'uno:28795xAB'
197
+
198
+ ```
199
+
200
+ Take in consideration that this is only available to generate successful strings but not for validation
201
+
202
+ #### Custom characters
203
+
204
+ Also, it's possible to provide the characters we want. To do that we'll use the symbol_type [characters]
205
+
206
+ If we want to add the character ] we have to write ]]
207
+
208
+ Examples
209
+
210
+ ```ruby
211
+ # four chars from the ones provided: asDF9
212
+ p "4:[asDF9]".gen #> aaaa, asFF, 9sFD
213
+
214
+ # from 2 to 20 chars, capital and lower chars (Xx) and also valid the characters $#6
215
+ p "2-20:[$#6]Xx".gen #> aaaa, asFF, 66, B$DkKL#9aDD
216
+
217
+ # four chars from these: asDF]9
218
+ p "4:[asDF]]9]".gen #> aa]a, asFF, 9s]D
219
+ ```
220
+
221
+ #### Required characters or symbol types
222
+
223
+ 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/
224
+
225
+ If we need to add the character / we'll use //
226
+
227
+ Examples:
228
+
229
+ ```ruby
230
+ # four characters. optional: capitals and numbers, required: lower
231
+ "4:XN/x/".gen # aaaa, FF9b, j4em, asdf, ADFt
232
+
233
+ # from 6 to 15 chars. optional: numbers, capitals and the chars $ and Æ. required the chars: 23abCD
234
+ "6-15:[/23abCD/$Æ]NX".gen # bCa$D32, 32DJIOKLaCb, b23aD568C
235
+
236
+ # from 4 to 9 chars. optional: numbers and capitals. required: lowers and the characters $ and 5
237
+ "4-9:[/$5/]XN/x/".generate # aa5$, F5$F9b, j$4em5, a5sdf$, $ADFt5
238
+ ```
239
+
240
+ #### Excluded characters
241
+
242
+ If we want to exclude a few characters in the result, we'll use the symbol %characters%
243
+
244
+ If you need to exclude the character %, you should use %%
245
+
246
+ Examples:
247
+
248
+ ```ruby
249
+ # from 2 to 20 characters. optional: Numbers and characters A, B and C. excluded: the characters 8 and 3
250
+ "2-20:[%83%ABC]N".gen # B49, 22900, 9CAB, 22, 11CB6270C26C4572A50C
251
+
252
+ # 10 chars. optional: Letters (capital and lower). required: numbers. excluded: the characters 0 and WXYzZ
253
+ "10:L/n/[%0WXYzZ%]".gen # GoO2ukCt4l, Q1Je2remFL, qPg1T92T2H, 4445556781
254
+ ```
255
+
256
+ #### Not fulfilling a pattern
257
+
258
+ If we want our resulting string doesn't fulfill the pattern we supply, then we'll use the symbol ! at the beginning
259
+
260
+ Examples:
261
+
262
+ ```ruby
263
+ "!4:XN/x/".gen # a$aaa, FF9B, j4DDDem, as, 2345
264
+
265
+ "!10:N".gen # 123, 34899Add34, 3434234234234008, AAFj#kd2x
266
+ ```
267
+
268
+ ### Generate a string with specific expected errors
269
+
270
+ 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)
271
+
272
+ 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
273
+
274
+ :length: wrong length, minimum or maximum
275
+ :min_length: wrong minimum length
276
+ :max_length: wrong maximum length
277
+ :value: wrong resultant value
278
+ :required_data: the output string won't include all necessary required data. It works only if required data supplied on the pattern.
279
+ :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.
280
+ :string_set_not_allowed: it will include one or more characters that are not supposed to be on the string.
281
+
282
+ Examples:
283
+
284
+ ```ruby
285
+ "10-20:N".gen errors: [:min_length]
286
+ #> 627, 098262, 3408
287
+
288
+ "20:N".gen errors: [:length, :value]
289
+ #> |13, tS1b)r-1)<RT65202eTo6bV0g~, 021400323<2ahL0NP86a698063*56076
290
+
291
+ "10:L/n/".gen errors: [:value]
292
+ #> 1hwIw;v{KQ, mpk*l]!7:!, wocipgZt8@
293
+
294
+ ```
295
+
296
+ ### Validate if a string is following a pattern
297
+
298
+ If you need to validate if a specific text is fulfilling the pattern you can use the validate method.
299
+
300
+ If a string pattern supplied and no other parameters supplied the output will be an array with the errors detected.
301
+
302
+
303
+ 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
304
+
305
+ In case an array of patterns supplied it will return only true or false
306
+
307
+ Examples:
308
+
309
+ ```ruby
310
+ #StringPattern class
311
+ StringPattern.validate((text: "This text will be validated", pattern: :"10-20:Xn")
312
+ #> [:max_length, :length, :value, :string_set_not_allowed]
313
+
314
+ #String class
315
+ "10:N".validate "333444"
316
+ #> [:min_length, :length]
317
+
318
+ #Symbol class
319
+ :"10:N".validate("333444")
320
+ #> [:min_length, :length]
321
+
322
+ #Array class
323
+ ["5:L","3:xn","4-10:n"].validate "DjkljFFc343444390"
324
+ #> false
325
+ ```
326
+
327
+ 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).
328
+
329
+ In this case, the validate method will return true or false.
330
+
331
+ Examples:
332
+
333
+ ```ruby
334
+ "10:N".val "3445", errors: [:min_length]
335
+ #> true
336
+
337
+ "10:N/[09]/".validate "4434039440", errors: [:value]
338
+ #> false
339
+
340
+ "10-12:XN/x/".validate "FDDDDDAA343434", errors: [:max_length, :required_data]
341
+ #> true
342
+ ```
343
+
344
+ ### Configure
345
+
346
+ #### SP_ADD_TO_RUBY
347
+
348
+ This gem adds the methods generate (alias: gen) and validate (alias: val) to the Ruby classes: String, Array, and Symbol.
349
+
350
+ Also adds the method generate (alias: gen) to Kernel. By default (true) it is always added.
351
+
352
+ In case you don't want to be added, just before requiring the library set:
353
+
354
+ ```ruby
355
+ SP_ADD_TO_RUBY = false
356
+ require 'string_pattern'
357
+ ```
358
+
359
+ In case it is set to true (default) then you will be able to use:
360
+
361
+ ```ruby
362
+ require 'string_pattern'
363
+
364
+ #String object
365
+ "20-30:@".gen
366
+ #>dkj34MljjJD-df@jfdluul.dfu
367
+
368
+ "10:L/N/[/-./%d%]".validate("12ds6f--.s")
369
+ #>[:value, :string_set_not_allowed]
370
+
371
+ "20-40:@".validate(my_email)
372
+
373
+ #Kernel
374
+ gen "10:N"
375
+ #>3433409877
376
+
377
+ #Array object
378
+ "(,3:N,) ,3:N,-,2:N,-,2:N".split(",").generate
379
+ #>(937) 980-65-05
380
+
381
+ %w{( 3:N ) 1:_ 3:N - 2:N - 2:N}.gen
382
+ #>(045) 448-63-09
383
+
384
+ ["1:L", "5-10:LN", "-", "3:N"].gen
385
+ #>zqWihV-746
386
+ ```
387
+
388
+ #### national_chars
389
+
390
+ 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
391
+
392
+ ```ruby
393
+ StringPattern.national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join + "áéíóúÁÉÍÓÚüÜñÑ"
394
+ "10-20:Tn".gen #>AAñ34Ef99éNOP
395
+ ```
396
+
397
+ #### optimistic
398
+
399
+ 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.
400
+
401
+ ```ruby
402
+ StringPattern.optimistic = false
403
+ ["5:X","fixedtext", "3:N"].generate
404
+ #>5:Xfixedtext3:N
405
+ [:"5:X","fixedtext", :"3:N"].generate
406
+ #>AUJKJfixedtext454
407
+
408
+ StringPattern.optimistic = true
409
+ ["5:X","fixedtext", "3:N"].generate
410
+ #>KKDMEfixedtext344
411
+ [:"5:X","fixedtext", :"3:N"].generate
412
+ #>SAAERfixedtext988
413
+ ```
414
+
415
+ ## Contributing
416
+
417
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.
418
+
419
+
420
+ ## License
421
+
422
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
423
+