aub-machine 1.0.1 → 1.0.2
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/Rakefile +1 -1
- data/lib/machine/machine.rb +19 -5
- data/machine.gemspec +1 -1
- data/test/machine_test.rb +23 -0
- data/test/sequence_test.rb +5 -1
- data/test/test.db +0 -0
- data/test/test_helper.rb +3 -0
- metadata +1 -1
data/Rakefile
CHANGED
@@ -26,7 +26,7 @@ end
|
|
26
26
|
|
27
27
|
spec = Gem::Specification.new do |s|
|
28
28
|
s.name = %q{machine}
|
29
|
-
s.version = "1.0.
|
29
|
+
s.version = "1.0.2"
|
30
30
|
s.summary = %q{machine defines a factory system for creating model objects to replace fixtures in Ruby apps.}
|
31
31
|
s.description = %q{machine defines a factory system for creating model objects to replace fixtures in Ruby apps.}
|
32
32
|
|
data/lib/machine/machine.rb
CHANGED
@@ -12,8 +12,12 @@ class Machine
|
|
12
12
|
# machine definitions. By default, machine will attempt to require
|
13
13
|
# "machines," "test/machines," and "spec/machines." Only the first
|
14
14
|
# existing file will be loaded.
|
15
|
-
|
15
|
+
class << self
|
16
|
+
attr_accessor :definition_file_paths
|
17
|
+
end
|
16
18
|
|
19
|
+
@definition_file_paths = %w(machines test/machines spec/machines)
|
20
|
+
|
17
21
|
def self.find_definitions #:nodoc:
|
18
22
|
definition_file_paths.each do |file_path|
|
19
23
|
begin
|
@@ -95,17 +99,22 @@ class Machine
|
|
95
99
|
# attributes: (Hash)
|
96
100
|
# A set of attributes to use as a replacement for the default ones provided by
|
97
101
|
# the machine.
|
102
|
+
# block: (Proc, optional)
|
103
|
+
# If a block is passed to the function, it will attempt to call the block with
|
104
|
+
# the newly created object as an argument so that manual initialization can be
|
105
|
+
# done. This can be used in combination with or instead of passing an attributes hash.
|
98
106
|
#
|
99
107
|
# Example
|
100
108
|
#
|
101
109
|
# Machine.build(:car, :model => 'Civic', :make => 'Honda')
|
102
|
-
def self.build(name, attributes={})
|
110
|
+
def self.build(name, attributes={}, &block)
|
103
111
|
machines = machines_for(name)
|
104
112
|
raise MachineNotFoundError if machines.empty?
|
105
113
|
object = machines.shift.build(attributes)
|
106
114
|
while machine = machines.shift
|
107
115
|
machine.apply_to(object, attributes)
|
108
116
|
end
|
117
|
+
block.call(object) if block_given?
|
109
118
|
object
|
110
119
|
end
|
111
120
|
|
@@ -117,12 +126,16 @@ class Machine
|
|
117
126
|
# attributes: (Hash)
|
118
127
|
# A set of attributes to use as a replacement for the default ones provided by
|
119
128
|
# the machine.
|
129
|
+
# block: (Proc, optional)
|
130
|
+
# If a block is passed to the function, it will attempt to call the block with
|
131
|
+
# the newly created object as an argument so that manual initialization can be
|
132
|
+
# done. This can be used in combination with or instead of passing an attributes hash.
|
120
133
|
#
|
121
134
|
# Example
|
122
135
|
#
|
123
136
|
# Machine.build!(:car, :model => 'Civic', :make => 'Honda')
|
124
|
-
def self.build!(name, attributes={})
|
125
|
-
result = build(name, attributes)
|
137
|
+
def self.build!(name, attributes={}, &block)
|
138
|
+
result = build(name, attributes, &block)
|
126
139
|
result.save! if result.respond_to?(:save!)
|
127
140
|
result
|
128
141
|
end
|
@@ -187,7 +200,8 @@ class Machine
|
|
187
200
|
# address.street = machine.next(:street)
|
188
201
|
# end
|
189
202
|
def self.next(sequence)
|
190
|
-
|
203
|
+
raise MachineNotFoundError unless sequences.has_key?(sequence)
|
204
|
+
sequences[sequence].next
|
191
205
|
end
|
192
206
|
|
193
207
|
def initialize(name, options, proc) #:nodoc
|
data/machine.gemspec
CHANGED
data/test/machine_test.rb
CHANGED
@@ -266,4 +266,27 @@ class MachineTest < Test::Unit::TestCase
|
|
266
266
|
end
|
267
267
|
end
|
268
268
|
|
269
|
+
context 'with blocks on create' do
|
270
|
+
setup do
|
271
|
+
Machine.define :article do |article, machine|
|
272
|
+
article.title = 'aha'
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
should 'use a passed block in create to initialize objects' do
|
277
|
+
article = Machine.build(:article) do |article|
|
278
|
+
article.title = 'heck'
|
279
|
+
end
|
280
|
+
assert_equal 'heck', article.title
|
281
|
+
end
|
282
|
+
|
283
|
+
should 'use a passed block when creating with build!' do
|
284
|
+
article = Machine.build!(:article) do |article|
|
285
|
+
article.title = 'fun'
|
286
|
+
assert article.new_record?
|
287
|
+
end
|
288
|
+
assert_equal 'fun', article.title
|
289
|
+
assert !article.new_record?
|
290
|
+
end
|
291
|
+
end
|
269
292
|
end
|
data/test/sequence_test.rb
CHANGED
@@ -2,6 +2,10 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class SequenceTest < Test::Unit::TestCase
|
4
4
|
|
5
|
+
def setup
|
6
|
+
Machine.sequences.clear
|
7
|
+
end
|
8
|
+
|
5
9
|
should 'allow definition of sequences' do
|
6
10
|
Machine.sequence :thing do
|
7
11
|
end
|
@@ -36,4 +40,4 @@ class SequenceTest < Test::Unit::TestCase
|
|
36
40
|
Machine.next(:who)
|
37
41
|
end
|
38
42
|
end
|
39
|
-
end
|
43
|
+
end
|
data/test/test.db
CHANGED
Binary file
|
data/test/test_helper.rb
CHANGED