fabrication 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +25 -0
- data/lib/fabrication.rb +17 -0
- data/lib/fabrication/version.rb +1 -1
- data/spec/sequence_spec.rb +62 -0
- metadata +5 -4
data/README.markdown
CHANGED
@@ -80,6 +80,29 @@ Sometimes you don't actually need to save an option when it is created but just
|
|
80
80
|
|
81
81
|
Fabricate.build(:company, :name => "Hashrocket")
|
82
82
|
|
83
|
+
### Sequences ###
|
84
|
+
|
85
|
+
Sometimes you need a sequence of numbers while you're generating data. Fabrication provides you with an easy and flexible means for keeping track of sequences.
|
86
|
+
|
87
|
+
This will create a sequence named ":number" that will start at 0. Every time you call it, it will increment by one.
|
88
|
+
|
89
|
+
Fabricate.sequence(:number)
|
90
|
+
|
91
|
+
If you need to override the starting number, you can do it with a second parameter. It will always return the seed number on the first call and it will be ignored with subsequent calls.
|
92
|
+
|
93
|
+
Fabricate.sequence(:number, 99)
|
94
|
+
|
95
|
+
If you are generating something like an email address, you can pass it a block and the block response will be returned.
|
96
|
+
|
97
|
+
Fabricate.sequence(:name) { |i| "Name #{i}" }
|
98
|
+
|
99
|
+
And in a semi-real use case, it would look something like this:
|
100
|
+
|
101
|
+
Fabricate(:person) do
|
102
|
+
ssn { Fabricate.sequence :ssn, 111111111 }
|
103
|
+
email { Fabricate.sequence :email { |i| "user#{i}@example.com" } }
|
104
|
+
end
|
105
|
+
|
83
106
|
### Contributing ###
|
84
107
|
|
85
108
|
I (paulelliott) am actively maintaining this project. If you would like contribute, please fork the project, make your changes on a feature branch, and submit a pull request.
|
@@ -104,3 +127,5 @@ Fabricator(:atlanta_hub, :from => :hub) do
|
|
104
127
|
end
|
105
128
|
end
|
106
129
|
|
130
|
+
|
131
|
+
build option cascades down to associations
|
data/lib/fabrication.rb
CHANGED
@@ -36,6 +36,14 @@ module Fabrication
|
|
36
36
|
fabricators[name].fabricate(options, overrides, &block)
|
37
37
|
end
|
38
38
|
|
39
|
+
def sequence(name, start=0)
|
40
|
+
idx = sequences[name] ||= start
|
41
|
+
|
42
|
+
(block_given? ? yield(idx) : idx).tap do
|
43
|
+
sequences[name] += 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
39
47
|
def find_definitions
|
40
48
|
fabricator_file_paths = [
|
41
49
|
File.join('test', 'fabricators'),
|
@@ -56,12 +64,17 @@ module Fabrication
|
|
56
64
|
|
57
65
|
def clear_definitions
|
58
66
|
fabricators.clear
|
67
|
+
sequences.clear
|
59
68
|
end
|
60
69
|
|
61
70
|
def fabricators
|
62
71
|
@@fabricators ||= {}
|
63
72
|
end
|
64
73
|
|
74
|
+
def sequences
|
75
|
+
@@sequences ||= {}
|
76
|
+
end
|
77
|
+
|
65
78
|
private
|
66
79
|
|
67
80
|
@@fabricators = nil
|
@@ -84,4 +97,8 @@ class Fabricate
|
|
84
97
|
Fabrication.generate(name, {:save => false}, options, &block)
|
85
98
|
end
|
86
99
|
|
100
|
+
def self.sequence(name, start=0, &block)
|
101
|
+
Fabrication.sequence(name, start, &block)
|
102
|
+
end
|
103
|
+
|
87
104
|
end
|
data/lib/fabrication/version.rb
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Fabricate.sequence" do
|
4
|
+
|
5
|
+
context 'with only a name' do
|
6
|
+
|
7
|
+
it 'starts with 0' do
|
8
|
+
Fabricate.sequence(:incr).should == 0
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'increments by one with each call' do
|
12
|
+
Fabricate.sequence(:incr).should == 1
|
13
|
+
Fabricate.sequence(:incr).should == 2
|
14
|
+
Fabricate.sequence(:incr).should == 3
|
15
|
+
Fabricate.sequence(:incr).should == 4
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'increments counters separately' do
|
19
|
+
Fabricate.sequence(:number).should == 0
|
20
|
+
Fabricate.sequence(:number).should == 1
|
21
|
+
Fabricate.sequence(:number).should == 2
|
22
|
+
Fabricate.sequence(:number).should == 3
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with a name and starting number' do
|
28
|
+
|
29
|
+
it 'starts with the number provided' do
|
30
|
+
Fabricate.sequence(:incr, 69).should == 69
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'increments by one with each call' do
|
34
|
+
Fabricate.sequence(:incr).should == 70
|
35
|
+
Fabricate.sequence(:incr, 69).should == 71
|
36
|
+
Fabricate.sequence(:incr).should == 72
|
37
|
+
Fabricate.sequence(:incr).should == 73
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'with a block' do
|
43
|
+
|
44
|
+
it 'yields the number to the block and returns the value' do
|
45
|
+
Fabricate.sequence(:email) do |i|
|
46
|
+
"user#{i}@example.com"
|
47
|
+
end.should == "user0@example.com"
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'increments by one with each call' do
|
51
|
+
Fabricate.sequence(:email) do |i|
|
52
|
+
"user#{i}@example.com"
|
53
|
+
end.should == "user1@example.com"
|
54
|
+
|
55
|
+
Fabricate.sequence(:email) do |i|
|
56
|
+
"user#{i}@example.com"
|
57
|
+
end.should == "user2@example.com"
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fabrication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Paul Elliott
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-29 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -139,6 +139,7 @@ files:
|
|
139
139
|
- spec/generator/base_spec.rb
|
140
140
|
- spec/generator/mongoid_spec.rb
|
141
141
|
- spec/schematic_spec.rb
|
142
|
+
- spec/sequence_spec.rb
|
142
143
|
- spec/spec.opts
|
143
144
|
- spec/spec_helper.rb
|
144
145
|
- spec/support/active_record.rb
|