markbates (owner)

Revisions

gist: 171211 Download_button fork
public
Public Clone URL: git://gist.github.com/171211.git
Embed All Files: show embed
Ruby #
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
class User < ActiveRecord::Base
end
 
class Post < ActiveRecord::Base
  has_many :comments
end
 
class Comment < ActiveRecord::Base
  belongs_to :post
end
 
# ---- Basics ---- #
 
KathyLee::User do
  property :username, fake(:username)
  property :email, fake(:email_address)
  property :home_page_url, fake(:url)
  property :fullname, fake(:first_name, ' ', :last_name)
  property :login_count, 0
end
 
# Creates a new User:
user = KathyLee::User.new
user.username # => 'bill.smith'
user.email # => 'bill.smith@example.org'
user.home_page_url # => 'http://www.example.com'
user.fullname # => 'Bill Smith'
user.login_count # => 0
user.new_record? # => true
 
# Creates and saves a new User:
user = KathyLee::User.create
user.username # => 'bill.smith'
user.email # => 'bill.smith@example.org'
user.home_page_url # => 'http://www.example.com'
user.fullname # => 'Bill Smith'
user.login_count # => 0
user.new_record? # => false
 
# Creates a new User:
user = KathyLee::User.new(:username => 'mark.bates', :login_count => 5)
user.username # => 'mark.bates'
user.email # => 'bill.smith@example.org'
user.home_page_url # => 'http://www.example.com'
user.fullname # => 'Bill Smith'
user.login_count # => 5
user.new_record? # => true
 
# ---- Bulk creating ---- #
 
# Creates 10 new users:
users = KathyLee::User.sweatshop(10) do |user, index|
  user.login_count = index
end
 
# Creates and saves 10 new users:
users = KathyLee::User.sweatshop!(10) do |user, index|
  user.login_count = index
end
 
# ---- Associations ---- #
 
KathyLee::Post do
  property :title, fake(:title)
  property :body, fake(:lorem)
  
  has_many :comments, :size => 10
end
 
KathyLee::Comment do
  property :post_id
  property :body, fake(:lorem)
  
  belongs_to :post
end
 
post = KathyLee::Post.new
post.title # => 'my title'
post.body # => 'Lorem ipsum....'
post.comments # => [<comment>, ...]
post.comments.size # => 10
 
comment = KathyLee::Comment.create(:post => KathyLee::create(Post))
comment.body # => 'Lorem ipsum...'
comment.post.title # => 'my title'
 
 
# ---- Creating new generators ---- #
 
KathyLee.fake(:yes_or_no) do
  return ['yes', 'no'].sort_by({rand}).first
end
 
KathyLee::SomeModel do
  property :state, fake(:yes_or_no)
end