fabrication 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +6 -2
- data/lib/fabrication.rb +32 -5
- data/lib/fabrication/generator/active_record.rb +1 -1
- data/lib/fabrication/schematic.rb +1 -1
- data/lib/fabrication/support.rb +1 -1
- data/lib/fabrication/version.rb +1 -1
- data/spec/benchmark_spec.rb +11 -0
- data/spec/fabrication_spec.rb +21 -0
- metadata +5 -4
data/README.markdown
CHANGED
@@ -80,6 +80,10 @@ 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
|
+
You can also fabricate the object as an attribute hash instead of an actual instance. This is useful for controller or API testing where the receiver wants a hash representation of the object. If you have activesupport it will be a HashWithIndifferentAccess, otherwise it will be a regular Ruby Hash.
|
84
|
+
|
85
|
+
Fabricate.attributes_for(:company)
|
86
|
+
|
83
87
|
### Sequences ###
|
84
88
|
|
85
89
|
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.
|
@@ -100,12 +104,12 @@ And in a semi-real use case, it would look something like this:
|
|
100
104
|
|
101
105
|
Fabricate(:person) do
|
102
106
|
ssn { Fabricate.sequence :ssn, 111111111 }
|
103
|
-
email { Fabricate.sequence
|
107
|
+
email { Fabricate.sequence(:email) { |i| "user#{i}@example.com" } }
|
104
108
|
end
|
105
109
|
|
106
110
|
### Contributing ###
|
107
111
|
|
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.
|
112
|
+
I (paulelliott) am actively maintaining this project. If you would like to contribute, please fork the project, make your changes on a feature branch, and submit a pull request.
|
109
113
|
|
110
114
|
To run rake successfully:
|
111
115
|
|
data/lib/fabrication.rb
CHANGED
@@ -44,6 +44,14 @@ module Fabrication
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
def attributes_for(name, options)
|
48
|
+
hash = defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess.new : {}
|
49
|
+
fetch_schematic(name).attributes.inject(hash) do |hash, attribute|
|
50
|
+
value = attribute.value.respond_to?(:call) ? attribute.value.call : attribute.value
|
51
|
+
hash.merge(attribute.name => value)
|
52
|
+
end.merge(options)
|
53
|
+
end
|
54
|
+
|
47
55
|
def find_definitions
|
48
56
|
fabricator_file_paths = [
|
49
57
|
File.join('test', 'fabricators'),
|
@@ -79,6 +87,17 @@ module Fabrication
|
|
79
87
|
|
80
88
|
@@fabricators = nil
|
81
89
|
|
90
|
+
def fetch_schematic(name)
|
91
|
+
if fabricator = fabricators[name]
|
92
|
+
fabricator.schematic
|
93
|
+
else
|
94
|
+
# force finding definitions after setting @@fabricators to an empty array
|
95
|
+
Fabrication.send(:class_variable_set, :@@fabricators, nil) if Fabrication.fabricators.empty?
|
96
|
+
build(name)
|
97
|
+
fetch_schematic(name)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
82
101
|
end
|
83
102
|
|
84
103
|
end
|
@@ -93,12 +112,20 @@ end
|
|
93
112
|
|
94
113
|
class Fabricate
|
95
114
|
|
96
|
-
|
97
|
-
|
98
|
-
|
115
|
+
class << self
|
116
|
+
|
117
|
+
def build(name, options={}, &block)
|
118
|
+
Fabrication.generate(name, {:save => false}, options, &block)
|
119
|
+
end
|
120
|
+
|
121
|
+
def sequence(name, start=0, &block)
|
122
|
+
Fabrication.sequence(name, start, &block)
|
123
|
+
end
|
124
|
+
|
125
|
+
def attributes_for(name, options={})
|
126
|
+
Fabrication.attributes_for(name, options)
|
127
|
+
end
|
99
128
|
|
100
|
-
def self.sequence(name, start=0, &block)
|
101
|
-
Fabrication.sequence(name, start, &block)
|
102
129
|
end
|
103
130
|
|
104
131
|
end
|
@@ -19,7 +19,7 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
|
|
19
19
|
instance.instance_eval %<
|
20
20
|
def #{method_name}
|
21
21
|
original_value = @__#{method_name}_original.call
|
22
|
-
if @__#{method_name}_block
|
22
|
+
if @__#{method_name}_block
|
23
23
|
if #{count} \>= 1
|
24
24
|
original_value = #{method_name}= (1..#{count}).map { |i| @__#{method_name}_block.call(self, i) }
|
25
25
|
else
|
@@ -35,7 +35,7 @@ class Fabrication::Schematic
|
|
35
35
|
|
36
36
|
def method_missing(method_name, *args, &block)
|
37
37
|
method_name = parse_method_name(method_name, args)
|
38
|
-
if (attr = attribute(method_name))
|
38
|
+
if (attr = attribute(method_name))
|
39
39
|
if block_given?
|
40
40
|
attr.params = args.first
|
41
41
|
attr.value = block
|
data/lib/fabrication/support.rb
CHANGED
data/lib/fabrication/version.rb
CHANGED
data/spec/fabrication_spec.rb
CHANGED
@@ -333,4 +333,25 @@ describe Fabrication do
|
|
333
333
|
|
334
334
|
end
|
335
335
|
|
336
|
+
describe "Fabricate.attributes_for" do
|
337
|
+
|
338
|
+
before(:all) do
|
339
|
+
Fabricator(:person) do
|
340
|
+
first_name "Paul"
|
341
|
+
last_name { "Elliott" }
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
let(:person) { Fabricate.attributes_for(:person) }
|
346
|
+
|
347
|
+
it 'has the first name as a parameter' do
|
348
|
+
person['first_name'].should == "Paul"
|
349
|
+
end
|
350
|
+
|
351
|
+
it 'has the last name as a parameter' do
|
352
|
+
person[:last_name].should == "Elliott"
|
353
|
+
end
|
354
|
+
|
355
|
+
end
|
356
|
+
|
336
357
|
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: 7
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 6
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.6.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-31 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -133,6 +133,7 @@ files:
|
|
133
133
|
- lib/fabrication/support.rb
|
134
134
|
- lib/fabrication/version.rb
|
135
135
|
- lib/fabrication.rb
|
136
|
+
- spec/benchmark_spec.rb
|
136
137
|
- spec/fabrication_spec.rb
|
137
138
|
- spec/fabricator_spec.rb
|
138
139
|
- spec/generator/active_record_spec.rb
|