Permalink
Browse files

Keeping superclass generators when overriding some.

Fix proposed by Valdis (http://github.com/valdis/object_daddy/commit/9f8054f798e3794796c2824e2c77cf0e91a2aa71)
Made complete, correct, and tested.
  • Loading branch information...
ymendel committed Jul 28, 2009
1 parent 87d2659 commit 28da67223f89c16f34c58fb149ea03c77ddd8b32
Showing with 21 additions and 8 deletions.
  1. +4 −1 lib/object_daddy.rb
  2. +17 −7 spec/object_daddy_spec.rb
View
@@ -90,15 +90,18 @@ def generates_subclass(subclass_name)
def gather_exemplars
return if exemplars_generated
+
+ self.generators ||= {}
if superclass.respond_to?(:gather_exemplars)
superclass.gather_exemplars
- self.generators = (superclass.generators || {}).dup
+ self.generators = (superclass.generators).merge(generators).dup
end
exemplar_path.each do |raw_path|
path = File.join(raw_path, "#{underscore(name)}_exemplar.rb")
load(path) if File.exists?(path)
end
+
self.exemplars_generated = true
end
View
@@ -506,7 +506,7 @@ def self.generator_value_method(prev)
SubWidget.stubs(:exemplar_path).returns(@subfile_path)
File.open(@file_name, 'w') do |f|
- f.puts "class Widget\ngenerator_for :blah do |prev| 'blah'; end\nend\n"
+ f.puts "class Widget\ngenerator_for :blah do |prev| 'blah'; end\ngenerator_for :other do |prev| 'bang'; end\nend\n"
end
end
@@ -516,17 +516,27 @@ def self.generator_value_method(prev)
end
it 'should use generators from the parent class' do
- SubWidget.spawn.blah.should == 'blah'
+ SubWidget.spawn.other.should == 'bang'
+ SubWidget.spawn.blah.should == 'blah'
end
- it 'should let subclass generators override parent generators' do
- File.open(@subfile_name, 'w') do |f|
- f.puts "class SubWidget\ngenerator_for :blah do |prev| 'blip'; end\nend\n"
+ describe 'with subclass generators' do
+ before :each do
+ File.open(@subfile_name, 'w') do |f|
+ f.puts "class SubWidget\ngenerator_for :blah do |prev| 'blip'; end\nend\n"
+ end
+ end
+
+ it 'should still use generators from the parent class' do
+ SubWidget.spawn.other.should == 'bang'
+ end
+
+ it 'should allow overriding parent generators' do
+ SubWidget.spawn.blah.should == 'blip'
end
- SubWidget.spawn.blah.should == 'blip'
end
end
-
+
describe 'using generators called directly' do
it 'should use generators from the parent class' do
@class.generator_for :blah do |prev| 'blah'; end

0 comments on commit 28da672

Please sign in to comment.