Permalink
Browse files

modified Sequence to accept Enumerators

  • Loading branch information...
1 parent 49392a6 commit 0d5ecf28b2f7e659b1ef6c25bff52f5f229ea08c @spartan-developer committed May 12, 2012
Showing with 29 additions and 3 deletions.
  1. +16 −2 lib/factory_girl/sequence.rb
  2. +13 −1 spec/factory_girl/sequence_spec.rb
@@ -16,13 +16,27 @@ def initialize(name, *args, &proc)
end
def next
- @proc ? @proc.call(@value) : @value
+ @proc ? @proc.call(current_value) : current_value
ensure
- @value = @value.next
+ increment_value
end
def names
[@name] + @aliases
end
+
+ private
+
+ def current_value
+ @value.is_a?(Enumerator) ? @value.peek : @value
+ end
+
+ def increment_value
+ if @value.is_a?(Enumerator)
+ @value.next
+ else
+ @value = @value.next
+ end
+ end
end
end
@@ -15,7 +15,7 @@
end
end
- describe "a custom sequence" do
+ describe "a custom sequence with string elements" do
subject { FactoryGirl::Sequence.new(:name, "A") {|n| "=#{n}" } }
its(:next) { should == "=A" }
@@ -25,6 +25,18 @@
end
end
+ describe "a custom sequence with an Enumerator" do
+ subject { FactoryGirl::Sequence.new(:name, %w{foo bar baz}.cycle) {|n| "=#{n}" } }
+ its(:next) { should == "=foo" }
+
+ it "should enumerate properly" do
+ subject.next.should == "=foo"
+ subject.next.should == "=bar"
+ subject.next.should == "=baz"
+ subject.next.should == "=foo"
+ end
+ end
+
describe "a sequence with aliases using default value" do
subject { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } }
its(:next) { should == "=1" }

0 comments on commit 0d5ecf2

Please sign in to comment.