fabrication 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -43,3 +43,13 @@ Breaking down the above, we are defining a "company" fabricator, which will gene
43
43
  * It has a has_many association to employees and will generate an array of 20 records as indicated by the :count => 20. The block is passed the company object being fabricated and index of the array being created.
44
44
  * It has a belongs_to association to location and this will be generated immediately with the company object. This is because of the :force => true parameter being passed in.
45
45
  * After the object is created, it will update the "ceo" association with a new "drone" record.
46
+
47
+ ### Inheritance ###
48
+
49
+ So you already have a company fabricator, but you need one that specifically generates an LLC. No problem!
50
+
51
+ Fabricator(:llc, :from => :company) do
52
+ type "LLC"
53
+ end
54
+
55
+ Setting the :from option will inherit the class and all the attributes from the named Fabricator.
data/lib/fabrication.rb CHANGED
@@ -10,8 +10,10 @@ module Fabrication
10
10
 
11
11
  class << self
12
12
 
13
- def schematic(name, &block)
14
- fabricators[name] = Fabricator.new(name, &block)
13
+ def schematic(name, options, &block)
14
+ parent = fabricators[options[:from]]
15
+ class_name = parent ? parent.class_name : name
16
+ fabricators[name] = Fabricator.new(class_name, parent, &block)
15
17
  end
16
18
 
17
19
  def generate(name, options)
@@ -47,8 +49,8 @@ module Fabrication
47
49
 
48
50
  end
49
51
 
50
- def Fabricator(name, &block)
51
- Fabrication.schematic(name, &block)
52
+ def Fabricator(name, options={}, &block)
53
+ Fabrication.schematic(name, options, &block)
52
54
  end
53
55
 
54
56
  def Fabricate(name, options={})
@@ -6,22 +6,24 @@ class Fabrication::Fabricator
6
6
  Fabrication::Generator::Base
7
7
  ]
8
8
 
9
- attr_accessor :fabricator
9
+ attr_accessor :class_name
10
10
 
11
- def initialize(class_name, &block)
11
+ def initialize(class_name, parent=nil, &block)
12
+ self.class_name = class_name
12
13
  klass = class_for(class_name)
13
- self.fabricator = GENERATORS.detect do |generator|
14
- generator.supports?(klass)
15
- end.new(klass, &block)
14
+ self.generator = GENERATORS.detect do |gen|
15
+ gen.supports?(klass)
16
+ end.new(klass, parent, &block)
16
17
  end
17
18
 
18
19
  def fabricate(options={})
19
- fabricator.generate(options)
20
+ generator.generate(options)
20
21
  end
21
22
 
22
- protected
23
+ private
24
+
25
+ attr_accessor :generator
23
26
 
24
- #Stolen directly from factory_girl. Thanks thoughtbot!
25
27
  def class_for(class_or_to_s)
26
28
  if class_or_to_s.respond_to?(:to_sym)
27
29
  class_name = variable_name_to_class_name(class_or_to_s)
@@ -33,7 +35,6 @@ class Fabrication::Fabricator
33
35
  end
34
36
  end
35
37
 
36
- #Stolen directly from factory_girl. Thanks thoughtbot!
37
38
  def variable_name_to_class_name(name)
38
39
  name.to_s.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase}
39
40
  end
@@ -1,14 +1,15 @@
1
1
  class Fabrication::Generator::Base
2
2
 
3
- attr_accessor :klass, :block, :instance
3
+ attr_accessor :klass, :parent, :block, :instance
4
4
 
5
- def initialize(klass, &block)
5
+ def initialize(klass, parent=nil, &block)
6
6
  self.klass = klass
7
+ self.parent = parent
7
8
  self.block = block
8
9
  end
9
10
 
10
- def generate(options)
11
- self.instance = klass.new
11
+ def generate(options={})
12
+ self.instance = parent ? parent.fabricate : klass.new
12
13
  instance_eval &block
13
14
  options.each { |k,v| assign(instance, k, v) }
14
15
  instance
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -67,6 +67,24 @@ describe Fabrication do
67
67
 
68
68
  end
69
69
 
70
+ context 'with the generation parameter' do
71
+
72
+ before(:all) do
73
+ Fabricator(:person) do
74
+ first_name "Paul"
75
+ last_name { |person| "#{person.first_name}#{person.age}" }
76
+ age 60
77
+ end
78
+ end
79
+
80
+ let(:person) { Fabricate(:person) }
81
+
82
+ it 'evaluates the fields in order of declaration' do
83
+ person.last_name.should == "Paul"
84
+ end
85
+
86
+ end
87
+
70
88
  context 'multiple instance' do
71
89
 
72
90
  let(:person1) { Fabricate(:person, :first_name => 'Jane') }
@@ -149,4 +167,29 @@ describe Fabrication do
149
167
  end
150
168
  end
151
169
 
170
+ context 'with a parent fabricator' do
171
+
172
+ let(:ernie) { Fabricate(:hemingway) }
173
+
174
+ before do
175
+ Fabricator(:author) do
176
+ name 'George Orwell'
177
+ books { ['1984'] }
178
+ end
179
+
180
+ Fabricator(:hemingway, :from => :author) do
181
+ name 'Ernest Hemingway'
182
+ end
183
+ end
184
+
185
+ it 'has the values from the parent' do
186
+ ernie.books.should == ['1984']
187
+ end
188
+
189
+ it 'overrides specified values from the parent' do
190
+ ernie.name.should == 'Ernest Hemingway'
191
+ end
192
+
193
+ end
194
+
152
195
  end
@@ -16,7 +16,7 @@ describe Fabrication::Fabricator do
16
16
  end
17
17
 
18
18
  it 'uses the base generator' do
19
- fabricator.instance_variable_get(:@fabricator).instance_of?(Fabrication::Generator::Base).should be_true
19
+ fabricator.instance_variable_get(:@generator).instance_of?(Fabrication::Generator::Base).should be_true
20
20
  end
21
21
 
22
22
  end
@@ -33,7 +33,7 @@ describe Fabrication::Fabricator do
33
33
  end
34
34
 
35
35
  it 'uses the activerecord generator' do
36
- fabricator.instance_variable_get(:@fabricator).instance_of?(Fabrication::Generator::ActiveRecord).should be_true
36
+ fabricator.instance_variable_get(:@generator).instance_of?(Fabrication::Generator::ActiveRecord).should be_true
37
37
  end
38
38
 
39
39
  end
@@ -47,7 +47,7 @@ describe Fabrication::Fabricator do
47
47
  end
48
48
 
49
49
  it 'uses the activerecord generator' do
50
- fabricator.instance_variable_get(:@fabricator).instance_of?(Fabrication::Generator::Mongoid).should be_true
50
+ fabricator.instance_variable_get(:@generator).instance_of?(Fabrication::Generator::Mongoid).should be_true
51
51
  end
52
52
 
53
53
  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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul Elliott
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-20 00:00:00 -04:00
18
+ date: 2010-06-29 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency