Skip to content

Generators for random values (ints, strings, arrays, hashes etc.)

License

Notifications You must be signed in to change notification settings

bsielski/bsielski_value_generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bsielski Value Generator

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

Why to use it?

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.

Installation

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

Usage

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.

API

Class VGen::IntGen

require "v_gen/int_gen"

Constructor

VGen::IntGen.new # => new_generator

Optionally paramaters:

  • range - is the range from which the number is randomly generated. Default is (0..10).

Call

VGen::IntGen.new.call # => new_int

Class VGen::FloatGen

require "v_gen/float_gen"

Constructor

VGen::FloatGen.new # => new_generator

Optionally paramaters:

  • range - is the range from which the number is randomly generated. Default is (-10..10).

Call

VGen::FloatGen.new.call # => new_float

Class VGen::LetterGen

require "v_gen/letter_gen"

Constructor

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 [].

Call

VGen::LetterGen.new.call # => new_letter

Class VGen::LowerLetterGen

require "v_gen/lower_letter_gen"

Constructor

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 [].

Call

VGen::LowerLetterGen.new.call # => new_lower_letter

VGen::UpperLetterGen

require "v_gen/upper_letter_gen"

Constructor

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 [].

Call

VGen::UpperLetterGen.new.call # => new_upper_letter

Class VGen::TypicalLetterGen

This generator returns random lower letters with with taking into account the frequency of occurrence in English language.

require "v_gen/typical_letter_gen"

Constructor

VGen::TypicalLetterGen.new # => new_generator

Call

VGen::TypicalLetterGen.new.call # => new_letter

Class VGen::CharGen

require "v_gen/char_gen"

Constructor

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 [].

Call

VGen::CharGen.new.call # => new_char

Class VGen::StringGen

This generator returns random strings.

require "v_gen/string_gen"

Constructor

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 [].

Call

VGen::StringGen.new.call # => new_string

Class VGen::VarWordGen

This generator returns random lowercased strings sometimes with underscore in the middle of the string.

require "v_gen/var_word_gen"

Constructor

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 [].

Call

VGen::VarWordGen.new.call # => new_word

Class VGen::KeywordGen

This generator returns random lowercased symbols sometimes with underscore in the middle of the string.

require "v_gen/keyword_gen"

Constructor

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.

Call

VGen::KeywordGen.new.call # => new_keyword

Class VGen::ArrayGen

This generator returns an array of random values.

require "v_gen/array_gen"

Constructor

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.

Call

VGen::ArrayGen.new.call # => new_array

Class VGen::HashGen

This generator returns a hash of random keys and values.

require "v_gen/hash_gen"

Constructor

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 {}.

Call

VGen::HashGen.new.call # => new_array

Class VGen::WhateverGen

This generator returns one value.

require "bsielski_v_gen/int_gen"

Constructor

VGen::WhateverGen.new # => new_generator

Optionally paramaters:

  • gens: - are generators used randomly to generate values. Default is [ proc {Random.new.rand} ].

Call

VGen::WhateverGen.new.call # => new_array

Examples

Check "examples" folder.

To do features

  • Some easy to use generators with default parameters

License

The gem is available as open source under the terms of the MIT License.

About

Generators for random values (ints, strings, arrays, hashes etc.)

Resources

License

Stars

Watchers

Forks

Packages

No packages published