Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

modified Sequence to accept Enumerators

  • Loading branch information...
commit 0d5ecf28b2f7e659b1ef6c25bff52f5f229ea08c 1 parent 49392a6
spartan-developer authored May 12, 2012
18  lib/factory_girl/sequence.rb
@@ -16,13 +16,27 @@ def initialize(name, *args, &proc)
16 16
     end
17 17
 
18 18
     def next
19  
-      @proc ? @proc.call(@value) : @value
  19
+      @proc ? @proc.call(current_value) : current_value
20 20
     ensure
21  
-      @value = @value.next
  21
+      increment_value
22 22
     end
23 23
 
24 24
     def names
25 25
       [@name] + @aliases
26 26
     end
  27
+
  28
+    private
  29
+
  30
+    def current_value
  31
+      @value.is_a?(Enumerator) ? @value.peek : @value
  32
+    end
  33
+
  34
+    def increment_value
  35
+      if @value.is_a?(Enumerator)
  36
+        @value.next
  37
+      else
  38
+        @value = @value.next
  39
+      end
  40
+    end
27 41
   end
28 42
 end
14  spec/factory_girl/sequence_spec.rb
@@ -15,7 +15,7 @@
15 15
     end
16 16
   end
17 17
 
18  
-  describe "a custom sequence" do
  18
+  describe "a custom sequence with string elements" do
19 19
     subject    { FactoryGirl::Sequence.new(:name, "A") {|n| "=#{n}" } }
20 20
     its(:next) { should == "=A" }
21 21
 
@@ -25,6 +25,18 @@
25 25
     end
26 26
   end
27 27
 
  28
+  describe "a custom sequence with an Enumerator" do
  29
+    subject    { FactoryGirl::Sequence.new(:name, %w{foo bar baz}.cycle) {|n| "=#{n}" } }
  30
+    its(:next) { should == "=foo" }
  31
+
  32
+    it "should enumerate properly" do
  33
+      subject.next.should == "=foo"
  34
+      subject.next.should == "=bar"
  35
+      subject.next.should == "=baz"
  36
+      subject.next.should == "=foo"
  37
+    end
  38
+  end
  39
+
28 40
   describe "a sequence with aliases using default value" do
29 41
     subject     { FactoryGirl::Sequence.new(:test, aliases: [:alias, :other]) { |n| "=#{n}" } }
30 42
     its(:next)  { should == "=1" }

0 notes on commit 0d5ecf2

Please sign in to comment.
Something went wrong with that request. Please try again.