Skip to content
This repository
Browse code

sequence can now have multiple aliases

  • Loading branch information...
commit f387e3865f0b7db45c5bc405cdbcfda6876500c0 1 parent 055c976
Kristian Mandrup authored March 29, 2012 joshuaclayton committed April 06, 2012
18  lib/factory_girl/sequence.rb
@@ -3,12 +3,14 @@ module FactoryGirl
3 3
   # Sequences are defined using sequence within a FactoryGirl.define block.
4 4
   # Sequence values are generated using next.
5 5
   class Sequence
6  
-    attr_reader :name
  6
+    attr_reader :name, :names, :value
7 7
 
8  
-    def initialize(name, value = 1, &proc) #:nodoc:
9  
-      @name  = name
10  
-      @proc  = proc
11  
-      @value = value
  8
+    def initialize(*names, &proc) #:nodoc:
  9
+      names.flatten!
  10
+      @value  = value?(names.last) ? names.slice!(-1) : 1
  11
+      @names  = names
  12
+      @name   = names.first
  13
+      @proc   = proc      
12 14
     end
13 15
 
14 16
     def next
@@ -17,8 +19,10 @@ def next
17 19
       @value = @value.next
18 20
     end
19 21
 
20  
-    def names
21  
-      [@name]
  22
+    private
  23
+
  24
+    def value? item
  25
+      item.is_a?(Numeric) || item.is_a?(String)
22 26
     end
23 27
   end
24 28
 end
8  lib/factory_girl/syntax/default.rb
@@ -33,6 +33,14 @@ def sequence(name, start_value = 1, &block)
33 33
           FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
34 34
         end
35 35
 
  36
+        # define aliased sequence 
  37
+        # - register multiple sequences with same logic but different identifying names
  38
+        def sequences(*names, &block)                    
  39
+          names.each do |name|
  40
+            FactoryGirl.register_sequence(Sequence.new(names, &block))
  41
+          end
  42
+        end
  43
+
36 44
         def trait(name, &block)
37 45
           FactoryGirl.register_trait(Trait.new(name, &block))
38 46
         end
9  spec/factory_girl/registry_spec.rb
@@ -3,6 +3,8 @@
3 3
 describe FactoryGirl::Registry do
4 4
   let(:aliases)              { [:thing, :widget] }
5 5
   let(:sequence)             { FactoryGirl::Sequence.new(:email) { |n| "somebody#{n}@example.com" } }
  6
+  let(:sequences)            { FactoryGirl::Sequence.new(:email, :sender, :receiver) { |n| "some_one_else#{n}@example.com" } }
  7
+
6 8
   let(:factory)              { FactoryGirl::Factory.new(:object) }
7 9
   let(:other_factory)        { FactoryGirl::Factory.new(:string) }
8 10
   let(:factory_with_aliases) { FactoryGirl::Factory.new(:string, aliases: aliases) }
@@ -61,6 +63,13 @@
61 63
     subject.find(:email).should == sequence
62 64
   end
63 65
 
  66
+  it "registers a multi sequences" do
  67
+    subject.add(sequences)
  68
+    subject.find(:email).should == sequences
  69
+    subject.find(:sender).should == sequences
  70
+    subject.find(:receiver).should == sequences
  71
+  end
  72
+
64 73
   it "doesn't allow a duplicate name" do
65 74
     expect { 2.times { subject.add(factory) } }.
66 75
       to raise_error(FactoryGirl::DuplicateDefinitionError, "Factory already registered: object")
17  spec/factory_girl/sequence_spec.rb
@@ -25,6 +25,23 @@
25 25
     end
26 26
   end
27 27
 
  28
+  describe "a sequence with multiple names (aliases)" do
  29
+    let(:name) { :test }
  30
+    let(:value) { 3 }
  31
+    let(:aliases) { [:alias, :other] }
  32
+    subject    { FactoryGirl::Sequence.new(name, :alias, :other, value) {|n| "=#{n}" } }
  33
+
  34
+    its(:name)  { should == name }
  35
+    its(:value) { should == value }
  36
+    its(:names) { should == ([name] + aliases) }
  37
+    its(:next)  { should == "=3" }
  38
+
  39
+    describe "when incrementing" do
  40
+      before     { subject.next }
  41
+      its(:next) { should == "=4" }
  42
+    end
  43
+  end
  44
+
28 45
   describe "a basic sequence without a block" do
29 46
     subject    { FactoryGirl::Sequence.new(:name) }
30 47
     its(:next) { should == 1 }

0 notes on commit f387e38

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