string_pattern 1.4.0 → 1.4.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.
- checksums.yaml +4 -4
- data/.yardopts +4 -4
- data/LICENSE +21 -21
- data/README.md +323 -321
- data/lib/string/pattern/add_to_ruby.rb +67 -67
- data/lib/string_pattern.rb +920 -918
- metadata +2 -2
data/README.md
CHANGED
@@ -1,321 +1,323 @@
|
|
1
|
-
# StringPattern
|
2
|
-
|
3
|
-
[](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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
```
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
p StringPattern.
|
57
|
-
#>
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
p "
|
71
|
-
#>
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
If
|
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
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
If
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
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
|
-
|
281
|
-
|
282
|
-
|
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
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
1
|
+
# StringPattern
|
2
|
+
|
3
|
+
[](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
|
+
To do even more take a look at [nice_hash gem](https://github.com/MarioRuiz/nice_hash)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'string_pattern'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install string_pattern
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### What is a string pattern?
|
29
|
+
|
30
|
+
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
|
31
|
+
|
32
|
+
min_length: minimum length of the string
|
33
|
+
|
34
|
+
max_length (optional): maximum length of the string. If not provided, the result will be with the min_length provided
|
35
|
+
|
36
|
+
symbol_type: The type of the string we want.
|
37
|
+
x: from a to z (lowercase)
|
38
|
+
X: A to Z (capital letters)
|
39
|
+
L: A to Z and a to z
|
40
|
+
T: National characters defined on StringPattern.national_chars
|
41
|
+
n or N: for numbers. 0 to 9
|
42
|
+
$: special characters, $%&#... (includes blank space)
|
43
|
+
_: blank space
|
44
|
+
*: all characters
|
45
|
+
0: empty string will be accepted. It needs to be at the beginning of the symbol_type string
|
46
|
+
@: It will generate a valid email following the official algorithm. It cannot be used with other symbol_type
|
47
|
+
|
48
|
+
### How to generate a string following a pattern
|
49
|
+
|
50
|
+
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
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
require 'string_pattern'
|
54
|
+
|
55
|
+
#StringPattern class
|
56
|
+
p StringPattern.generate "10:N"
|
57
|
+
#>3448910834
|
58
|
+
p StringPattern.gen "5:X"
|
59
|
+
#>JDDDK
|
60
|
+
|
61
|
+
#String class
|
62
|
+
p "4:Nx".gen
|
63
|
+
#>xaa3
|
64
|
+
|
65
|
+
#Symbol class
|
66
|
+
p :"10:T".generate
|
67
|
+
#>AccBdjklñD
|
68
|
+
|
69
|
+
#Array class
|
70
|
+
p [:"3:N", "fixed", :"3:N"].gen
|
71
|
+
#>334fixed920
|
72
|
+
p "(,3:N,) ,3:N,-,2:N,-,2:N".split(',').generate
|
73
|
+
#>(937) 980-65-05
|
74
|
+
|
75
|
+
#Kernel
|
76
|
+
p gen "3:N"
|
77
|
+
#>443
|
78
|
+
```
|
79
|
+
|
80
|
+
If you want to generate for example 1000 strings and be sure all those strings are different you can use:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
StringPattern.dont_repeat = true #default: false
|
84
|
+
1000.times {
|
85
|
+
puts :"6-20:L/N/".gen
|
86
|
+
}
|
87
|
+
StringPattern.cache_values = Hash.new() #to clean the generated values from memory
|
88
|
+
```
|
89
|
+
|
90
|
+
Using dont_repeat all the generated string during the current run will be unique.
|
91
|
+
|
92
|
+
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: &
|
93
|
+
|
94
|
+
The pattern needs to be a symbol object.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
1000.times {
|
98
|
+
puts :"6-20:L/N/&".gen #will be unique
|
99
|
+
puts :"10:N".gen
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
103
|
+
#### Custom characters
|
104
|
+
|
105
|
+
Also, it's possible to provide the characters we want. To do that we'll use the symbol_type [characters]
|
106
|
+
|
107
|
+
If we want to add the character ] we have to write ]]
|
108
|
+
|
109
|
+
Examples
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# four chars from the ones provided: asDF9
|
113
|
+
p "4:[asDF9]".gen #> aaaa, asFF, 9sFD
|
114
|
+
|
115
|
+
# from 2 to 20 chars, capital and lower chars (Xx) and also valid the characters $#6
|
116
|
+
p "2-20:[$#6]Xx".gen #> aaaa, asFF, 66, B$DkKL#9aDD
|
117
|
+
|
118
|
+
# four chars from these: asDF]9
|
119
|
+
p "4:[asDF]]9]".gen #> aa]a, asFF, 9s]D
|
120
|
+
```
|
121
|
+
|
122
|
+
#### Required characters or symbol types
|
123
|
+
|
124
|
+
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/
|
125
|
+
|
126
|
+
If we need to add the character / we'll use //
|
127
|
+
|
128
|
+
Examples:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
# four characters. optional: capitals and numbers, required: lower
|
132
|
+
"4:XN/x/".gen # aaaa, FF9b, j4em, asdf, ADFt
|
133
|
+
|
134
|
+
# from 6 to 15 chars. optional: numbers, capitals and the chars $ and Æ. required the chars: 23abCD
|
135
|
+
"6-15:[/23abCD/$Æ]NX".gen # bCa$D32, 32DJIOKLaCb, b23aD568C
|
136
|
+
|
137
|
+
# from 4 to 9 chars. optional: numbers and capitals. required: lowers and the characters $ and 5
|
138
|
+
"4-9:[/$5/]XN/x/".generate # aa5$, F5$F9b, j$4em5, a5sdf$, $ADFt5
|
139
|
+
```
|
140
|
+
|
141
|
+
#### Excluded characters
|
142
|
+
|
143
|
+
If we want to exclude a few characters in the result, we'll use the symbol %characters%
|
144
|
+
|
145
|
+
If you need to exclude the character %, you should use %%
|
146
|
+
|
147
|
+
Examples:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
# from 2 to 20 characters. optional: Numbers and characters A, B and C. excluded: the characters 8 and 3
|
151
|
+
"2-20:[%83%ABC]N".gen # B49, 22900, 9CAB, 22, 11CB6270C26C4572A50C
|
152
|
+
|
153
|
+
# 10 chars. optional: Letters (capital and lower). required: numbers. excluded: the characters 0 and WXYzZ
|
154
|
+
"10:L/n/[%0WXYzZ%]".gen # GoO2ukCt4l, Q1Je2remFL, qPg1T92T2H, 4445556781
|
155
|
+
```
|
156
|
+
|
157
|
+
#### Not fulfilling a pattern
|
158
|
+
|
159
|
+
If we want our resulting string doesn't fulfill the pattern we supply, then we'll use the symbol ! at the beginning
|
160
|
+
|
161
|
+
Examples:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
"!4:XN/x/".gen # a$aaa, FF9B, j4DDDem, as, 2345
|
165
|
+
|
166
|
+
"!10:N".gen # 123, 34899Add34, 3434234234234008, AAFj#kd2x
|
167
|
+
```
|
168
|
+
|
169
|
+
### Generate a string with specific expected errors
|
170
|
+
|
171
|
+
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)
|
172
|
+
|
173
|
+
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
|
174
|
+
|
175
|
+
:length: wrong length, minimum or maximum
|
176
|
+
:min_length: wrong minimum length
|
177
|
+
:max_length: wrong maximum length
|
178
|
+
:value: wrong resultant value
|
179
|
+
:required_data: the output string won't include all necessary required data. It works only if required data supplied on the pattern.
|
180
|
+
: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.
|
181
|
+
:string_set_not_allowed: it will include one or more characters that are not supposed to be on the string.
|
182
|
+
|
183
|
+
Examples:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
"10-20:N".gen errors: [:min_length]
|
187
|
+
#> 627, 098262, 3408
|
188
|
+
|
189
|
+
"20:N".gen errors: [:length, :value]
|
190
|
+
#> |13, tS1b)r-1)<RT65202eTo6bV0g~, 021400323<2ahL0NP86a698063*56076
|
191
|
+
|
192
|
+
"10:L/n/".gen errors: [:value]
|
193
|
+
#> 1hwIw;v{KQ, mpk*l]!7:!, wocipgZt8@
|
194
|
+
```
|
195
|
+
|
196
|
+
### Validate if a string is following a pattern
|
197
|
+
|
198
|
+
If you need to validate if a specific text is fulfilling the pattern you can use the validate method.
|
199
|
+
|
200
|
+
If a string pattern supplied and no other parameters supplied the output will be an array with the errors detected.
|
201
|
+
|
202
|
+
|
203
|
+
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
|
204
|
+
|
205
|
+
In case an array of patterns supplied it will return only true or false
|
206
|
+
|
207
|
+
Examples:
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
#StringPattern class
|
211
|
+
StringPattern.validate((text: "This text will be validated", pattern: :"10-20:Xn")
|
212
|
+
#> [:max_length, :length, :value, :string_set_not_allowed]
|
213
|
+
|
214
|
+
#String class
|
215
|
+
"10:N".validate "333444"
|
216
|
+
#> [:min_length, :length]
|
217
|
+
|
218
|
+
#Symbol class
|
219
|
+
:"10:N".validate("333444")
|
220
|
+
#> [:min_length, :length]
|
221
|
+
|
222
|
+
#Array class
|
223
|
+
["5:L","3:xn","4-10:n"].validate "DjkljFFc343444390"
|
224
|
+
#> false
|
225
|
+
```
|
226
|
+
|
227
|
+
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).
|
228
|
+
|
229
|
+
In this case, the validate method will return true or false.
|
230
|
+
|
231
|
+
Examples:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
"10:N".val "3445", errors: [:min_length]
|
235
|
+
#> true
|
236
|
+
|
237
|
+
"10:N/[09]/".validate "4434039440", errors: [:value]
|
238
|
+
#> false
|
239
|
+
|
240
|
+
"10-12:XN/x/".validate "FDDDDDAA343434", errors: [:max_length, :required_data]
|
241
|
+
#> true
|
242
|
+
```
|
243
|
+
|
244
|
+
### Configure
|
245
|
+
|
246
|
+
#### SP_ADD_TO_RUBY
|
247
|
+
|
248
|
+
This gem adds the methods generate (alias: gen) and validate (alias: val) to the Ruby classes: String, Array, and Symbol.
|
249
|
+
|
250
|
+
Also adds the method generate (alias: gen) to Kernel. By default (true) it is always added.
|
251
|
+
|
252
|
+
In case you don't want to be added, just before requiring the library set:
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
SP_ADD_TO_RUBY = false
|
256
|
+
require 'string_pattern'
|
257
|
+
```
|
258
|
+
|
259
|
+
In case it is set to true (default) then you will be able to use:
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
require 'string_pattern'
|
263
|
+
|
264
|
+
#String object
|
265
|
+
"20-30:@".gen
|
266
|
+
#>dkj34MljjJD-df@jfdluul.dfu
|
267
|
+
|
268
|
+
"10:L/N/[/-./%d%]".validate("12ds6f--.s")
|
269
|
+
#>[:value, :string_set_not_allowed]
|
270
|
+
|
271
|
+
"20-40:@".validate(my_email)
|
272
|
+
|
273
|
+
#Kernel
|
274
|
+
gen "10:N"
|
275
|
+
#>3433409877
|
276
|
+
|
277
|
+
#Array object
|
278
|
+
"(,3:N,) ,3:N,-,2:N,-,2:N".split(",").generate
|
279
|
+
#>(937) 980-65-05
|
280
|
+
|
281
|
+
%w{( 3:N ) 1:_ 3:N - 2:N - 2:N}.gen
|
282
|
+
#>(045) 448-63-09
|
283
|
+
|
284
|
+
["1:L", "5-10:LN", "-", "3:N"].gen
|
285
|
+
#>zqWihV-746
|
286
|
+
```
|
287
|
+
|
288
|
+
#### national_chars
|
289
|
+
|
290
|
+
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
|
291
|
+
|
292
|
+
```ruby
|
293
|
+
StringPattern.national_chars = (('a'..'z').to_a + ('A'..'Z').to_a).join + "áéíóúÁÉÍÓÚüÜñÑ"
|
294
|
+
"10-20:Tn".gen #>AAñ34Ef99éNOP
|
295
|
+
```
|
296
|
+
|
297
|
+
#### optimistic
|
298
|
+
|
299
|
+
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.
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
StringPattern.optimistic = false
|
303
|
+
["5:X","fixedtext", "3:N"].generate
|
304
|
+
#>5:Xfixedtext3:N
|
305
|
+
[:"5:X","fixedtext", :"3:N"].generate
|
306
|
+
#>AUJKJfixedtext454
|
307
|
+
|
308
|
+
StringPattern.optimistic = true
|
309
|
+
["5:X","fixedtext", "3:N"].generate
|
310
|
+
#>KKDMEfixedtext344
|
311
|
+
[:"5:X","fixedtext", :"3:N"].generate
|
312
|
+
#>SAAERfixedtext988
|
313
|
+
```
|
314
|
+
|
315
|
+
## Contributing
|
316
|
+
|
317
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.
|
318
|
+
|
319
|
+
|
320
|
+
## License
|
321
|
+
|
322
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
323
|
+
|