Browse files

Refactor generator api

  • Loading branch information...
1 parent 09879bf commit 21f0bd1a09b51405932a014e8ffe81d390766061 @paulelliott committed May 4, 2012
View
2 lib/fabrication/generator/active_record.rb
@@ -4,7 +4,7 @@ def self.supports?(klass)
defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
end
- def build
+ def build_instance
self.__instance = __klass.new(__attributes)
end
View
23 lib/fabrication/generator/base.rb
@@ -2,30 +2,35 @@ class Fabrication::Generator::Base
def self.supports?(__klass); true end
- def generate(options={:save => true}, attributes=[], callbacks={})
+ def build(attributes=[], callbacks={})
process_attributes(attributes)
if callbacks[:on_init]
- build_with_init_callback(callbacks[:on_init])
+ build_instance_with_init_callback(callbacks[:on_init])
else
- build
+ build_instance
end
callbacks[:after_build].each { |callback| callback.call(__instance) } if callbacks[:after_build]
- after_generation(options)
- callbacks[:after_create].each { |callback| callback.call(__instance) } if callbacks[:after_create] && options[:save]
__instance
end
- def build_with_init_callback(callback)
+ def create(attributes=[], callbacks=[])
+ build(attributes, callbacks)
+ persist
+ callbacks[:after_create].each { |callback| callback.call(__instance) } if callbacks[:after_create]
+ __instance
+ end
+
+ def build_instance_with_init_callback(callback)
self.__instance = __klass.new(*callback.call)
__attributes.each do |k,v|
__instance.send("#{k}=", v)
end
end
- def build
+ def build_instance
self.__instance = __klass.new
__attributes.each do |k,v|
__instance.send("#{k}=", v)
@@ -52,8 +57,8 @@ def __attributes
@__attributes ||= {}
end
- def after_generation(options)
- __instance.save! if options[:save] && __instance.respond_to?(:save!)
+ def persist
+ __instance.save! if __instance.respond_to?(:save!)
end
def assign(method_name, options, raw_value=nil)
View
2 lib/fabrication/generator/mongoid.rb
@@ -4,7 +4,7 @@ def self.supports?(klass)
defined?(Mongoid) && klass.ancestors.include?(Mongoid::Document)
end
- def build
+ def build_instance
self.__instance = __klass.new(__attributes)
end
View
4 lib/fabrication/generator/sequel.rb
@@ -4,8 +4,8 @@ def self.supports?(klass)
defined?(Sequel) && klass.ancestors.include?(Sequel::Model)
end
- def after_generation(options)
- __instance.save if options[:save]
+ def persist
+ __instance.save
end
end
View
6 lib/fabrication/schematic/definition.rb
@@ -48,19 +48,19 @@ def callbacks
def build(overrides={}, &block)
merge(overrides, &block).instance_eval do
- generator.new(klass).generate({save: false}, attributes, callbacks)
+ generator.new(klass).build(attributes, callbacks)
end
end
def fabricate(overrides={}, &block)
merge(overrides, &block).instance_eval do
- generator.new(klass).generate({save: true}, attributes, callbacks)
+ generator.new(klass).create(attributes, callbacks)
end
end
def to_attributes(overrides={}, &block)
merge(overrides, &block).instance_eval do
- to_hash(generator.new(klass).generate({save: false}, attributes, callbacks))
+ to_hash(generator.new(klass).build(attributes, callbacks))
end
end
View
16 spec/fabrication/generator/active_record_spec.rb
@@ -12,24 +12,19 @@
end
end
- describe "#after_generation" do
+ describe "#persist" do
let(:instance) { mock(:instance) }
let(:generator) { Fabrication::Generator::ActiveRecord.new(Company) }
before { generator.send(:__instance=, instance) }
- it "saves with a true save flag" do
+ it "saves" do
instance.should_receive(:save!)
- generator.send(:after_generation, {:save => true})
- end
-
- it "does not save without a true save flag" do
- instance.should_not_receive(:save)
- generator.send(:after_generation, {})
+ generator.send(:persist)
end
end
- describe "#generate" do
+ describe "#create" do
let(:attributes) do
Fabrication::Schematic::Definition.new(Company) do
@@ -40,7 +35,7 @@
end
let(:generator) { Fabrication::Generator::ActiveRecord.new(Company) }
- let!(:company) { generator.generate({:save => true}, attributes) }
+ let!(:company) { generator.create(attributes, {}) }
let(:divisions) { company.divisions }
it 'passes the object to blocks' do
@@ -55,7 +50,6 @@
Company.find_by_name('Company Name').should be
end
-
it 'generates the divisions' do
divisions.length.should == 2
end
View
15 spec/fabrication/generator/base_spec.rb
@@ -9,7 +9,7 @@
end
end
- describe "#generate" do
+ describe "#build" do
let(:generator) { Fabrication::Generator::Base.new(Person) }
@@ -22,7 +22,7 @@
end.attributes
end
- let(:person) { generator.generate({}, attributes) }
+ let(:person) { generator.build(attributes) }
it 'generates an instance' do
person.instance_of?(Person).should be_true
@@ -88,20 +88,15 @@
end
- describe "#after_generation" do
+ describe "#persist" do
let(:instance) { mock(:instance) }
let(:generator) { Fabrication::Generator::Base.new(Object) }
before { generator.send(:__instance=, instance) }
- it "saves with a true save flag" do
+ it 'saves' do
instance.should_receive(:save!)
- generator.send(:after_generation, {:save => true})
- end
-
- it "does not save without a true save flag" do
- instance.should_not_receive(:save)
- generator.send(:after_generation, {})
+ generator.send(:persist)
end
end

0 comments on commit 21f0bd1

Please sign in to comment.