bsielski_value_generator 1.0.1 → 1.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: 2d6bf8e7eab4c6c2df03480e8fdf0fda7ae423ae2c3c521c2978f09e5d933b35
4
- data.tar.gz: 2690611ff53b74df4303eb3d47185c6cbf811e9398e174e5a194cafdcf09d2f1
3
+ metadata.gz: 8d9a3b8d67dba32050aa9fa381fc01c5ab17271c6ab1fa32d9fab0edec461f1c
4
+ data.tar.gz: d245b33cc4b1ae56f6ce5b316be8bfe3bc77bbb510f654c8386eae56147b5b0b
5
5
  SHA512:
6
- metadata.gz: 5c0cf9430f04c3c5de34dee49b520a4d350108b3ce175b98ca578d039db20c6435dec4eed2c594cb798dcd3773b0e1a0885d68715328458de8230a64e551c463
7
- data.tar.gz: 805f87aa4a4447367ee1a132ca3addb59f159634cb0cf84938e9b58958df41c39774971ad47d285dea70725ad982d9f006d13a71ee9b943b6c7e48b88cbfbcc6
6
+ metadata.gz: 17f230e9f5ff70ae372c8b389056c6a000d1c2ff59344800f51ef514a2dffe1cc0e2493c8f00ad7ba140f0f87a50bb0d36737685b852c8c0ad20f635521bb96b
7
+ data.tar.gz: e67eed2246e46ce31c5e4314d1aa1a8b4bbf5e33656a0b7ad4a97906abec94c8403a339e0e78fdee537367775d8d2484d9fb97240291e098c560c8a45a73b67b
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
- --format documentation
2
1
  --color
3
2
  --require spec_helper
data/README.md CHANGED
@@ -45,250 +45,262 @@ int_gen = VGen::IntGen.new
45
45
  random_int = int_gen.call
46
46
  ```
47
47
 
48
- All generators have just one public method: `#call`.
49
-
48
+ All generators have just one public method: ***#call***.
50
49
 
51
50
 
52
51
  ## API
53
52
 
54
53
 
55
-
56
- ### VGen::IntGen
54
+ ### Class VGen::IntGen
57
55
 
58
56
  ```ruby
59
57
  require "v_gen/int_gen"
60
58
  ```
61
59
 
62
- #### Class Public Methods
63
-
64
- ##### `new(range=(0..10)) → new_generator`
60
+ #### Constructor
65
61
 
66
- *range* is the range from which the number is randomly generated.
62
+ ```ruby
63
+ VGen::IntGen.new # => new_generator
64
+ ```
67
65
 
68
- #### Public Instance Methods
66
+ Optionally paramaters:
69
67
 
70
- ##### `call new_int`
68
+ - **_range_** - is the range from which the number is randomly generated. Default is `(0..10)`.
69
+
71
70
 
71
+ #### Call
72
72
 
73
+ ```ruby
74
+ VGen::IntGen.new.call # => new_int
75
+ ```
73
76
 
74
- ### VGen::FloatGen
77
+ ### Class VGen::FloatGen
75
78
 
76
79
  ```ruby
77
80
  require "v_gen/float_gen"
78
81
  ```
82
+ #### Constructor
79
83
 
80
- #### Class Public Methods
81
-
82
- ##### `new(range=(-10..10)) → new_generator`
83
-
84
- See VGen::IntGen.new for details about *range* argument.
84
+ ```ruby
85
+ VGen::FloatGen.new # => new_generator
86
+ ```
85
87
 
86
- #### Public Instance Methods
88
+ Optionally paramaters:
87
89
 
88
- ##### `call new_float`
90
+ - **_range_** - is the range from which the number is randomly generated. Default is `(-10..10)`.
89
91
 
92
+ #### Call
90
93
 
94
+ ```ruby
95
+ VGen::FloatGen.new.call # => new_float
96
+ ```
91
97
 
92
- ### VGen::LetterGen
98
+ ### Class VGen::LetterGen
93
99
 
94
100
  ```ruby
95
101
  require "v_gen/letter_gen"
96
102
  ```
