fabrication 0.0.3 → 0.1.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 CHANGED
@@ -0,0 +1,3 @@
1
+ ### Fabrication ###
2
+
3
+
data/lib/fabrication.rb CHANGED
@@ -15,9 +15,28 @@ module Fabrication
15
15
  end
16
16
 
17
17
  def generate(name, options)
18
+ find_definitions if fabricators.empty?
18
19
  fabricators[name].fabricate(options)
19
20
  end
20
21
 
22
+ def find_definitions
23
+ fabricator_file_paths = [
24
+ File.join('test', 'fabricators'),
25
+ File.join('spec', 'fabricators')
26
+ ]
27
+ fabricator_file_paths.each do |path|
28
+ if File.exists?("#{path}.rb")
29
+ require("#{path}.rb")
30
+ end
31
+
32
+ if File.directory? path
33
+ Dir[File.join(path, '*.rb')].each do |file|
34
+ require file
35
+ end
36
+ end
37
+ end
38
+ end
39
+
21
40
  private
22
41
 
23
42
  def fabricators
@@ -1,10 +1,10 @@
1
1
  class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
2
2
 
3
+ attr_accessor :instance, :options
4
+
3
5
  def generate(options)
4
- @options = options
5
- @instance = super
6
- @instance.save
7
- @instance
6
+ self.options = options
7
+ self.instance = super.tap { |t| t.save }
8
8
  end
9
9
 
10
10
  def self.supports?(klass)
@@ -13,21 +13,26 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
13
13
 
14
14
  def method_missing(method_name, *args, &block)
15
15
  method_name = method_name.to_s
16
- unless @options.include?(method_name.to_sym)
16
+ count = (args && args.first && args.first[:count]) || 1
17
+ unless options.include?(method_name.to_sym)
17
18
  if block_given?
18
- unless args.include?(:force) || @instance.class.columns.map(&:name).include?(method_name)
19
+ unless (args.first && args.first[:force]) || instance.class.columns.map(&:name).include?(method_name)
19
20
  # copy the original getter
20
- @instance.instance_variable_set("@__#{method_name}_original", @instance.method(method_name).clone)
21
+ instance.instance_variable_set("@__#{method_name}_original", instance.method(method_name).clone)
21
22
 
22
23
  # store the block for lazy generation
23
- @instance.instance_variable_set("@__#{method_name}_block", block)
24
+ instance.instance_variable_set("@__#{method_name}_block", block)
24
25
 
25
26
  # redefine the getter
26
- @instance.instance_eval %<
27
+ instance.instance_eval %<
27
28
  def #{method_name}
28
29
  original_value = @__#{method_name}_original.call
29
30
  if @__#{method_name}_block.present?
30
- original_value = #{method_name}= @__#{method_name}_block.call(self)
31
+ if #{count} \> 1
32
+ original_value = #{method_name}= (1..#{count}).map { |i| @__#{method_name}_block.call(self, i) }
33
+ else
34
+ original_value = #{method_name}= @__#{method_name}_block.call(self)
35
+ end
31
36
  @__#{method_name}_block = nil
32
37
  @__#{method_name}_original.call
33
38
  end
@@ -35,10 +40,10 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
35
40
  end
36
41
  >
37
42
  else
38
- assign(@instance, method_name, yield)
43
+ assign(instance, method_name, args.first, &block)
39
44
  end
40
45
  else
41
- assign(@instance, method_name, args.first)
46
+ assign(instance, method_name, args)
42
47
  end
43
48
  end
44
49
  end
@@ -1,26 +1,36 @@
1
1
  class Fabrication::Generator::Base
2
2
 
3
+ attr_accessor :klass, :block, :instance
4
+
3
5
  def initialize(klass, &block)
4
- @klass = klass
5
- @block = block
6
+ self.klass = klass
7
+ self.block = block
6
8
  end
7
9
 
8
10
  def generate(options)
9
- @instance = @klass.new
10
- instance_eval &@block
11
- options.each { |k,v| assign(@instance, k, v) }
12
- @instance
11
+ self.instance = klass.new
12
+ instance_eval &block
13
+ options.each { |k,v| assign(instance, k, v) }
14
+ instance
13
15
  end
14
16
 
15
17
  def method_missing(method_name, *args, &block)
16
- assign(@instance, method_name.to_s, block_given? ? yield : args.first)
18
+ assign(instance, method_name.to_s, args.first, &block)
17
19
  end
18
20
 
19
21
  def self.supports?(klass); true end
20
22
 
21
23
  private
22
24
 
23
- def assign(instance, method_name, value)
25
+ def assign(instance, method_name, param)
26
+ value = nil
27
+ if param.is_a?(Hash) && param[:count] && param[:count] > 1
28
+ value = (1..param[:count]).map do |i|
29
+ block_given? ? yield(i) : param
30
+ end
31
+ else
32
+ value = block_given? ? yield : param
33
+ end
24
34
  instance.send("#{method_name.to_s}=", value)
25
35
  end
26
36
 
@@ -1,10 +1,9 @@
1
1
  class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
2
2
 
3
+ attr_accessor :instance
4
+
3
5
  def generate(options)
4
- @options = options
5
- @instance = super
6
- @instance.save
7
- @instance
6
+ self.instance = super.tap { |t| t.save }
8
7
  end
9
8
 
10
9
  def self.supports?(klass)
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -2,6 +2,98 @@ require 'spec_helper'
2
2
 
3
3
  describe Fabrication do
4
4
 
5
+ context 'static fields' do
6
+
7
+ let(:person) do
8
+ Fabricate(:person, :last_name => 'Awesome')
9
+ end
10
+
11
+ before(:all) do
12
+ Fabricator(:person) do
13
+ first_name 'Joe'
14
+ last_name 'Schmoe'
15
+ age 78
16
+ end
17
+ end
18
+
19
+ it 'has the default first name' do
20
+ person.first_name.should == 'Joe'
21
+ end
22
+
23
+ it 'has an overridden last name' do
24
+ person.last_name.should == 'Awesome'
25
+ end
26
+
27
+ it 'has the default age' do
28
+ person.age.should == 78
29
+ end
30
+
31
+ it 'generates a fresh object every time' do
32
+ Fabricate(:person).should_not == person
33
+ end
34
+
35
+ end
36
+
37
+ context 'block generated fields' do
38
+
39
+ let(:person) do
40
+ Fabricate(:person)
41
+ end
42
+
43
+ before(:all) do
44
+ Fabricator(:person) do
45
+ first_name { Faker::Name.first_name }
46
+ last_name { Faker::Name.last_name }
47
+ age { rand(100) }
48
+ shoes(:count => 10) { |i| "shoe #{i}" }
49
+ end
50
+ end
51
+
52
+ it 'has a first name' do
53
+ person.first_name.should be
54
+ end
55
+
56
+ it 'has a last name' do
57
+ person.last_name.should be
58
+ end
59
+
60
+ it 'has an age' do
61
+ person.age.should be
62
+ end
63
+
64
+ it 'has 10 shoes' do
65
+ person.shoes.should == (1..10).map { |i| "shoe #{i}" }
66
+ end
67
+
68
+ end
69
+
70
+ context 'multiple instance' do
71
+
72
+ let(:person1) { Fabricate(:person, :first_name => 'Jane') }
73
+ let(:person2) { Fabricate(:person, :first_name => 'John') }
74
+
75
+ before(:all) do
76
+ Fabricator(:person) do
77
+ first_name { Faker::Name.first_name }
78
+ last_name { Faker::Name.last_name }
79
+ age { rand(100) }
80
+ end
81
+ end
82
+
83
+ it 'person1 is named Jane' do
84
+ person1.first_name.should == 'Jane'
85
+ end
86
+
87
+ it 'person2 is named John' do
88
+ person2.first_name.should == 'John'
89
+ end
90
+
91
+ it 'they have different last names' do
92
+ person1.last_name.should_not == person2.last_name
93
+ end
94
+
95
+ end
96
+
5
97
  context 'with an active record object' do
6
98
 
7
99
  before(:all) { TestMigration.up }
@@ -10,7 +102,8 @@ describe Fabrication do
10
102
  before do
11
103
  Fabricator(:company) do
12
104
  name { Faker::Company.name }
13
- divisions(:force) { [Fabricate(:division)] }
105
+ divisions(:force => true, :count => 4) { Fabricate(:division) }
106
+ after_create { |o| o.update_attribute(:city, "Jacksonville Beach") }
14
107
  end
15
108
 
16
109
  Fabricator(:division) do
@@ -25,12 +118,35 @@ describe Fabrication do
25
118
  end
26
119
 
27
120
  it 'generates associations immediately when forced' do
28
- Division.find_all_by_company_id(company.id).count.should == 1
121
+ Division.find_all_by_company_id(company.id).count.should == 4
29
122
  end
30
123
 
31
124
  it 'overrides associations' do
32
125
  Fabricate(:company, :divisions => []).divisions.should == []
33
126
  end
