Mack-data_factory
== Summary
Utility to create faux data.
== Installation
sudo gem install mack-data_factory
== Using The Gem
** Constructing Factory
For each model that you want to produce, you will need to define a factory class.
Let's say that I have 2 models: Item and User, and Item belongs to user. So the factories will look like the following:
class ItemFactory
include Mack::Data::Factory
field :title, :default => "MyItem"
association :owner_id, {:user => 'id'}
end
class UserFactory
include Mack::Data::Factory
field :username, :default => "planters", :length => 25, :content => :alpha
field :password, :default => "roastedPeanuts", :immutable => true
end
So, the 2 classes above defined the factory for item and user. As you can see, each factory will need to explicitly list all the fields that it will populate, and for each field, you can define rules on how the content is generated.
Important: For each field defined in the factory, you will need to make sure that there's a corresponding setter
method in the model class (e.g. Item class is required to have title= and owner_id=).
For numeric content, you can specify the start number and end number, and the content generator will generate a random number between that range.
For all fields, you can specify :immutable to true, which means for all instances created, the default value for that field will not change.
To define a default value for a field, you use the :default options (e.g. field :username, :default => 'planters')
Supported content types:
Strings and Numbers
- :alpha --> alphabets. rules: [:length, :min_length, :max_length]
- :alphanumeric --> alphabets and number. rules: same as :alpha
- :numeric --> numbers [optional, because if the field's default value is number, its content type will automatically set to numeric)
Time and Money
- :time --> generate random time object. rules: [:start_time, :end_time]. It will generate random time between the given start and end time if available, otherwise it'll generate random time between 'now' and 1 day from 'now'
- :money --> generate random amount of money. rules: [:min, :max]. It will generate random money amount (of BigDecimal type) between the given min and max amount.
Internet related content
- :email --> generate random email address
- :username --> generate random username
- :domain --> generate random domain name
Name related info
- :firstname --> generate first name
- :lastname --> generate last name
- :name --> generate full name
Address related info
- :city --> generate city name
- :streetname --> generate street name
- :state --> generate state. rules: [:country --> :us or :uk, :abbr --> true if you want a abbreviated state name (us only)]
- :zipcode --> generate zipcode. rules: [:country --> :us or :uk]
- :phone --> generate phone number
Company info
- :company --> generate company name. rules: [:include_bs --> include sales tag line]
example: field, "", :content => :company, :include_bs => true
could generate something like:
Fadel-Larkin
monetize cross-media experiences
** Association
class ItemFactory
include Mack::Data::Factory
field :title, :default => "MyItem"
association :owner_id, {:user => 'id'}, :first
end
class UserFactory
include Mack::Data::Factory
field :username, :default => "planters", :length => 25, :content => :alpha
field :password, :default => "roastedPeanuts", :immutable => true
end
In the above example, the ItemFactory define the owner_id field as an association to {:user => 'id'}. When the factory is run, it will generate value for owner_id as 'the id of the user #x'. You can also pass in rules to define which instance of user that the id will be retrieved from. The rules are: :first, :last, :random, and :spread. The default is :spread. Definition of the association rules:
:first --> always associate with the first object that this object belongs to. If there are 10 users [id = 0 - 10], then the item will always get associated with user #0 (i.e. item's owner_id always == 0)
:last --> always associate with the last object that this object belongs to. If there are 10 users [id = 0 - 10], then the item will always get associated with user #10 (i.e. item's owner_id always == 9)
:random --> always associate with the Nth object that this object belongs to (and N is random). If there are 10 users [id = 0 - 10], then the item will get associated with user #n (i.e. item's owner_id == rand(10))
:spread --> spread the association. If there are 3 users [id = 0 - 2], then the items' association will be spread out (i.e. 6 items will have id--sequentially--[0, 1, 2, 0, 1, 2])
** Pumping Data
To create objects from the factory is very easy, just call create method on that factory and tell it how many items
you want to create.
Note that if your factory has dependencies to other model (like the ItemFactory in the example), then make sure you
have created the model that it's depending on first.
The create method will return constructed objects. If there's only 1 instance constructed, then it will return that object;
if more than one then it will return an array of objects.
** Creating Factories Execution Chain
In some instances, you may want to create an execution chain of a bunch of factories. In the above example: the UserFactory
has to be run before the ItemFactory.
If that's the case, you can create factory chain that you can execute later.
factories(:my_cool_factory) do
UserFactory.create(1)
ItemFactory.create(1000)
end
Once you have defined a named factory chain, you can execute that any time, by calling:
run_factories(:my_cool_factory)
** Scoping
In other instances, you may want different settings in the factory for different test scope. You can achieve this by doing
the following:
class UserFactory
include Mack::Data::Factory
field :username, :default => "planters", :length => 25, :content => :alpha
field :password, :default => "roastedPeanuts", :immutable => true
scope_for(:long_username) do
field :username, :default => "planters", :length => 128, :content => :alpha
end
end
The above example defined a scoping for "long_username", which you can use by calling:
UserFactory.create(100, :long_username)
When a scope is defined and called, the field defined in the block will overwrite the default field listing
in that class. Scopes in the factory is independent to each other, and one scope cannot affect the others.
** Custom content generator
In some cases, the default content generators that are provided by this framework don't fit your needs.
To accommodate this situation, you can provide a custom content generator for each field you defined by
passing in a proc.
Example:
I'm creating a users, but I don't want the username be totally random, instead I want it to use the default
name I provide, and append 0-n at the end of the string to represent the Nth user in the list. Here's how to
accomplish that:
class UserFactory
include Mack::Data::Factory
field :username, :default => "planters" do |def_value, rules, index|
"#{def_value}#{index}"
end
field :password, :default => "roastedPeanuts", :immutable => true
end
== Contact
Please mail bugs, suggestions, and patches to darsono.sutedja@gmail.com
== Copyright Notices
Copyright (c) 2008 Darsono Sutedja
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.