fabrication 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -56,6 +56,12 @@ So you already have a company fabricator, but you need one that specifically gen
56
56
 
57
57
  Setting the :from option will inherit the class and all the attributes from the named Fabricator. Even if you haven't defined a :company Fabricator yet, it will still work as long as it references an actual class name.
58
58
 
59
+ You can also explicitly specify the class being fabricated with the :class_name parameter.
60
+
61
+ Fabricator(:llc, :class_name => :company) do
62
+ type "LLC"
63
+ end
64
+
59
65
  ### Fabricating ###
60
66
 
61
67
  Now that your Fabricators are defined, you can start generating some objects! To generate the LLC from the previous example, just do this:
@@ -70,6 +76,10 @@ If you need to do something more complex, you can also pass a block to Fabricate
70
76
  location!(:count => 2) { Faker::Address.city }
71
77
  end
72
78
 
79
+ Sometimes you don't actually need to save an option when it is created but just build it. In that case, just call `Fabricate.build` and it will skip the saving step.
80
+
81
+ Fabricate.build(:company, :name => "Hashrocket")
82
+
73
83
  ### Contributing ###
74
84
 
75
85
  I (paulelliott) am actively maintaining this project. If you would like contribute, please fork the project, make your changes on a feature branch, and submit a pull request.
@@ -83,8 +93,6 @@ To run rake successfully:
83
93
  5. Run `rake` and the test suite should be all green!
84
94
 
85
95
 
86
- build without create...
87
-
88
96
  look into: Object.reflect_on_associations
89
97
 
90
98
  Fabricator(:atlanta_hub, :from => :hub) do
data/lib/fabrication.rb CHANGED
@@ -17,7 +17,9 @@ module Fabrication
17
17
  def schematic(name, options, &block)
18
18
  raise DuplicateFabricatorError if fabricators.has_key?(name)
19
19
  parent = fabricators[options[:from]]
20
- if parent
20
+ if options[:class_name]
21
+ class_name = options[:class_name]
22
+ elsif parent
21
23
  class_name = parent.class_name
22
24
  elsif options[:from]
23
25
  class_name = options[:from]
@@ -27,11 +29,11 @@ module Fabrication
27
29
  fabricators[name] = Fabricator.new(class_name, parent, &block)
28
30
  end
29
31
 
30
- def generate(name, options, &block)
32
+ def generate(name, options, overrides, &block)
31
33
  find_definitions if @@fabricators.nil?
32
34
  raise UnknownFabricatorError unless Fabrication::Support.fabricatable?(name)
33
35
  schematic(name, {}) unless fabricators.has_key?(name)
34
- fabricators[name].fabricate(options, &block)
36
+ fabricators[name].fabricate(options, overrides, &block)
35
37
  end
36
38
 
37
39
  def find_definitions
@@ -73,5 +75,13 @@ def Fabricator(name, options={}, &block)
73
75
  end
74
76
 
75
77
  def Fabricate(name, options={}, &block)
76
- Fabrication.generate(name, options, &block)
78
+ Fabrication.generate(name, {:save => true}, options, &block)
79
+ end
80
+
81
+ class Fabricate
82
+
83
+ def self.build(name, options={}, &block)
84
+ Fabrication.generate(name, {:save => false}, options, &block)
85
+ end
86
+
77
87
  end
@@ -17,8 +17,8 @@ class Fabrication::Fabricator
17
17
  end.new(klass, schematic)
18
18
  end
19
19
 
20
- def fabricate(options={}, &block)
21
- generator.generate(options, &block)
20
+ def fabricate(options={}, overrides={}, &block)
21
+ generator.generate(options, overrides, &block)
22
22
  end
23
23
 
24
24
  private
@@ -41,8 +41,8 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
41
41
 
42
42
  protected
43
43
 
44
- def after_generation
45
- instance.save
44
+ def after_generation(options)
45
+ instance.save if options[:save]
46
46
  end
47
47
 
48
48
  end
@@ -6,10 +6,10 @@ class Fabrication::Generator::Base
6
6
  self.after_create_block = block if block_given?
7
7
  end
8
8
 
9
- def generate(options={}, &block)
9
+ def generate(options={:save => true}, overrides={}, &block)
10
10
  self.instance = klass.new
11
- process_attributes(options, &block)
12
- after_generation
11
+ process_attributes(overrides, &block)
12
+ after_generation(options)
13
13
  after_create_block.call(instance) if after_create_block
14
14
  instance
15
15
  end
@@ -25,7 +25,7 @@ class Fabrication::Generator::Base
25
25
 
26
26
  protected
27
27
 
28
- def after_generation; end
28
+ def after_generation(options); end
29
29
 
30
30
  private
31
31
 
@@ -1,7 +1,7 @@
1
1
  class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
2
2
 
3
- def after_generation
4
- instance.save
3
+ def after_generation(options)
4
+ instance.save if options[:save]
5
5
  end
6
6
 
7
7
  def self.supports?(klass)
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -112,6 +112,22 @@ describe Fabrication do
112
112
 
113
113
  end
114
114
 
115
+ context 'with a specified class name' do
116
+
117
+ let(:someone) { Fabricate(:someone) }
118
+
119
+ before do
120
+ Fabricator(:someone, :class_name => :person) do
121
+ first_name "Paul"
122
+ end
123
+ end
124
+
125
+ it 'generates the person as someone' do
126
+ someone.first_name.should == "Paul"
127
+ end
128
+
129
+ end
130
+
115
131
  context 'with an active record object' do
116
132
 
117
133
  before(:all) do
@@ -20,7 +20,7 @@ describe Fabrication::Generator::ActiveRecord do
20
20
  context 'active record object' do
21
21
 
22
22
  let(:company) do
23
- generator.generate({})
23
+ generator.generate
24
24
  end
25
25
 
26
26
  before(:all) do
@@ -68,4 +68,18 @@ describe Fabrication::Generator::ActiveRecord do
68
68
 
69
69
  end
70
70
 
71
+ context 'with the build option' do
72
+
73
+ let(:company) { Fabricate.build(:company, :name => "Epitaph") }
74
+
75
+ it 'creates the record' do
76
+ company.name.should == 'Epitaph'
77
+ end
78
+
79
+ it 'does not save it to the database' do
80
+ Company.count.should == 0
81
+ end
82
+
83
+ end
84
+
71
85
  end
@@ -14,7 +14,7 @@ describe Fabrication::Generator::Base do
14
14
  let(:generator) { Fabrication::Generator::Base.new(Person, schematic) }
15
15
 
16
16
  let(:person) do
17
- generator.generate({:first_name => 'Body'})
17
+ generator.generate({:save => true}, {:first_name => 'Body'})
18
18
  end
19
19
 
20
20
  it 'passes the object to blocks' do
@@ -18,14 +18,14 @@ describe Fabrication::Generator::Mongoid do
18
18
 
19
19
  context 'mongoid object' do
20
20
 
21
- before { generator.generate(:name => 'Something') }
21
+ before { generator.generate({:save => true}, :name => 'Something') }
22
22
 
23
23
  it 'passes the object to blocks' do
24
- generator.generate({}).handle.should == "name"
24
+ generator.generate.handle.should == "name"
25
25
  end
26
26
 
27
27
  it 'passes the object and count to blocks' do
28
- generator.generate({}).books.map(&:title).should == ["Name 1","Name 2","Name 3"]
28
+ generator.generate.books.map(&:title).should == ["Name 1","Name 2","Name 3"]
29
29
  end
30
30
 
31
31
  it 'persists the author upon creation' do
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: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.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-07-23 00:00:00 -04:00
18
+ date: 2010-07-27 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency