cranky 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +20 -11
- data/lib/cranky.rb +7 -3
- data/spec/cranky_spec.rb +7 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -46,13 +46,13 @@ Cranky allows you to build factories via std Ruby methods, like this...
|
|
46
46
|
# Define attributes via a hash, generate the values any way you want
|
47
47
|
define :name => "Jimmy",
|
48
48
|
:email => "jimmy#{n}@home.com", # An 'n' counter method is available to help make things unique
|
49
|
-
:role =>
|
49
|
+
:role => "pleb",
|
50
50
|
:address => default_address # Call your own helper methods to wire up your associations
|
51
51
|
end
|
52
52
|
|
53
53
|
# Easily create variations via the inherit helper, callable via Factory.build(:admin)
|
54
54
|
def admin
|
55
|
-
inherit(:user, :role =>
|
55
|
+
inherit(:user, :role => "admin")
|
56
56
|
end
|
57
57
|
|
58
58
|
# Return a default address if it already exists, or call the address factory to make one
|
@@ -92,7 +92,7 @@ So for example to create a simple user factory...
|
|
92
92
|
u = User.new
|
93
93
|
u.name = options[:name] || "Jimmy" # Use the passed in name if present, or the default
|
94
94
|
u.email = options[:email] || "jimmy#{n}@home.com" # Give each user a unique email address
|
95
|
-
u.role = options[:role] ||
|
95
|
+
u.role = options[:role] || "pleb"
|
96
96
|
u
|
97
97
|
end
|
98
98
|
|
@@ -114,7 +114,7 @@ For example here it is with the capability to automatically create a default add
|
|
114
114
|
u = User.new
|
115
115
|
u.name = options[:name] || "Jimmy"
|
116
116
|
u.email = options[:email] || "jimmy#{n}@home.com"
|
117
|
-
u.role = options[:role] ||
|
117
|
+
u.role = options[:role] || "pleb"
|
118
118
|
u.address = default_address
|
119
119
|
u
|
120
120
|
end
|
@@ -129,7 +129,7 @@ So a nice tip is to implement default associations like this (assuming you're us
|
|
129
129
|
# Return the default address if it already exists, or call the address factory to make one
|
130
130
|
def default_address
|
131
131
|
# If the default address exists, but has been cleared from the database...
|
132
|
-
@default_address = nil if @default_address && !
|
132
|
+
@default_address = nil if @default_address && !Address.exists?(@default_address.id)
|
133
133
|
@default_address ||= create(:address)
|
134
134
|
end
|
135
135
|
|
@@ -141,7 +141,7 @@ You can pass additional arguments to your factories via the overrides hash...
|
|
141
141
|
u = User.new
|
142
142
|
u.name = options[:name] || "Jimmy"
|
143
143
|
u.email = options[:email] || "jimmy#{n}@home.com"
|
144
|
-
u.role = options[:role] ||
|
144
|
+
u.role = options[:role] || "pleb"
|
145
145
|
u.address = options[:new_address] ? create(:address) : default_address
|
146
146
|
u
|
147
147
|
end
|
@@ -158,20 +158,29 @@ Most of your factories are likely to simply define a list of mimimum attribute v
|
|
158
158
|
def user
|
159
159
|
define :name => "Jimmy",
|
160
160
|
:email => "jimmy#{n}@home.com",
|
161
|
-
:role =>
|
161
|
+
:role => "pleb",
|
162
162
|
:address => default_address
|
163
163
|
end
|
164
164
|
|
165
165
|
Note that you don't have to worry about handling the overrides here, they will be applied automatically if present, just define the defaults.
|
166
166
|
|
167
|
-
The
|
167
|
+
The define argument is just a regular hash, you have complete freedom to choose how to generate the values to be passed into it.
|
168
|
+
|
169
|
+
If you like you can generate attributes with a block:
|
170
|
+
|
171
|
+
def user
|
172
|
+
define :name => "Jimmy",
|
173
|
+
:email => lambda{|u| "#{u.name.downcase}@home.com"},
|
174
|
+
:role => "pleb",
|
175
|
+
:address => default_address
|
176
|
+
end
|
168
177
|
|
169
178
|
The define method will return the object, you can grab this for additional manipulation as you would expect...
|
170
179
|
|
171
180
|
def user
|
172
181
|
u = define :name => "Jimmy",
|
173
182
|
:email => "jimmy#{n}@home.com",
|
174
|
-
:role =>
|
183
|
+
:role => "pleb",
|
175
184
|
:address => default_address
|
176
185
|
u.do_something
|
177
186
|
u # Remember to return it at the end
|
@@ -184,7 +193,7 @@ If for any reason you want to have your factory method named differently from th
|
|
184
193
|
u = define :class => :user,
|
185
194
|
:name => "Jimmy",
|
186
195
|
:email => "jimmy#{n}@home.com",
|
187
|
-
:role =>
|
196
|
+
:role => "pleb",
|
188
197
|
:address => default_address
|
189
198
|
end
|
190
199
|
|
@@ -194,7 +203,7 @@ You can inherit from other factories via the inherit method. So for example to c
|
|
194
203
|
|
195
204
|
# Called via Factory.create(:admin)
|
196
205
|
def admin
|
197
|
-
inherit(:user, :role =>
|
206
|
+
inherit(:user, :role => "admin") # Pass in any attribute overrides you want
|
198
207
|
end
|
199
208
|
|
200
209
|
=== Unique Attributes (n)
|
data/lib/cranky.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Cranky
|
2
2
|
|
3
|
-
VERSION = "0.0.
|
3
|
+
VERSION = "0.0.5"
|
4
4
|
|
5
5
|
# Dir.glob("#{File.expand_path(File.dirname(__FILE__))}/*.rb").each do |file|
|
6
6
|
# require file
|
@@ -62,11 +62,15 @@ class Cranky
|
|
62
62
|
|
63
63
|
def define(attrs={})
|
64
64
|
final_attrs = attrs.merge(@attrs.last)
|
65
|
-
#item = (attrs[:class] ? attrs[:class] : @what.last).to_s.camelcase.constantize.new
|
66
65
|
item = get_constant(attrs[:class] ? attrs[:class] : @what.last).new
|
67
66
|
final_attrs.delete(:class)
|
67
|
+
# Assign all explicit attributes first
|
68
68
|
final_attrs.each do |attr, value|
|
69
|
-
item.send("#{attr}=", value) if item.respond_to?("#{attr}=")
|
69
|
+
item.send("#{attr}=", value) if item.respond_to?("#{attr}=") && !value.respond_to?("call")
|
70
|
+
end
|
71
|
+
# Then call any blocks
|
72
|
+
final_attrs.each do |attr, value|
|
73
|
+
item.send("#{attr}=", value.call(item)) if item.respond_to?("#{attr}=") && value.respond_to?("call")
|
70
74
|
end
|
71
75
|
item
|
72
76
|
end
|
data/spec/cranky_spec.rb
CHANGED
@@ -79,6 +79,13 @@ describe Cranky do
|
|
79
79
|
Factory.build(:user).argument_received.should == nil
|
80
80
|
Factory.build(:user, :argument_supplied => true).argument_received.should == true
|
81
81
|
end
|
82
|
+
|
83
|
+
it "should allow blocks to be passed into the define method" do
|
84
|
+
Factory.build(:user, :name => "jenny", :email => lambda{ |u| "#{u.name}@home.com" }).email.should == "jenny@home.com"
|
85
|
+
Factory.build(:user, :name => lambda{"jimmy" + " cranky"}).name.should == "jimmy cranky"
|
86
|
+
Factory.build(:user, :name => "jenny", :email => Proc.new{ |u| "#{u.name}@home.com" }).email.should == "jenny@home.com"
|
87
|
+
Factory.build(:user, :name => Proc.new{"jimmy" + " cranky"}).name.should == "jimmy cranky"
|
88
|
+
end
|
82
89
|
|
83
90
|
end
|
84
91
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Stephen McGinty
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-26 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|