This gem can:
- create random int, float, string, and symbols
- create a random value of random type
- create an array of random values
- create a hash of random key and values
Who knows. I have made it when I was creating a code with functional style and was working mainly with hashes as a data containers and I wanted some configurable generator of random hashes for my unit tests.
Add this line to your application's Gemfile:
gem 'bsielski_value_generator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bsielski_value_generator
Reqiure proper class.
require "v_gen/int_gen"
Use it.
random_int = VGen::IntGen.new.call
# or
int_gen = VGen::IntGen.new
random_int = int_gen.call
All generators have just one public method: #call.
require "v_gen/int_gen"
VGen::IntGen.new # => new_generator
Optionally paramaters:
- range - is the range from which the number is randomly generated. Default is
(0..10)
.
VGen::IntGen.new.call # => new_int
require "v_gen/float_gen"
VGen::FloatGen.new # => new_generator
Optionally paramaters:
- range - is the range from which the number is randomly generated. Default is
(-10..10)
.
VGen::FloatGen.new.call # => new_float
require "v_gen/letter_gen"
VGen::LetterGen.new # => new_generator
Optionally paramaters:
- only: - an array (or range) of objects from which one randomly chosen is returned. Default is
("A".."Z").to_a + ("a".."z").to_a
. - except: - an array (or range) that is substracted from only: array (or range). Default is
[]
.
VGen::LetterGen.new.call # => new_letter
require "v_gen/lower_letter_gen"
VGen::LowerLetterGen.new # => new_generator
Optionally paramaters:
- 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). - except: - an array (or range) that is substracted from only: array (or range). Default is
[]
.
VGen::LowerLetterGen.new.call # => new_lower_letter
require "v_gen/upper_letter_gen"
VGen::UpperGen.new # => new_generator
Optionally paramaters:
- only: - an array (or range) of objects from which one randomly chosen is returned. Default is
("A".."Z")
. - except: - an array (or range) that is substracted from only: array (or range). Default is
[]
.
VGen::UpperLetterGen.new.call # => new_upper_letter
This generator returns random lower letters with with taking into account the frequency of occurrence in English language.
require "v_gen/typical_letter_gen"
VGen::TypicalLetterGen.new # => new_generator
VGen::TypicalLetterGen.new.call # => new_letter
require "v_gen/char_gen"
VGen::CharGen.new # => new_generator
Optionally paramaters:
- only: - an array (or range) of objects from which one randomly chosen is returned. Default is:
[
"`", "~", "!", "@", "#", "$", "%", "^", "&",
"*", "(", ")", "-", "_", "+", "=", "[", "{",
"]", "}", "\\", "|", ";", ":", "'", "\"", "<",
",", ">", ".", "?", "/", " "
] + ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
- except: - an array (or range) that is substracted from only: array (or range). Default is
[]
.
VGen::CharGen.new.call # => new_char
This generator returns random strings.
require "v_gen/string_gen"
VGen::StringGen.new # => new_generator
Optionally paramaters:
- char_gen: - is a generator used to creating a characters for strings. Default is
VGen::LetterGen.new
. - length: - possible string length as a range (for random length) or an int (for fixed length).
- except: - strings forbidden to generate. Default is
[]
.
VGen::StringGen.new.call # => new_string
This generator returns random lowercased strings sometimes with underscore in the middle of the string.
require "v_gen/var_word_gen"
VGen::VarWordGen.new # => new_generator
Optionally paramaters:
- letter_gen: - is a generator used to creating a letters for words. Default is
VGen::TypicalLetterGen.new
. - length: - possible word length as a range (for random length) or an int (for fixed length).
- except: - words forbidden to generate. Default is
[]
.
VGen::VarWordGen.new.call # => new_word
This generator returns random lowercased symbols sometimes with underscore in the middle of the string.
require "v_gen/keyword_gen"
VGen::KeywordGen.new # => new_generator
Optionally paramaters:
- word_gen: - is a generator used to creating a string, which is converted into a symbos. Default is
VGen::VarWordGen.new
.
VGen::KeywordGen.new.call # => new_keyword
This generator returns an array of random values.
require "v_gen/array_gen"
VGen::ArrayGen.new # => new_generator
Optionally paramaters:
- min: - is a minimum size of a generated arrays. Default is
4
. - max: - is a maximum size of a generated arrays. Default is
9
. - length: - possible array length as a range (for random length) or an int (for fixed length).
- gens: - are generators used randomly to generate values. Default is
[ proc {Random.new.rand} ]
. - uniq: - if truthy then generated arrays have unique elements. Default is
false
.
VGen::ArrayGen.new.call # => new_array
This generator returns a hash of random keys and values.
require "v_gen/hash_gen"
VGen::HashGen.new # => new_generator
Optionally paramaters:
- min: - is a minimum size of a generated hashes. Default is
4
. - max: - is a maximum size of a generated hashes. Default is
8
. - length: - possible hash length as a range (for random length) or an int (for fixed length).
- key_gens: - are generators used randomly to generate keys. Default is
[ proc {Random.new.rand(0..100)} ]
. - value_gens: - are generators used randomly to generate values. Default is
[ proc {Random.new.rand} ]
. - 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
{}
.
VGen::HashGen.new.call # => new_array
This generator returns one value.
require "bsielski_v_gen/int_gen"
VGen::WhateverGen.new # => new_generator
Optionally paramaters:
- gens: - are generators used randomly to generate values. Default is
[ proc {Random.new.rand} ]
.
VGen::WhateverGen.new.call # => new_array
Check "examples" folder.
- Some easy to use generators with default parameters
The gem is available as open source under the terms of the MIT License.