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 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.1"
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
 
@@ -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
- cattr_accessor :definition_file_paths
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
- sequences[sequence].next if sequences.has_key?(sequence)
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
@@ -1,5 +1,5 @@
1
1
  Gem::Specification.new do |s|
2
- s.version = '1.0.1'
2
+ s.version = '1.0.2'
3
3
  s.date = %q{2008-11-07}
4
4
 
5
5
  s.name = %q{machine}
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
@@ -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
@@ -1,6 +1,9 @@
1
1
  $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  $: << File.join(File.dirname(__FILE__))
3
3
 
4
+ require 'rubygems'
5
+ gem 'sqlite3-ruby'
6
+
4
7
  require 'test/unit'
5
8
  require 'rubygems'
6
9
  require 'ruby-debug'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aub-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aubrey Holland