fabrication 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -63,6 +63,14 @@ Breaking down the above, we are defining a "company" fabricator, which will gene
63
63
  * After the object is built but before it is saved, it will update the name to "Another Fun Factory".
64
64
  * After the object is created, it will update the "ceo" association with a new "drone" record.
65
65
 
66
+
67
+ For a class with required arguments in its constructor, use the `on_init` method:
68
+
69
+ Fabricator(:location) do
70
+ on_init { init_with(30.284167, -81.396111) }
71
+ end
72
+
73
+
66
74
  ### Inheritance ###
67
75
 
68
76
  So you already have a company fabricator, but you need one that specifically generates an LLC. No problem!
@@ -155,6 +163,7 @@ To run rake successfully:
155
163
  * Dave Ott (daveott)
156
164
  * Matt (winescout)
157
165
  * Lar Van Der Jagt (supaspoida)
166
+ * Sandro Turriate (sandro)
158
167
  * Justin Smestad (jsmestad)
159
168
  * Christopher Hanks (chanks) - Chained callbacks
160
169
  * monsterlabs - Rails 3 Generators
@@ -3,7 +3,12 @@ class Fabrication::Generator::Base
3
3
  def self.supports?(klass); true end
4
4
 
5
5
  def generate(options={:save => true}, attributes=[], callbacks={})
6
- self.instance = klass.new
6
+ if callbacks[:on_init]
7
+ self.instance = klass.new(*callbacks[:on_init].call)
8
+ else
9
+ self.instance = klass.new
10
+ end
11
+
7
12
  process_attributes(attributes)
8
13
 
9
14
  callbacks[:after_build].each { |callback| callback.call(instance) } if callbacks[:after_build]
@@ -38,11 +38,13 @@ class Fabrication::Schematic
38
38
  end
39
39
 
40
40
  def generate(options={}, overrides={}, &block)
41
- attributes = merge(overrides, &block).attributes
42
- if options[:attributes]
43
- to_hash(attributes, overrides)
44
- else
45
- generator.new(klass).generate(options, attributes, callbacks)
41
+ new_schematic = merge(overrides, &block)
42
+ new_schematic.instance_eval do
43
+ if options[:attributes]
44
+ to_hash(attributes, overrides)
45
+ else
46
+ generator.new(klass).generate(options, attributes, callbacks)
47
+ end
46
48
  end
47
49
  end
48
50
 
@@ -57,6 +59,10 @@ class Fabrication::Schematic
57
59
  end
58
60
  end
59
61
 
62
+ def init_with(*args)
63
+ args
64
+ end
65
+
60
66
  def merge(overrides={}, &block)
61
67
  clone.tap do |schematic|
62
68
  schematic.instance_eval(&block) if block_given?
@@ -87,6 +93,10 @@ class Fabrication::Schematic
87
93
  end
88
94
  end
89
95
 
96
+ def on_init(&block)
97
+ callbacks[:on_init] = block
98
+ end
99
+
90
100
  def parse_method_name(method_name, args)
91
101
  if method_name.to_s.end_with?("!")
92
102
  method_name = method_name.to_s.chomp("!").to_sym
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.8.3'
2
+ VERSION = '0.9.0'
3
3
  end
@@ -36,6 +36,39 @@ describe Fabrication::Generator::Base do
36
36
  person.instance_variable_get(:@first_name).should == 'Guy'
37
37
  end
38
38
 
39
+ context "with on_init block" do
40
+ subject { schematic.generate }
41
+
42
+ let(:klass) { Struct.new :arg1, :arg2 }
43
+
44
+ context "using init_with" do
45
+ let(:schematic) do
46
+ Fabrication::Schematic.new(klass) do
47
+ on_init { init_with(:a, :b) }
48
+ end
49
+ end
50
+
51
+ it "sends the return value of the block to the klass' initialize method" do
52
+ subject.arg1.should == :a
53
+ subject.arg2.should == :b
54
+ end
55
+ end
56
+
57
+ context "not using init_with" do
58
+ let(:schematic) do
59
+ Fabrication::Schematic.new(klass) do
60
+ on_init { [ :a, :b ] }
61
+ end
62
+ end
63
+
64
+ it "sends the return value of the block to the klass' initialize method" do
65
+ subject.arg1.should == :a
66
+ subject.arg2.should == :b
67
+ end
68
+
69
+ end
70
+ end
71
+
39
72
  end
40
73
 
41
74
  end
@@ -135,4 +135,31 @@ describe Fabrication::Schematic do
135
135
 
136
136
  end
137
137
 
138
+ describe "#on_init" do
139
+ let(:init_block) { lambda {} }
140
+ let(:init_schematic) do
141
+ block = init_block
142
+ Fabrication::Schematic.new(OpenStruct) do
143
+ on_init &block
144
+ end
145
+ end
146
+
147
+ it "stores the on_init callback" do
148
+ init_schematic.callbacks[:on_init].should == init_block
149
+ end
150
+
151
+ context "with inheritance" do
152
+ let(:child_block) { lambda {} }
153
+ let(:child_schematic) do
154
+ block = child_block
155
+ init_schematic.merge do
156
+ on_init &block
157
+ end
158
+ end
159
+
160
+ it "overwrites the on_init callback" do
161
+ child_schematic.callbacks[:on_init].should == child_block
162
+ end
163
+ end
164
+ end
138
165
  end
@@ -56,6 +56,17 @@ describe Fabrication do
56
56
 
57
57
  end
58
58
 
59
+ context 'when the class requires a constructor' do
60
+ subject do
61
+ Fabricate(:city) do
62
+ on_init { init_with('Jacksonville Beach', 'FL') }
63
+ end
64
+ end
65
+
66
+ its(:city) { should == 'Jacksonville Beach' }
67
+ its(:state) { should == 'FL' }
68
+ end
69
+
59
70
  context "when referring to other fabricators" do
60
71
 
61
72
  let(:person) { Fabricate(:person) }
data/spec/fabricators.rb CHANGED
@@ -29,6 +29,10 @@ Fabricator(:senior, :from => :child) do
29
29
  after_build { |senior| senior.age *= 7 }
30
30
  end
31
31
 
32
+ Fabricator(:city) do
33
+ on_init { init_with('Boulder', 'CO') }
34
+ end
35
+
32
36
  # ActiveRecord Objects
33
37
  Fabricator(:division) do
34
38
  name "Division Name"
@@ -9,3 +9,12 @@ end
9
9
  class Person
10
10
  attr_accessor :age, :first_name, :last_name, :shoes, :location
11
11
  end
12
+
13
+ class City
14
+ attr_accessor :city, :state
15
+
16
+ def initialize(city, state)
17
+ self.city = city
18
+ self.state = state
19
+ end
20
+ 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: 57
4
+ hash: 59
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 8
9
- - 3
10
- version: 0.8.3
8
+ - 9
9
+ - 0
10
+ version: 0.9.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-09-30 00:00:00 -04:00
18
+ date: 2010-10-07 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency