fabrication 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -0
- data/lib/fabrication.rb +1 -0
- data/lib/fabrication/attribute.rb +0 -5
- data/lib/fabrication/generator/active_record.rb +2 -41
- data/lib/fabrication/generator/base.rb +38 -2
- data/lib/fabrication/generator/mongoid.rb +0 -4
- data/lib/fabrication/generator/sequel.rb +7 -0
- data/lib/fabrication/schematic.rb +17 -20
- data/lib/fabrication/version.rb +1 -1
- data/lib/rails/generators/fabrication/model/model_generator.rb +1 -1
- data/spec/fabrication/attribute_spec.rb +0 -54
- data/spec/fabrication/generator/sequel_spec.rb +32 -0
- data/spec/fabrication/schematic_spec.rb +28 -0
- data/spec/fabrication_spec.rb +5 -0
- data/spec/support/sequel.rb +13 -0
- metadata +33 -26
data/README.markdown
CHANGED
@@ -7,6 +7,7 @@ Currently supported object types are...
|
|
7
7
|
* Plain old Ruby objects
|
8
8
|
* ActiveRecord objects
|
9
9
|
* Mongoid Documents
|
10
|
+
* Sequel Models
|
10
11
|
|
11
12
|
By default it will lazily generate active record associations. So if you have a has_many :widgets defined, it will not actually generate the widgets until the association is accessed. You can override this by appending "!" to the name of the parameter when defining the field in the Fabricator.
|
12
13
|
|
@@ -167,3 +168,4 @@ To run rake successfully:
|
|
167
168
|
* Justin Smestad (jsmestad)
|
168
169
|
* Christopher Hanks (chanks) - Chained callbacks
|
169
170
|
* monsterlabs - Rails 3 Generators
|
171
|
+
* Brandon Weiss (brandonweiss) - Sequal Model support
|
data/lib/fabrication.rb
CHANGED
@@ -13,6 +13,7 @@ module Fabrication
|
|
13
13
|
module Generator
|
14
14
|
autoload :ActiveRecord, 'fabrication/generator/active_record'
|
15
15
|
autoload :Mongoid, 'fabrication/generator/mongoid'
|
16
|
+
autoload :Sequel, 'fabrication/generator/sequel'
|
16
17
|
autoload :Base, 'fabrication/generator/base'
|
17
18
|
end
|
18
19
|
|
@@ -8,47 +8,8 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
|
|
8
8
|
@associations ||= klass.reflections.keys
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
method_name
|
13
|
-
if block_given?
|
14
|
-
options = args.first || {}
|
15
|
-
if !options[:force] && associations.include?(method_name.to_sym)
|
16
|
-
count = options[:count] || 0
|
17
|
-
|
18
|
-
# copy the original getter
|
19
|
-
instance.instance_variable_set("@__#{method_name}_original", instance.method(method_name).clone)
|
20
|
-
|
21
|
-
# store the block for lazy generation
|
22
|
-
instance.instance_variable_set("@__#{method_name}_block", block)
|
23
|
-
|
24
|
-
# redefine the getter
|
25
|
-
instance.instance_eval %<
|
26
|
-
def #{method_name}
|
27
|
-
original_value = @__#{method_name}_original.call
|
28
|
-
if @__#{method_name}_block
|
29
|
-
if #{count} \>= 1
|
30
|
-
original_value = #{method_name}= (1..#{count}).map { |i| @__#{method_name}_block.call(self, i) }
|
31
|
-
else
|
32
|
-
original_value = #{method_name}= @__#{method_name}_block.call(self)
|
33
|
-
end
|
34
|
-
@__#{method_name}_block = nil
|
35
|
-
@__#{method_name}_original.call
|
36
|
-
end
|
37
|
-
original_value
|
38
|
-
end
|
39
|
-
>
|
40
|
-
else
|
41
|
-
assign(method_name, options, &block)
|
42
|
-
end
|
43
|
-
else
|
44
|
-
assign(method_name, args.first)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
protected
|
49
|
-
|
50
|
-
def after_generation(options)
|
51
|
-
instance.save if options[:save]
|
11
|
+
def association?(method_name)
|
12
|
+
associations.include?(method_name.to_sym)
|
52
13
|
end
|
53
14
|
|
54
15
|
end
|
@@ -22,15 +22,51 @@ class Fabrication::Generator::Base
|
|
22
22
|
self.klass = klass
|
23
23
|
end
|
24
24
|
|
25
|
+
def association?(method_name); false end
|
26
|
+
|
25
27
|
def method_missing(method_name, *args, &block)
|
26
|
-
|
28
|
+
if block_given?
|
29
|
+
options = args.first || {}
|
30
|
+
if !options[:force] && association?(method_name)
|
31
|
+
method_name = method_name.to_s
|
32
|
+
count = options[:count] || 0
|
33
|
+
|
34
|
+
# copy the original getter
|
35
|
+
instance.instance_variable_set("@__#{method_name}_original", instance.method(method_name))
|
36
|
+
|
37
|
+
# store the block for lazy generation
|
38
|
+
instance.instance_variable_set("@__#{method_name}_block", block)
|
39
|
+
|
40
|
+
# redefine the getter
|
41
|
+
instance.instance_eval %<
|
42
|
+
def #{method_name}
|
43
|
+
original_value = @__#{method_name}_original.call
|
44
|
+
if @__#{method_name}_block
|
45
|
+
if #{count} \>= 1
|
46
|
+
original_value = #{method_name}= (1..#{count}).map { |i| @__#{method_name}_block.call(self, i) }
|
47
|
+
else
|
48
|
+
original_value = #{method_name}= @__#{method_name}_block.call(self)
|
49
|
+
end
|
50
|
+
@__#{method_name}_block = nil
|
51
|
+
end
|
52
|
+
original_value
|
53
|
+
end
|
54
|
+
>
|
55
|
+
else
|
56
|
+
assign(method_name, options, &block)
|
57
|
+
end
|
58
|
+
else
|
59
|
+
assign(method_name, args.first)
|
60
|
+
end
|
27
61
|
end
|
28
62
|
|
29
63
|
protected
|
30
64
|
|
31
65
|
attr_accessor :klass, :instance
|
32
66
|
|
33
|
-
def after_generation(options)
|
67
|
+
def after_generation(options)
|
68
|
+
instance.save if options[:save] && instance.respond_to?(:save)
|
69
|
+
end
|
34
70
|
|
35
71
|
def assign(method_name, param)
|
36
72
|
if param.is_a?(Hash) && param.has_key?(:count)
|
@@ -3,6 +3,7 @@ class Fabrication::Schematic
|
|
3
3
|
GENERATORS = [
|
4
4
|
Fabrication::Generator::ActiveRecord,
|
5
5
|
Fabrication::Generator::Mongoid,
|
6
|
+
Fabrication::Generator::Sequel,
|
6
7
|
Fabrication::Generator::Base
|
7
8
|
]
|
8
9
|
|
@@ -27,6 +28,10 @@ class Fabrication::Schematic
|
|
27
28
|
attributes.select { |a| a.name == name }.first
|
28
29
|
end
|
29
30
|
|
31
|
+
def delete_attribute(name)
|
32
|
+
attributes.reject! { |a| a.name == name }
|
33
|
+
end
|
34
|
+
|
30
35
|
attr_writer :attributes
|
31
36
|
def attributes
|
32
37
|
@attributes ||= []
|
@@ -41,7 +46,7 @@ class Fabrication::Schematic
|
|
41
46
|
new_schematic = merge(overrides, &block)
|
42
47
|
new_schematic.instance_eval do
|
43
48
|
if options[:attributes]
|
44
|
-
to_hash(attributes,
|
49
|
+
to_hash(generator.new(klass).generate({:save => false}, attributes, callbacks))
|
45
50
|
else
|
46
51
|
generator.new(klass).generate(options, attributes, callbacks)
|
47
52
|
end
|
@@ -54,9 +59,7 @@ class Fabrication::Schematic
|
|
54
59
|
self.callbacks[type] = callbacks.clone
|
55
60
|
end
|
56
61
|
|
57
|
-
self.attributes = original.attributes.
|
58
|
-
Fabrication::Attribute.new(a.name, a.params, a.value)
|
59
|
-
end
|
62
|
+
self.attributes = original.attributes.clone
|
60
63
|
end
|
61
64
|
|
62
65
|
def init_with(*args)
|
@@ -67,11 +70,8 @@ class Fabrication::Schematic
|
|
67
70
|
clone.tap do |schematic|
|
68
71
|
schematic.instance_eval(&block) if block_given?
|
69
72
|
overrides.each do |name, value|
|
70
|
-
|
71
|
-
|
72
|
-
else
|
73
|
-
schematic.attributes << Fabrication::Attribute.new(name, nil, value)
|
74
|
-
end
|
73
|
+
schematic.delete_attribute(name)
|
74
|
+
schematic.attributes << Fabrication::Attribute.new(name, nil, value)
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
@@ -86,11 +86,8 @@ class Fabrication::Schematic
|
|
86
86
|
value = args.first
|
87
87
|
end
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
else
|
92
|
-
attributes.push(Fabrication::Attribute.new(method_name, params, value))
|
93
|
-
end
|
89
|
+
delete_attribute(method_name)
|
90
|
+
attributes.push(Fabrication::Attribute.new(method_name, params, value))
|
94
91
|
end
|
95
92
|
|
96
93
|
def on_init(&block)
|
@@ -115,12 +112,12 @@ class Fabrication::Schematic
|
|
115
112
|
Proc.new { Fabricate(name.to_sym) }
|
116
113
|
end
|
117
114
|
|
118
|
-
def to_hash(
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
end
|
115
|
+
def to_hash(object)
|
116
|
+
(defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess.new : {}).tap do |hash|
|
117
|
+
attributes.map do |attribute|
|
118
|
+
hash[attribute.name] = object.send(attribute.name)
|
119
|
+
end
|
120
|
+
end
|
124
121
|
end
|
125
122
|
|
126
123
|
end
|
data/lib/fabrication/version.rb
CHANGED
@@ -8,7 +8,7 @@ module Fabrication
|
|
8
8
|
class_option :extension, :type => :string, :default => "rb", :desc => "file extension name"
|
9
9
|
|
10
10
|
def create_fabrication_file
|
11
|
-
template 'fabricator.rb', File.join(options[:dir], "#{
|
11
|
+
template 'fabricator.rb', File.join(options[:dir], "#{singular_table_name}_fabricator.#{options[:extension].to_s}")
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -50,58 +50,4 @@ describe Fabrication::Attribute do
|
|
50
50
|
|
51
51
|
end
|
52
52
|
|
53
|
-
describe "#update!" do
|
54
|
-
|
55
|
-
let(:attr) do
|
56
|
-
Fabrication::Attribute.new("a", nil, nil)
|
57
|
-
end
|
58
|
-
|
59
|
-
context "with name, params, and a static value" do
|
60
|
-
|
61
|
-
subject { attr }
|
62
|
-
before do
|
63
|
-
attr.update!(:params => { :b => :c }, :value => "d")
|
64
|
-
end
|
65
|
-
|
66
|
-
its(:params) { should == { :b => :c } }
|
67
|
-
its(:value) { should == "d" }
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
context "with a block value" do
|
72
|
-
|
73
|
-
before do
|
74
|
-
attr.update!(:value => Proc.new { "c" })
|
75
|
-
end
|
76
|
-
|
77
|
-
it "has a proc for a value" do
|
78
|
-
Proc.should === attr.value
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
context "with a nil value" do
|
84
|
-
|
85
|
-
subject { attr }
|
86
|
-
before do
|
87
|
-
attr.update!(:value => nil)
|
88
|
-
end
|
89
|
-
|
90
|
-
its(:value) { should be_nil }
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
context "with nil params" do
|
95
|
-
|
96
|
-
subject { attr }
|
97
|
-
before do
|
98
|
-
attr.update!(:params => nil)
|
99
|
-
end
|
100
|
-
|
101
|
-
its(:params) { should be_nil }
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
53
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Fabrication::Generator::Sequel do
|
4
|
+
|
5
|
+
describe ".supports?" do
|
6
|
+
subject { Fabrication::Generator::Sequel }
|
7
|
+
it "returns true for sequel objects" do
|
8
|
+
subject.supports?(Artist).should be_true
|
9
|
+
end
|
10
|
+
it "returns false for non-sequel objects" do
|
11
|
+
subject.supports?(Person).should be_false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#after_generation" do
|
16
|
+
let(:instance) { mock(:instance) }
|
17
|
+
let(:generator) { Fabrication::Generator::Sequel.new(Object) }
|
18
|
+
|
19
|
+
before { generator.send(:instance=, instance) }
|
20
|
+
|
21
|
+
it "saves with a true save flag" do
|
22
|
+
instance.should_receive(:save)
|
23
|
+
generator.send(:after_generation, {:save => true})
|
24
|
+
end
|
25
|
+
|
26
|
+
it "does not save without a true save flag" do
|
27
|
+
instance.should_not_receive(:save)
|
28
|
+
generator.send(:after_generation, {})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -10,6 +10,24 @@ describe Fabrication::Schematic do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
describe "generator selection" do
|
14
|
+
context "for an activerecord object" do
|
15
|
+
it "uses the activerecord generator" do
|
16
|
+
Fabrication::Schematic.new(Division).generator.should == Fabrication::Generator::ActiveRecord
|
17
|
+
end
|
18
|
+
end
|
19
|
+
context "for a mongoid object" do
|
20
|
+
it "uses the mongoid generator" do
|
21
|
+
Fabrication::Schematic.new(Author).generator.should == Fabrication::Generator::Mongoid
|
22
|
+
end
|
23
|
+
end
|
24
|
+
context "for a sequel object" do
|
25
|
+
it "uses the sequel generator" do
|
26
|
+
Fabrication::Schematic.new(Artist).generator.should == Fabrication::Generator::Sequel
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
13
31
|
describe ".new" do
|
14
32
|
it "stores the klass" do
|
15
33
|
schematic.klass.should == OpenStruct
|
@@ -31,6 +49,16 @@ describe Fabrication::Schematic do
|
|
31
49
|
end
|
32
50
|
end
|
33
51
|
|
52
|
+
describe "#delete_attribute" do
|
53
|
+
it "deletes the requested attribute if it exists" do
|
54
|
+
schematic.delete_attribute(:name).should_not be_nil
|
55
|
+
schematic.attribute(:name).should be_nil
|
56
|
+
end
|
57
|
+
it "returns nil if it does not exist" do
|
58
|
+
schematic.delete_attribute(:not_there).should be_nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
34
62
|
describe "#attributes" do
|
35
63
|
it "always returns an empty array" do
|
36
64
|
schematic.attributes = nil
|
data/spec/fabrication_spec.rb
CHANGED
@@ -125,6 +125,7 @@ describe Fabrication do
|
|
125
125
|
Fabricate(:person) do
|
126
126
|
first_name "Paul"
|
127
127
|
last_name { |person| "#{person.first_name}#{person.age}" }
|
128
|
+
age 50
|
128
129
|
end
|
129
130
|
end
|
130
131
|
|
@@ -382,6 +383,10 @@ describe Fabrication do
|
|
382
383
|
person[:last_name].should == "Smith"
|
383
384
|
end
|
384
385
|
|
386
|
+
it 'has the fabricator provided attributes' do
|
387
|
+
person[:shoes].length.should == 10
|
388
|
+
end
|
389
|
+
|
385
390
|
end
|
386
391
|
|
387
392
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fabrication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 59
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
8
|
+
- 1
|
9
|
+
version: 0.9.1
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Paul Elliott
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-11-21 00:00:00 -05:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,14 +25,11 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 62196427
|
30
28
|
segments:
|
31
29
|
- 2
|
30
|
+
- 1
|
32
31
|
- 0
|
33
|
-
|
34
|
-
- beta
|
35
|
-
- 20
|
36
|
-
version: 2.0.0.beta.20
|
32
|
+
version: 2.1.0
|
37
33
|
type: :development
|
38
34
|
version_requirements: *id001
|
39
35
|
- !ruby/object:Gem::Dependency
|
@@ -44,7 +40,6 @@ dependencies:
|
|
44
40
|
requirements:
|
45
41
|
- - ">="
|
46
42
|
- !ruby/object:Gem::Version
|
47
|
-
hash: 15
|
48
43
|
segments:
|
49
44
|
- 0
|
50
45
|
- 4
|
@@ -60,12 +55,11 @@ dependencies:
|
|
60
55
|
requirements:
|
61
56
|
- - ">="
|
62
57
|
- !ruby/object:Gem::Version
|
63
|
-
hash: 7
|
64
58
|
segments:
|
65
59
|
- 3
|
66
60
|
- 0
|
67
|
-
-
|
68
|
-
version: 3.0.
|
61
|
+
- 1
|
62
|
+
version: 3.0.1
|
69
63
|
type: :development
|
70
64
|
version_requirements: *id003
|
71
65
|
- !ruby/object:Gem::Dependency
|
@@ -76,12 +70,11 @@ dependencies:
|
|
76
70
|
requirements:
|
77
71
|
- - ">="
|
78
72
|
- !ruby/object:Gem::Version
|
79
|
-
hash: 27
|
80
73
|
segments:
|
81
74
|
- 1
|
82
75
|
- 3
|
83
|
-
-
|
84
|
-
version: 1.3.
|
76
|
+
- 1
|
77
|
+
version: 1.3.1
|
85
78
|
type: :development
|
86
79
|
version_requirements: *id004
|
87
80
|
- !ruby/object:Gem::Dependency
|
@@ -92,12 +85,11 @@ dependencies:
|
|
92
85
|
requirements:
|
93
86
|
- - "="
|
94
87
|
- !ruby/object:Gem::Version
|
95
|
-
hash: 31
|
96
88
|
segments:
|
97
89
|
- 1
|
98
|
-
-
|
99
|
-
-
|
100
|
-
version: 1.
|
90
|
+
- 1
|
91
|
+
- 2
|
92
|
+
version: 1.1.2
|
101
93
|
type: :development
|
102
94
|
version_requirements: *id005
|
103
95
|
- !ruby/object:Gem::Dependency
|
@@ -108,17 +100,31 @@ dependencies:
|
|
108
100
|
requirements:
|
109
101
|
- - "="
|
110
102
|
- !ruby/object:Gem::Version
|
111
|
-
hash: 62196423
|
112
103
|
segments:
|
113
104
|
- 2
|
114
105
|
- 0
|
115
106
|
- 0
|
116
107
|
- beta
|
117
|
-
-
|
118
|
-
version: 2.0.0.beta.
|
108
|
+
- 20
|
109
|
+
version: 2.0.0.beta.20
|
119
110
|
type: :development
|
120
111
|
version_requirements: *id006
|
121
|
-
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: sequel
|
114
|
+
prerelease: false
|
115
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - "="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
segments:
|
121
|
+
- 3
|
122
|
+
- 16
|
123
|
+
- 0
|
124
|
+
version: 3.16.0
|
125
|
+
type: :development
|
126
|
+
version_requirements: *id007
|
127
|
+
description: Fabrication is an object generation framework for ActiveRecord, Mongoid, and Sequel. It has a sensible syntax and lazily generates ActiveRecord associations!
|
122
128
|
email:
|
123
129
|
- paul@hashrocket.com
|
124
130
|
executables: []
|
@@ -136,6 +142,7 @@ files:
|
|
136
142
|
- lib/fabrication/generator/active_record.rb
|
137
143
|
- lib/fabrication/generator/base.rb
|
138
144
|
- lib/fabrication/generator/mongoid.rb
|
145
|
+
- lib/fabrication/generator/sequel.rb
|
139
146
|
- lib/fabrication/schematic.rb
|
140
147
|
- lib/fabrication/sequencer.rb
|
141
148
|
- lib/fabrication/support.rb
|
@@ -149,6 +156,7 @@ files:
|
|
149
156
|
- spec/fabrication/generator/active_record_spec.rb
|
150
157
|
- spec/fabrication/generator/base_spec.rb
|
151
158
|
- spec/fabrication/generator/mongoid_spec.rb
|
159
|
+
- spec/fabrication/generator/sequel_spec.rb
|
152
160
|
- spec/fabrication/schematic_spec.rb
|
153
161
|
- spec/fabrication/sequence_spec.rb
|
154
162
|
- spec/fabrication/support_spec.rb
|
@@ -159,6 +167,7 @@ files:
|
|
159
167
|
- spec/support/active_record.rb
|
160
168
|
- spec/support/helper_objects.rb
|
161
169
|
- spec/support/mongoid.rb
|
170
|
+
- spec/support/sequel.rb
|
162
171
|
- README.markdown
|
163
172
|
has_rdoc: true
|
164
173
|
homepage: http://github.com/paulelliott/fabrication
|
@@ -174,7 +183,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
183
|
requirements:
|
175
184
|
- - ">="
|
176
185
|
- !ruby/object:Gem::Version
|
177
|
-
hash: 3
|
178
186
|
segments:
|
179
187
|
- 0
|
180
188
|
version: "0"
|
@@ -183,7 +191,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
191
|
requirements:
|
184
192
|
- - ">="
|
185
193
|
- !ruby/object:Gem::Version
|
186
|
-
hash: 3
|
187
194
|
segments:
|
188
195
|
- 0
|
189
196
|
version: "0"
|