97
103
 
98
- #### Class Public Methods
104
+ #### Constructor
99
105
 
100
- ##### `new(only: (("A".."Z").to_a + ("a".."z").to_a), except: []) → new_generator`
101
-
102
- *only* an array (or range) of objects from which one randomly chosen is returned.
103
-
104
- *except* an array (or range) that is substracted from *only* array (range).
106
+ ```ruby
107
+ VGen::LetterGen.new # => new_generator
108
+ ```
105
109
 
106
- #### Public Instance Methods
110
+ Optionally paramaters:
107
111
 
108
- ##### `call new_object`
112
+ - **_only:_** - an array (or range) of objects from which one randomly chosen is returned. Default is `("A".."Z").to_a + ("a".."z").to_a`.
113
+ - **_except:_** - an array (or range) that is substracted from **_only:_** array (or range). Default is `[]`.
109
114
 
115
+ #### Call
110
116
 
117
+ ```ruby
118
+ VGen::LetterGen.new.call # => new_letter
119
+ ```
111
120
 
112
- ### VGen::LowerLetterGen
121
+ ### Class VGen::LowerLetterGen
113
122
 
114
123
  ```ruby
115
124
  require "v_gen/lower_letter_gen"
116
125
  ```
117
126
 
118
- #### Class Public Methods
119
-
120
- ##### `new(only: ("A".."Z"), except: []) → new_generator`
121
-
122
- See VGen::LetterGen.new for details about *only* and *except* arguments.
123
-
124
- The object in *only* and *except* arrays or ranges must respond to `#downcase` method.
127
+ #### Constructor
125
128
 
126
- #### Public Instance Methods
127
-
128
- ##### `call → new_lowercased_object`
129
+ ```ruby
130
+ VGen::LowerLetterGen.new # => new_generator
131
+ ```
129
132
 
133
+ Optionally paramaters:
130
134
 
135
+ - **_only:_** - an array (or range) of objects from which one randomly chosen is returned. Default is `("A".."Z")` (those letters are downcased anyway by the class).
136
+ - **_except:_** - an array (or range) that is substracted from **_only:_** array (or range). Default is `[]`.
131
137
 
132
- #### Example
138
+ #### Call
133
139
 
134
140
  ```ruby
135
- lower_letter_gen = VGen::LowerLetterGen.new(only: ("A".."Z"))
136
- lower_letter_gen.call # => "j"
137
- lower_letter_gen.call # => "e"
141
+ VGen::LowerLetterGen.new.call # => new_lower_letter
138
142
  ```
139
143
 
144
+
140
145
  ### VGen::UpperLetterGen
141
146
 
142
147
  ```ruby
143
148
  require "v_gen/upper_letter_gen"
144
149
  ```
145
150
 
146
- #### Class Public Methods
151
+ #### Constructor
147
152
 
148
- ##### `new(only: ("A".."Z"), except: []) → new_generator`
149
-
150
- See VGen::LetterGen.new for details about *only* and *except* arguments.
151
-
152
- The object in *only* and *except* arrays or ranges must respond to `#upcase` method.
153
+ ```ruby
154
+ VGen::UpperGen.new # => new_generator
155
+ ```
153
156
 
154
- #### Public Instance Methods
157
+ Optionally paramaters:
155
158
 
156
- ##### `call new_uppercased_object`
159
+ - **_only:_** - an array (or range) of objects from which one randomly chosen is returned. Default is `("A".."Z")`.
160
+ - **_except:_** - an array (or range) that is substracted from **_only:_** array (or range). Default is `[]`.
157
161
 
158
- #### Example
162
+ #### Call
159
163
 
160
164
  ```ruby
161
- upper_letter_gen = VGen::UpperLetterGen.new(only: ("A".."Z"))
162
- upper_letter_gen.call # => "I"
163
- upper_letter_gen.call # => "D"
165
+ VGen::UpperLetterGen.new.call # => new_upper_letter
164
166
  ```
165
167
 
166
168
 
