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