Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

multi sequence via optional aliases option

  • Loading branch information...
commit 178a7ab3f46303634b12ee247e1806d746f1c1fa 1 parent be7ac28
Kristian Mandrup kristianmandrup authored joshuaclayton committed
4 lib/factory_girl/definition_proxy.rb
View
@@ -108,8 +108,8 @@ def method_missing(name, *args, &block)
# end
#
# Except that no globally available sequence will be defined.
- def sequence(name, start_value = 1, &block)
- sequence = Sequence.new(name, start_value, &block)
+ def sequence(name, start_value = 1, options = {}, &block)
+ sequence = Sequence.new(name, start_value, options, &block)
add_attribute(name) { sequence.next }
end
16 lib/factory_girl/sequence.rb
View
@@ -5,14 +5,18 @@ module FactoryGirl
class Sequence
attr_reader :name, :names, :value
- def initialize(*names, &proc) #:nodoc:
- names.flatten!
- @value = value?(names.last) ? names.slice!(-1) : 1
- @names = names
- @name = names.first
- @proc = proc
+ def initialize(name, value = 1, options = {}, &proc) #:nodoc:
+ @value = value
+ if value.kind_of?(Hash)
+ options = value
+ @value = options[:value] || 1
+ end
+ @name = name
+ @names = ([name] + (options[:aliases] || [])).flatten
+ @proc = proc
end
+ # aliased sequences share the same sequence counter
def next
@proc ? @proc.call(@value) : @value
ensure
10 lib/factory_girl/syntax/default.rb
View
@@ -29,14 +29,8 @@ def factory(name, options = {}, &block)
end
end
- def sequence(name, start_value = 1, &block)
- FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
- end
-
- # define aliased sequence
- # - register multiple sequences with same logic but different identifying names
- def sequences(*names, &block)
- FactoryGirl.register_sequence(Sequence.new(names, &block))
+ def sequence(name, start_value = 1, options = {}, &block)
+ FactoryGirl.register_sequence(Sequence.new(name, start_value, options, &block))
end
def trait(name, &block)
2  spec/factory_girl/registry_spec.rb
View
@@ -3,7 +3,7 @@
describe FactoryGirl::Registry do
let(:aliases) { [:thing, :widget] }
let(:sequence) { FactoryGirl::Sequence.new(:email) { |n| "somebody#{n}@example.com" } }
- let(:sequences) { FactoryGirl::Sequence.new(:email, :sender, :receiver) { |n| "some_one_else#{n}@example.com" } }
+ let(:sequences) { FactoryGirl::Sequence.new(:email, :aliases => [:sender, :receiver]) { |n| "some_one_else#{n}@example.com" } }
let(:factory) { FactoryGirl::Factory.new(:object) }
let(:other_factory) { FactoryGirl::Factory.new(:string) }
20 spec/factory_girl/sequence_spec.rb
View
@@ -25,11 +25,27 @@
end
end
- describe "a sequence with multiple names (aliases)" do
+ describe "a sequence with aliases using default value" do
+ let(:name) { :test }
+ let(:aliases) { [:alias, :other] }
+ subject { FactoryGirl::Sequence.new(name, :aliases => aliases) {|n| "=#{n}" } }
+
+ its(:name) { should == name }
+ its(:value) { should == 1 }
+ its(:names) { should == ([name] + aliases) }
+ its(:next) { should == "=1" }
+
+ describe "when incrementing" do
+ before { subject.next }
+ its(:next) { should == "=2" }
+ end
+ end
+
+ describe "a sequence with custom value and aliases" do
let(:name) { :test }
let(:value) { 3 }
let(:aliases) { [:alias, :other] }
- subject { FactoryGirl::Sequence.new(name, :alias, :other, value) {|n| "=#{n}" } }
+ subject { FactoryGirl::Sequence.new(name, value, :aliases => aliases) {|n| "=#{n}" } }
its(:name) { should == name }
its(:value) { should == value }
Please sign in to comment.
Something went wrong with that request. Please try again.