169
+ ### Class VGen::TypicalLetterGen
167
170
 
168
- ### VGen::TypicalLetterGen
171
+ This generator returns random lower letters with with taking into account the frequency of occurrence in English language.
169
172
 
170
173
  ```ruby
171
174
  require "v_gen/typical_letter_gen"
172
175
  ```
173
176
 
174
- #### Class Public Methods
177
+ #### Constructor
175
178
 
176
- ##### `new → new_generator`
177
-
178
- #### Public Instance Methods
179
+ ```ruby
180
+ VGen::TypicalLetterGen.new # => new_generator
181
+ ```
179
182
 
180
- ##### `call → new_letter`
183
+ #### Call
181
184
 
182
- It returns a random lowercased letter with taking into account the frequency of occurrence in English language.
185
+ ```ruby
186
+ VGen::TypicalLetterGen.new.call # => new_letter
187
+ ```
183
188
 
184
189
 
190
+ ### Class VGen::VarWordGen
185
191
 
186
- ### VGen::VarWordGen
192
+ This generator returns random lowercased strings sometimes with underscore in the middle of the string.
187
193
 
188
194
  ```ruby
189
195
  require "v_gen/var_word_gen"
190
196
  ```
191
197
 
192
- #### Class Public Methods
193
-
194
- ##### `new(letter_gen: TypicalLetterGen.new, lenght: (4..9), except: []) → new_generator`
195
-
196
- *letter_gen* is a generator used to creating a letters for words.
198
+ #### Constructor
197
199
 
198
- *length* possible word length as a range.
200
+ ```ruby
201
+ VGen::VarWordGen.new # => new_generator
202
+ ```
199
203
 
200
- *except* words forbidden to generate.
204
+ Optionally paramaters:
201
205
 
202
- #### Public Instance Methods
206
+ - **_letter_gen:_** - is a generator used to creating a letters for words. Default is `VGen::TypicalLetterGen.new`.
207
+ - **_length:_** - possible word length as a range (for random length) or an int (for fixed length).
208
+ - **_except:_** - words forbidden to generate. Default is `[]`.
203
209
 
204
- ##### `call(lenght: (4..9), except: []) → new_random_word`
210
+ #### Call
205
211
 
206
- It returns a random lowercased word sometimes with underscore in the middle.
212
+ ```ruby
213
+ VGen::VarWordGen.new.call # => new_word
214
+ ```
207
215
 
208
216
 
217
+ ### Class VGen::KeywordGen
209
218
 
210
- ### VGen::KeywordGen
219
+ This generator returns random lowercased symbols sometimes with underscore in the middle of the string.
211
220
 
212
221
  ```ruby
213
222
  require "v_gen/keyword_gen"
214
223
  ```
215
224
 
216
- #### Class Public Methods
225
+ #### Constructor
217
226
 
218
- ##### `new(word_gen: VarWordGen.new) → new_generator`
219
-
220
- *word_gen* is a generator used to creating a word that will be converted to a symbol.
227
+ ```ruby
228
+ VGen::KeywordGen.new # => new_generator
229
+ ```
221
230
 
222
- #### Public Instance Methods
231
+ Optionally paramaters:
223
232
 
224
- ##### `call new_random_word`
233
+ - **_word_gen:_** - is a generator used to creating a string, which is converted into a symbos. Default is `VGen::VarWordGen.new`.
225
234
 
226
- It returns a random lowercased word sometimes with underscore in the middle.
235
+ #### Call
227
236
 
237
+ ```ruby
238
+ VGen::KeywordGen.new.call # => new_keyword
239
+ ```
228
240
 
241
+ ### Class VGen::ArrayGen
229
242
 
230
- ### VGen::ArrayGen
243
+ This generator returns an array of random values.
231
244
 
232
245
  ```ruby
233
246
  require "v_gen/array_gen"
234
247
  ```
235
248
 
236
- #### Class Public Methods
237
-
238
- ##### `new(min: 4, max: 8, gens: [proc {Random.new.rand}]) → new_generator`
249
+ #### Constructor
239
250
 
