Permalink
Browse files

Adding a reset for cumulative generators

  • Loading branch information...
awebneck committed Nov 26, 2012
1 parent 4ee220c commit 00b408ab55aecdff4b5e05d58bef9dc29c220175
Showing with 37 additions and 0 deletions.
  1. +10 −0 lib/object_daddy.rb
  2. +27 −0 spec/object_daddy_spec.rb
View
@@ -89,6 +89,15 @@ def generator_for(handle, args = {}, &block)
end
end
def reset_generators
generators.each do |handle, generator|
if generator[:generator][:initial]
generator[:generator][:start] = generator[:generator][:initial]
end
generator[:generator].delete :prev
end
end
def generates_subclass(subclass_name)
@concrete_subclass_name = subclass_name.to_s
end
@@ -159,6 +168,7 @@ def generate_values(args)
def process_generated_value(args, handle, generator, block)
if generator[:start]
value = generator[:start]
generator[:initial] = value
generator.delete(:start)
else
if block.arity == 0
View
@@ -264,6 +264,33 @@
end
end
describe ObjectDaddy, "when resetting a class instance" do
before(:each) do
@class = Class.new(OpenStruct)
@class.send(:include, ObjectDaddy)
@file_path = [File.join(File.dirname(__FILE__), 'tmp')]
@file_name = File.join(@file_path, 'widget_exemplar.rb')
@class.stubs(:exemplar_path).returns(@file_path)
@class.stubs(:name).returns('Widget')
end
it "should return the initial value once more after calling if the start value is specified" do
@class.generator_for :foo, :start => 'frobnitz' do |prev| prev + 'a'; end
@class.spawn
@class.spawn
@class.reset_generators
@class.spawn.foo.should == 'frobnitz'
end
it "should pass nil for the previous item after calling" do
@class.generator_for :foo do |prev| prev ? prev.succ : 'hats'; end
@class.spawn
@class.spawn
@class.reset_generators
@class.spawn.foo.should == 'hats'
end
end
describe ObjectDaddy, "when spawning a class instance" do
before(:each) do
@class = Class.new(OpenStruct)

0 comments on commit 00b408a

Please sign in to comment.