Skip to content

Commit

Permalink
modified Sequence to accept Enumerators
Browse files Browse the repository at this point in the history
  • Loading branch information
spartan-developer committed May 12, 2012
1 parent 49392a6 commit 0d5ecf2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/factory_girl/sequence.rb
Expand Up @@ -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
14 changes: 13 additions & 1 deletion spec/factory_girl/sequence_spec.rb
Expand Up @@ -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" }

Expand All @@ -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" }
Expand Down

0 comments on commit 0d5ecf2

Please sign in to comment.