240
- *min* is a minimum size of a generated arrays.
241
-
242
- *max* is a maximum size of a generated arrays.
251
+ ```ruby
252
+ VGen::ArrayGen.new # => new_generator
253
+ ```
243
254
 
244
- *gens* are generators used randomly to generate values.
255
+ Optionally paramaters:
245
256
 
246
- #### Public Instance Methods
257
+ - **_min:_** - is a minimum size of a generated arrays. Default is `4`.
258
+ - **_max:_** - is a maximum size of a generated arrays. Default is `9`.
259
+ - **_length:_** - possible array length as a range (for random length) or an int (for fixed length).
260
+ - **_size:_** - alias for **_length:_**.
261
+ - **_gens:_** - are generators used randomly to generate values. Default is `[ proc {Random.new.rand} ]`.
262
+ - **_uniq:_** - if truthy then generated arrays have unique elements. Default is `false`.
247
263
 
248
- ##### `call → new_random_word`
264
+ #### Call
249
265
 
250
- It returns an array of random values.
266
+ ```ruby
267
+ VGen::ArrayGen.new.call # => new_array
268
+ ```
251
269
 
252
270
 
271
+ ### Class VGen::HashGen
253
272
 
254
- ### VGen::HashGen
273
+ This generator returns a hash of random keys and values.
255
274
 
256
275
  ```ruby
257
276
  require "v_gen/hash_gen"
258
277
  ```
259
278
 
260
- #### Class Public Methods
261
-
262
- ##### `new(min: 4, max: 8, key_gens: [proc {Random.new.rand(0..100)}], value_gens: [proc {Random.new.rand}]) → new_generator`
263
-
264
- *min* is a minimum size of a generated arrays.
265
-
266
- *max* is a maximum size of a generated arrays.
267
-
268
- *key_gens* are generators used randomly to generate keys.
269
-
270
- *value_gens* are generators used randomly to generate values.
271
-
272
- #### Public Instance Methods
273
-
274
- ##### `call → new_random_word`
275
-
276
- It returns a hash of random keys and values.
277
-
278
-
279
+ #### Constructor
279
280
 
281
+ ```ruby
282
+ VGen::HashGen.new # => new_generator
283
+ ```
280
284
 
285
+ Optionally paramaters:
281
286
 
287
+ - **_min:_** - is a minimum size of a generated hashes. Default is `4`.
288
+ - **_max:_** - is a maximum size of a generated hashes. Default is `8`.
289
+ - **_length:_** - possible hash length as a range (for random length) or an int (for fixed length).
290
+ - **_size:_** - alias for **_length:_**.
291
+ - **_key_gens:_** - are generators used randomly to generate keys. Default is `[ proc {Random.new.rand(0..100)} ]`.
292
+ - **_value_gens:_** - are generators used randomly to generate values. Default is `[ proc {Random.new.rand} ]`.
293
+ - **_with:_** - is a hash that must be included in the generated hash. In other words it is a obligatory set of key and values pairs. Default is `{}`.
282
294
 
283
- ## To do features
295
+ #### Call
284
296
 
285
- - Better API
286
- - Some easy to use generators with default parameters
297
+ ```ruby
298
+ VGen::HashGen.new.call # => new_array
299
+ ```
287
300
 
288
301
  ## To do features
289
302
 
290
- - Better API
291
- - Some easy to use generators with default parameters
303
+ - Some easy to use generators with default parameters
292
304
 
293
305
  ## License
294
306
 
@@ -1,20 +1,42 @@
1
1
  module VGen
2
2
  class ArrayGen
3
3
  def initialize(
4
+ uniq: false,
4
5
  min: 4,
5
- max: 8,
6
+ max: 9,
7
+ length: nil,
8
+ size: nil,
6
9
  gens: [ proc {Random.new.rand} ]
7
10
  )
11
+ @uniq = uniq
12
+ @length = length || size || (min..max)
8
13
  @gens = gens
9
14
  @min = min
10
15
  @max = max
11
16
  end
12
17
 
13
18
  def call()
