modified Sequence to accept Enumerators #378

Closed
wants to merge 1 commit into
from
@@ -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" }