Skip to content
This repository
Browse code

multi sequence via optional aliases option

  • Loading branch information...
commit 178a7ab3f46303634b12ee247e1806d746f1c1fa 1 parent be7ac28
Kristian Mandrup authored April 01, 2012 joshuaclayton committed April 06, 2012
4  lib/factory_girl/definition_proxy.rb
@@ -108,8 +108,8 @@ def method_missing(name, *args, &block)
108 108
     #   end
109 109
     #
110 110
     # Except that no globally available sequence will be defined.
111  
-    def sequence(name, start_value = 1, &block)
112  
-      sequence = Sequence.new(name, start_value, &block)
  111
+    def sequence(name, start_value = 1, options = {}, &block)
  112
+      sequence = Sequence.new(name, start_value, options, &block)
113 113
       add_attribute(name) { sequence.next }
114 114
     end
115 115
 
16  lib/factory_girl/sequence.rb
@@ -5,14 +5,18 @@ module FactoryGirl
5 5
   class Sequence
6 6
     attr_reader :name, :names, :value
7 7
 
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      
  8
+    def initialize(name, value = 1, options = {}, &proc) #:nodoc:
  9
+      @value  = value
  10
+      if value.kind_of?(Hash)
  11
+        options = value
  12
+        @value = options[:value] || 1
  13
+      end      
  14
+      @name   = name
  15
+      @names  = ([name] + (options[:aliases] || [])).flatten
  16
+      @proc   = proc     
14 17
     end
15 18
 
  19
+    # aliased sequences share the same sequence counter
16 20
     def next
17 21
       @proc ? @proc.call(@value) : @value
18 22
     ensure
10  lib/factory_girl/syntax/default.rb
@@ -29,14 +29,8 @@ def factory(name, options = {}, &block)
29 29
           end
30 30
         end
31 31
 
32  
-        def sequence(name, start_value = 1, &block)
33  
-          FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
34  
-        end
35  
-
36  
-        # define aliased sequence 
37  
-        # - register multiple sequences with same logic but different identifying names
38  
-        def sequences(*names, &block)                    
39  
-          FactoryGirl.register_sequence(Sequence.new(names, &block))
  32
+        def sequence(name, start_value = 1, options = {}, &block)
  33
+          FactoryGirl.register_sequence(Sequence.new(name, start_value, options, &block))
40 34
         end
41 35
 
42 36
         def trait(name, &block)
2  spec/factory_girl/registry_spec.rb
@@ -3,7 +3,7 @@
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" } }
  6
+  let(:sequences)            { FactoryGirl::Sequence.new(:email, :aliases => [:sender, :receiver]) { |n| "some_one_else#{n}@example.com" } }
7 7
 
8 8
   let(:factory)              { FactoryGirl::Factory.new(:object) }
9 9
   let(:other_factory)        { FactoryGirl::Factory.new(:string) }
20  spec/factory_girl/sequence_spec.rb
@@ -25,11 +25,27 @@
25 25
     end
26 26
   end
27 27
 
28  
-  describe "a sequence with multiple names (aliases)" do
  28
+  describe "a sequence with aliases using default value" do
  29
+    let(:name) { :test }
  30
+    let(:aliases) { [:alias, :other] }
  31
+    subject    { FactoryGirl::Sequence.new(name, :aliases => aliases) {|n| "=#{n}" } }
  32
+
  33
+    its(:name)  { should == name }
  34
+    its(:value) { should == 1 }
  35
+    its(:names) { should == ([name] + aliases) }
  36
+    its(:next)  { should == "=1" }
  37
+
  38
+    describe "when incrementing" do
  39
+      before     { subject.next }
  40
+      its(:next) { should == "=2" }
  41
+    end
  42
+  end
  43
+
  44
+  describe "a sequence with custom value and aliases" do
29 45
     let(:name) { :test }
30 46
     let(:value) { 3 }
31 47
     let(:aliases) { [:alias, :other] }
32  
-    subject    { FactoryGirl::Sequence.new(name, :alias, :other, value) {|n| "=#{n}" } }
  48
+    subject    { FactoryGirl::Sequence.new(name, value, :aliases => aliases) {|n| "=#{n}" } }
33 49
 
34 50
     its(:name)  { should == name }
35 51
     its(:value) { should == value }

0 notes on commit 178a7ab

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