14
- arr = Array.new(Random.new.rand(@min..@max)) {
15
- @gens.sample
19
+ used_values = []
20
+ Array.new(array_length) {
21
+ loop do
22
+ candidate_value = @gens.sample.call
23
+ if used_values.include?(candidate_value)
24
+ next
25
+ else
26
+ used_values << candidate_value if @uniq
27
+ break candidate_value
28
+ end
29
+ end
16
30
  }
17
- arr.map(&:call)
31
+ end
32
+
33
+ private
34
+
35
+ def array_length
36
+ length = Random.new.rand(@length) if @length.is_a? Range
37
+ length = @length if @length.is_a? Integer
38
+ raise "length (size) can't be negative" if length < 0
39
+ length
18
40
  end
19
41
  end
20
42
  end
@@ -1,11 +1,16 @@
1
1
  module VGen
2
2
  class HashGen
3
3
  def initialize(
4
+ with: {},
4
5
  min: 4,
5
6
  max: 8,
7
+ length: nil,
8
+ size: nil,
6
9
  key_gens: [ proc {Random.new.rand(0..100)} ],
7
10
  value_gens: [ proc {Random.new.rand} ]
8
11
  )
12
+ @with = with
13
+ @length = length || size || (min..max)
9
14
  @key_gens = key_gens
10
15
  @value_gens = value_gens
11
16
  @min = min
@@ -13,23 +18,35 @@ module VGen
13
18
  end
14
19
 
15
20
  def call()
16
- length = Random.new.rand(@min..@max)
21
+ with_length = @with.length
17
22
  hash = Hash[
18
- Array.new(length) do
23
+ Array.new(hash_length - with_length) do
19
24
  [
20
25
  @key_gens.sample.call,
21
26
  @value_gens.sample.call
22
27
  ]
23
28
  end
24
29
  ]
25
- while hash.size < length do
26
- hash = hash.merge(
27
- {
28
- @key_gens.sample.call => @value_gens.sample.call
29
- }
30
- )
30
+ unless @with.empty?
31
+ hash.merge!(@with)
32
+ end
33
+ while hash.size < hash_length do
34
+ new_pair = {
35
+ @key_gens.sample.call => @value_gens.sample.call
36
+ }
37
+ hash = new_pair.merge(hash)
31
38
  end
39
+ hash = hash.to_a.shuffle.to_h unless @with.empty?
32
40
  hash
33
41
  end
42
+
43
+ private
44
+
45
+ def hash_length
46
+ length = Random.new.rand(@length) if @length.is_a? Range
47
+ length = @length if @length.is_a? Integer
48
+ raise "length (size) can't be negative" if length < 0
49
+ length
50
+ end
34
51
  end
35
52
  end
@@ -4,18 +4,20 @@ module VGen
4
4
  class VarWordGen
5
5
  def initialize(
6
6
  letter_gen: TypicalLetterGen.new,
7
- length: (4..9),
7
+ length: nil,
8
+ size: (4..9),
8
9
  except: []
9
10
  )
10
- @length = length
11
+ @length = length || size
11
12
  @letter_gen = letter_gen
12
13
  @except = except
13
14
  end
14
15
 
15
16
  def call()
17
+
16
18
  loop do
17
19
  word = Array.new(
18
- Random.new.rand(@length),
20
+ word_length,
19
21
  @letter_gen
20
22
  ).map(&:call).join
21
23
  if word.size > 2
@@ -26,5 +28,14 @@ module VGen
26
28
  return word unless @except.include? word
27
29
  end
28
30
  end
31
+
32
+ private
33
+
34
+ def word_length
35
+ length = Random.new.rand(@length) if @length.is_a? Range
36
+ length = @length if @length.is_a? Integer
37
+ raise "length (size) can't be negative" if length < 0
38
+ length
39
+ end
29
40
  end
30
41
  end
@@ -1,3 +1,3 @@
1
1
  module VGen
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bsielski_value_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartłomiej Sielski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-24 00:00:00.000000000 Z
11
+ date: 2018-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler