Rando: a random sequence generator for Machinist, Factory Girl, and other mocking frameworks
Rando: a random sequence generator for Machinist, Factory Girl, and other mocking frameworks
Otherwise known as have your Sham and Machinist 2.
A bit of a history:
I’m a big fan of Machinist. But I wasn’t a huge fan of one of the changes that came with Machinist 2: the removal of Sham.
Simply put, satisfying uniqueness constraints by simply inputting random numbers into a field value doesn’t always cut it. Sometimes you need
a unique ip address, a unique first name (with no numeric characters), or a unique latitude/longitude (which is not necessarily easy to generate
simply by using a serial number).
a unique ip address, a unique first name (with no numeric characters), or a unique latitude/longitude (which is not necessarily easy to generate
simply by using a serial number).
So, in classic OS style, I popped open the old version of Sham in Machinist 1, and did a little re-write from the ground up.
What Rando Does
Rando works in a similar way to Sham. (It works great with Faker, too). Here’s a usage example:
First, you are going to want to define some commonly used data types for Rando:
Rando.define do
latitude { (180 * rand - 90) + rand }
longitude { (360 * rand - 180) + rand }
email { Faker::Internet.email } #requires Faker gem
end
You can then call Rando in your Blueprints/Factories:
Business.blueprint do
latitude { Rando.latitude }
longitude { Rando.longitude }
email { Rando.email }
end
Rando will execute each block as you define it, and change the random generator seed
each time. This should guarantee different results each time. (For the most part -
true ‘uniqueness detection’ will come in a later version)
each time. This should guarantee different results each time. (For the most part -
true ‘uniqueness detection’ will come in a later version)
How Rando Is Different From Sham
1. Rando does not generate the same series of values each time it is executed (i deemed this unnecessary – feel free to disagree)
2. Rando does not, yet, guarantee that every value it generates will be 100% unique. It can
only “assure” this based on the approx. variance of the output of the block you pass. For the
most part, it should be unique, but there are no checks on this, yet.
2. Rando does not, yet, guarantee that every value it generates will be 100% unique. It can
only “assure” this based on the approx. variance of the output of the block you pass. For the
most part, it should be unique, but there are no checks on this, yet.
How to download and use Rando
Right now, I haven’t cut a Gem for Rando, but will when I feel its “ready”. For now you can
simply download rando (its a simple .rb file), drop it in your directory of choice (probably lib) , and require it in your blueprints.rb or factories.rb file.
simply download rando (its a simple .rb file), drop it in your directory of choice (probably lib) , and require it in your blueprints.rb or factories.rb file.
If you have any issues/comments/concerns, please add them to the Github Issues list!
Enjoy!