127
+
128
+ it 'executes after create blocks' do
129
+ company.city.should == 'Jacksonville Beach'
130
+ end
131
+
132
+ end
133
+
134
+ context 'with a mongoid document' do
135
+
136
+ let(:author) do
137
+ Fabricator(:author) do
138
+ name "George Orwell"
139
+ books(:count => 4) { |i| "book title #{i}" }
140
+ end.fabricate
141
+ end
142
+
143
+ it "sets the author name" do
144
+ author.name.should == "George Orwell"
145
+ end
146
+
147
+ it 'generates four books' do
148
+ author.books.should == (1..4).map { |i| "book title #{i}" }
149
+ end
34
150
  end
35
151
 
36
152
  end
@@ -11,6 +11,7 @@ class TestMigration < ActiveRecord::Migration
11
11
  def self.up
12
12
  create_table :companies, :force => true do |t|
13
13
  t.column :name, :string
14
+ t.column :city, :string
14
15
  end
15
16
 
16
17
  create_table :divisions, :force => true do |t|
@@ -1 +1,3 @@
1
- class Person; attr_accessor :age, :first_name, :last_name end
1
+ class Person
2
+ attr_accessor :age, :first_name, :last_name, :shoes
3
+ end
@@ -1,9 +1,10 @@
1
1
  Mongoid.configure do |config|
2
- config.allow_dynamic_fields = false
2
+ config.allow_dynamic_fields = true
3
3
  config.master = Mongo::Connection.new.db("fabrication_test")
4
4
  end
5
5
 
6
6
  class Author
7
7
  include Mongoid::Document
8
8
  field :name
9
+ field :books, :type => Array
9
10
  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: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.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-06-17 00:00:00 -04:00
18
+ date: 2010-06-19 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -114,14 +114,11 @@ files:
114
114
  - lib/fabrication/generator/mongoid.rb
115
115
  - lib/fabrication/version.rb
116
116
  - lib/fabrication.rb
117
- - spec/block_generated_fields_spec.rb
118
117
  - spec/fabrication_spec.rb
119
118
  - spec/fabricator_spec.rb
120
119
  - spec/generator/active_record_spec.rb
121
120
  - spec/generator/base_spec.rb
122
121
  - spec/generator/mongoid_spec.rb
123
- - spec/multiple_instance_spec.rb
124
- - spec/simple_object_spec.rb
125
122
  - spec/spec.opts
126
123
  - spec/spec_helper.rb
127
124
  - spec/support/active_record_setup.rb
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Fabrication do
4
-
5
- context 'block generated fields' do
6
-
7
- let(:person) do
8
- Fabricate(:person)
9
- end
10
-
11
- before(:all) do
12
- Fabricator(:person) do
13
- first_name { Faker::Name.first_name }
14
- last_name { Faker::Name.last_name }
15
- age { rand(100) }
16
- end
17
- end
18
-
19
- it 'has a first name' do
20
- person.first_name.should be
21
- end
22
-
23
- it 'has a last name' do
24
- person.last_name.should be
25
- end
26
-
27
- it 'has an age' do
28
- person.age.should be
29
- end
30
-
31
- end
32
-
33
- end
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Fabrication do
4
-
5
- context 'multiple instance' do
6
-
7
- let(:person1) { Fabricate(:person, :first_name => 'Jane') }
8
- let(:person2) { Fabricate(:person, :first_name => 'John') }
9
-
10
- before(:all) do
11
- Fabricator(:person) do
12
- first_name { Faker::Name.first_name }
13
- last_name { Faker::Name.last_name }
14
- age { rand(100) }
15
- end
16
- end
17
-
18
- it 'person1 is named Jane' do
19
- person1.first_name.should == 'Jane'
20
- end
21
-
22
- it 'person2 is named John' do
23
- person2.first_name.should == 'John'
24
- end
25
-
26
- it 'they have different last names' do
27
- person1.last_name.should_not == person2.last_name
28
- end
29
-
30
- end
31
-
32
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Fabrication do
4
-
5
- context 'static fields' do
6
-
7
- let(:person) do
8
- Fabricate(:person, :last_name => 'Awesome')
9
- end
10
-
11
- before(:all) do
12
- Fabricator(:person) do
13
- first_name 'Joe'
14
- last_name 'Schmoe'
15
- age 78
16
- end
17
- end
18
-
19
- it 'has the default first name' do
20
- person.first_name.should == 'Joe'
21
- end
22
-
23
- it 'has an overridden last name' do
24
- person.last_name.should == 'Awesome'
25
- end
26
-
27
- it 'has the default age' do
28
- person.age.should == 78
29
- end
30
-
31
- it 'generates a fresh object every time' do
32
- Fabricate(:person).should_not == person
33
- end
34
-
35
- end
36
-
37
- end