fabrication 0.2.0 → 0.2.1

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/lib/fabrication.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Fabrication
2
2
 
3
+ require 'fabrication/errors'
3
4
  autoload :Fabricator, 'fabrication/fabricator'
4
5
 
5
6
  module Generator
@@ -12,12 +13,19 @@ module Fabrication
12
13
 
13
14
  def schematic(name, options, &block)
14
15
  parent = fabricators[options[:from]]
15
- class_name = parent ? parent.class_name : name
16
+ if parent
17
+ class_name = parent.class_name
18
+ elsif options[:from]
19
+ class_name = options[:from]
20
+ else
21
+ class_name = name
22
+ end
16
23
  fabricators[name] = Fabricator.new(class_name, parent, &block)
17
24
  end
18
25
 
19
26
  def generate(name, options)
20
- find_definitions if fabricators.empty?
27
+ find_definitions if @@fabricators.nil?
28
+ raise UnknownFabricatorError unless fabricators.has_key?(name)
21
29
  fabricators[name].fabricate(options)
22
30
  end
23
31
 
@@ -39,6 +47,10 @@ module Fabrication
39
47
  end
40
48
  end
41
49
 
50
+ def clear_definitions
51
+ fabricators.clear
52
+ end
53
+
42
54
  private
43
55
 
44
56
  def fabricators
@@ -0,0 +1,5 @@
1
+ module Fabrication
2
+
3
+ class UnknownFabricatorError < StandardError; end
4
+
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -169,25 +169,63 @@ describe Fabrication do
169
169
 
170
170
  context 'with a parent fabricator' do
171
171
 
172
- let(:ernie) { Fabricate(:hemingway) }
172
+ context 'and a previously defined parent' do
173
173
 
174
- before do
175
- Fabricator(:author) do
176
- name 'George Orwell'
177
- books { ['1984'] }
174
+ let(:ernie) { Fabricate(:hemingway) }
175
+
176
+ before(:all) do
177
+ Fabricator(:author) do
178
+ name 'George Orwell'
179
+ books { ['1984'] }
180
+ end
181
+
182
+ Fabricator(:hemingway, :from => :author) do
183
+ name 'Ernest Hemingway'
184
+ end
185
+ end
186
+
187
+ it 'has the values from the parent' do
188
+ ernie.books.should == ['1984']
178
189
  end
179
190
 
180
- Fabricator(:hemingway, :from => :author) do
181
- name 'Ernest Hemingway'
191
+ it 'overrides specified values from the parent' do
192
+ ernie.name.should == 'Ernest Hemingway'
182
193
  end
194
+
183
195
  end
184
196
 
185
- it 'has the values from the parent' do
186
- ernie.books.should == ['1984']
197
+ context 'and a class name as a parent' do
198
+
199
+ before(:all) do
200
+ Fabrication.clear_definitions
201
+ Fabricator(:hemingway, :from => :author) do
202
+ name 'Ernest Hemingway'
203
+ end
204
+ end
205
+
206
+ let(:ernie) { Fabricate(:hemingway) }
207
+
208
+ it 'has the name defined' do
209
+ ernie.name.should == 'Ernest Hemingway'
210
+ end
211
+
212
+ it 'not have any books' do
213
+ ernie.books.should be_nil
214
+ end
215
+
216
+ end
217
+
218
+ end
219
+
220
+ describe '.clear_definitions' do
221
+
222
+ before(:all) do
223
+ Fabricator(:author) {}
224
+ Fabrication.clear_definitions
187
225
  end
188
226
 
189
- it 'overrides specified values from the parent' do
190
- ernie.name.should == 'Ernest Hemingway'
227
+ it 'should not generate authors' do
228
+ lambda { Fabricate(:author) }.should raise_error(Fabrication::UnknownFabricatorError)
191
229
  end
192
230
 
193
231
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Elliott
@@ -108,6 +108,7 @@ extensions: []
108
108
  extra_rdoc_files: []
109
109
 
110
110
  files:
111
+ - lib/fabrication/errors.rb
111
112
  - lib/fabrication/fabricator.rb
112
113
  - lib/fabrication/generator/active_record.rb
113
114
  - lib/fabrication/generator/base.rb