public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
100644 125 lines (89 sloc) 5.527 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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, "MyItem"
        field :owner_id, {:user => 'id'}
    end
    
    class UserFactory
        include Mack::Data::Factory
        
        field :username, "planters", :length => 25, :content => :alpha
        field :password, "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.
 
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 string content, you can specify :content to either :alpha or :alphanumeric, and you can also specify the
:length, :min_length or :max_length.
 
For all fields, you can specify :immutable to true, which means for all instances created, the content for that field
will not change.
 
Other supported content types:
  - :email --> generate random email address
  - :firstname --> generate first name
  - :lastname --> generate last name
  - :name --> generate full name
  - :phone --> generate phone number
  - :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
  
** 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.
 
** 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, "planters", :length => 25, :content => :alpha
        field :password, "roastedPeanuts", :immutable => true
        
        scope_for(:long_username) do
            field :username, "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, "planters" { |def_value, rules, index| "#{def_value}#{index}" }
        field :password, "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.