indirect-machinist 2.0.0.beta3

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.
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe Machinist::Blueprint do
5
+
6
+ it "makes an object of the given class" do
7
+ blueprint = Machinist::Blueprint.new(OpenStruct) { }
8
+ blueprint.make.should be_an(OpenStruct)
9
+ end
10
+
11
+ it "constructs an attribute from the blueprint" do
12
+ blueprint = Machinist::Blueprint.new(OpenStruct) do
13
+ name { "Fred" }
14
+ end
15
+ blueprint.make.name.should == "Fred"
16
+ end
17
+
18
+ it "constructs an array for an attribute in the blueprint" do
19
+ blueprint = Machinist::Blueprint.new(OpenStruct) do
20
+ things(3) { Object.new }
21
+ end
22
+ things = blueprint.make.things
23
+ things.should be_an(Array)
24
+ things.should have(3).elements
25
+ things.each {|thing| thing.should be_an(Object) }
26
+ things.uniq.should == things
27
+ end
28
+
29
+ it "allows passing in attributes to override the blueprint" do
30
+ block_called = false
31
+ blueprint = Machinist::Blueprint.new(OpenStruct) do
32
+ name { block_called = true; "Fred" }
33
+ end
34
+ blueprint.make(:name => "Bill").name.should == "Bill"
35
+ block_called.should be_false
36
+ end
37
+
38
+ it "provides a serial number within the blueprint" do
39
+ blueprint = Machinist::Blueprint.new(OpenStruct) do
40
+ name { "Fred #{sn}" }
41
+ end
42
+ blueprint.make.name.should == "Fred 0001"
43
+ blueprint.make.name.should == "Fred 0002"
44
+ end
45
+
46
+ it "provides access to the object being constructed within the blueprint" do
47
+ blueprint = Machinist::Blueprint.new(OpenStruct) do
48
+ title { "Test" }
49
+ body { object.title }
50
+ end
51
+ blueprint.make.body.should == "Test"
52
+ end
53
+
54
+ it "allows attribute names to be strings" do
55
+ blueprint = Machinist::Blueprint.new(OpenStruct) do
56
+ name { "Fred" }
57
+ end
58
+ blueprint.make("name" => "Bill").name.should == "Bill"
59
+ end
60
+
61
+ # These are normally a problem because of name clashes with the standard (but
62
+ # deprecated) Ruby methods. This test makes sure we work around this.
63
+ it "works with type and id attributes" do
64
+ klass = Class.new do
65
+ attr_accessor :id, :type
66
+ end
67
+ blueprint = Machinist::Blueprint.new(klass) do
68
+ id { "custom id" }
69
+ type { "custom type" }
70
+ end
71
+ object = blueprint.make
72
+ object.id.should == "custom id"
73
+ object.type.should == "custom type"
74
+ end
75
+
76
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Machinist, "exceptions" do
4
+
5
+ describe Machinist::BlueprintCantSaveError do
6
+ it "presents the right message" do
7
+ blueprint = Machinist::Blueprint.new(String) { }
8
+ exception = Machinist::BlueprintCantSaveError.new(blueprint)
9
+ exception.message.should == "make! is not supported by blueprints for class String"
10
+ end
11
+ end
12
+
13
+ describe Machinist::NoBlueprintError do
14
+ it "presents the right message" do
15
+ exception = Machinist::NoBlueprintError.new(String, :master)
16
+ exception.message.should == "No master blueprint defined for class String"
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,104 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'ostruct'
3
+
4
+ module InheritanceSpecs
5
+ class Grandpa
6
+ extend Machinist::Machinable
7
+ attr_accessor :name, :age
8
+ end
9
+
10
+ class Dad < Grandpa
11
+ extend Machinist::Machinable
12
+ attr_accessor :name, :age
13
+ end
14
+
15
+ class Son < Dad
16
+ extend Machinist::Machinable
17
+ attr_accessor :name, :age
18
+ end
19
+ end
20
+
21
+ describe Machinist::Blueprint do
22
+
23
+ describe "explicit inheritance" do
24
+ it "inherits attributes from the parent blueprint" do
25
+ parent_blueprint = Machinist::Blueprint.new(OpenStruct) do
26
+ name { "Fred" }
27
+ age { 97 }
28
+ end
29
+
30
+ child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do
31
+ name { "Bill" }
32
+ end
33
+
34
+ child = child_blueprint.make
35
+ child.name.should == "Bill"
36
+ child.age.should == 97
37
+ end
38
+
39
+ it "takes the serial number from the parent" do
40
+ parent_blueprint = Machinist::Blueprint.new(OpenStruct) do
41
+ parent_serial { sn }
42
+ end
43
+
44
+ child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do
45
+ child_serial { sn }
46
+ end
47
+
48
+ parent_blueprint.make.parent_serial.should == "0001"
49
+ child_blueprint.make.child_serial.should == "0002"
50
+ parent_blueprint.make.parent_serial.should == "0003"
51
+ end
52
+ end
53
+
54
+ describe "class inheritance" do
55
+ before(:each) do
56
+ [InheritanceSpecs::Grandpa, InheritanceSpecs::Dad, InheritanceSpecs::Son].each(&:clear_blueprints!)
57
+ end
58
+
59
+ it "inherits blueprinted attributes from the parent class" do
60
+ InheritanceSpecs::Dad.blueprint do
61
+ name { "Fred" }
62
+ end
63
+ InheritanceSpecs::Son.blueprint { }
64
+ InheritanceSpecs::Son.make.name.should == "Fred"
65
+ end
66
+
67
+ it "overrides blueprinted attributes in the child class" do
68
+ InheritanceSpecs::Dad.blueprint do
69
+ name { "Fred" }
70
+ end
71
+ InheritanceSpecs::Son.blueprint do
72
+ name { "George" }
73
+ end
74
+ InheritanceSpecs::Dad.make.name.should == "Fred"
75
+ InheritanceSpecs::Son.make.name.should == "George"
76
+ end
77
+
78
+ it "inherits from blueprinted attributes in ancestor class" do
79
+ InheritanceSpecs::Grandpa.blueprint do
80
+ name { "Fred" }
81
+ end
82
+ InheritanceSpecs::Son.blueprint { }
83
+ InheritanceSpecs::Grandpa.make.name.should == "Fred"
84
+ lambda { InheritanceSpecs::Dad.make }.should raise_error(RuntimeError)
85
+ InheritanceSpecs::Son.make.name.should == "Fred"
86
+ end
87
+
88
+ it "follows inheritance for named blueprints correctly" do
89
+ InheritanceSpecs::Dad.blueprint do
90
+ name { "John" }
91
+ age { 56 }
92
+ end
93
+ InheritanceSpecs::Dad.blueprint(:special) do
94
+ name { "Paul" }
95
+ end
96
+ InheritanceSpecs::Son.blueprint(:special) do
97
+ age { 37 }
98
+ end
99
+ InheritanceSpecs::Son.make(:special).name.should == "John"
100
+ InheritanceSpecs::Son.make(:special).age.should == 37
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ module MachinableSpecs
4
+ class Post
5
+ extend Machinist::Machinable
6
+ attr_accessor :title, :body, :comments
7
+ end
8
+
9
+ class Comment
10
+ extend Machinist::Machinable
11
+ attr_accessor :post, :title
12
+ end
13
+ end
14
+
15
+ describe Machinist::Machinable do
16
+
17
+ before(:each) do
18
+ MachinableSpecs::Post.clear_blueprints!
19
+ end
20
+
21
+ it "makes an object" do
22
+ MachinableSpecs::Post.blueprint do
23
+ title { "First Post" }
24
+ end
25
+
26
+ post = MachinableSpecs::Post.make
27
+ post.should be_a(MachinableSpecs::Post)
28
+ post.title.should == "First Post"
29
+ end
30
+
31
+ it "makes an object from a named blueprint" do
32
+ MachinableSpecs::Post.blueprint do
33
+ title { "First Post" }
34
+ body { "Woot!" }
35
+ end
36
+
37
+ MachinableSpecs::Post.blueprint(:extra) do
38
+ title { "Extra!" }
39
+ end
40
+
41
+ post = MachinableSpecs::Post.make(:extra)
42
+ post.should be_a(MachinableSpecs::Post)
43
+ post.title.should == "Extra!"
44
+ post.body.should == "Woot!"
45
+ end
46
+
47
+ it "makes an array of objects" do
48
+ MachinableSpecs::Post.blueprint do
49
+ title { "First Post" }
50
+ end
51
+
52
+ posts = MachinableSpecs::Post.make(3)
53
+ posts.should be_an(Array)
54
+ posts.should have(3).elements
55
+ posts.each do |post|
56
+ post.should be_a(MachinableSpecs::Post)
57
+ post.title.should == "First Post"
58
+ end
59
+ end
60
+
61
+ it "makes array attributes from the blueprint" do
62
+ MachinableSpecs::Comment.blueprint { }
63
+ MachinableSpecs::Post.blueprint do
64
+ comments(3) { MachinableSpecs::Comment.make }
65
+ end
66
+
67
+ post = MachinableSpecs::Post.make
68
+ post.comments.should be_a(Array)
69
+ post.comments.should have(3).elements
70
+ post.comments.each do |comment|
71
+ comment.should be_a(MachinableSpecs::Comment)
72
+ end
73
+ end
74
+
75
+ it "fails without a blueprint" do
76
+ expect do
77
+ MachinableSpecs::Post.make
78
+ end.should raise_error(Machinist::NoBlueprintError) do |exception|
79
+ exception.klass.should == MachinableSpecs::Post
80
+ exception.name.should == :master
81
+ end
82
+
83
+ expect do
84
+ MachinableSpecs::Post.make(:some_name)
85
+ end.should raise_error(Machinist::NoBlueprintError) do |exception|
86
+ exception.klass.should == MachinableSpecs::Post
87
+ exception.name.should == :some_name
88
+ end
89
+ end
90
+
91
+ it "fails when calling make! on an unsavable object" do
92
+ MachinableSpecs::Post.blueprint { }
93
+
94
+ expect do
95
+ MachinableSpecs::Post.make!
96
+ end.should raise_error(Machinist::BlueprintCantSaveError) do |exception|
97
+ exception.blueprint.klass.should == MachinableSpecs::Post
98
+ end
99
+ end
100
+
101
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'rspec'
7
+ require 'machinist'
@@ -0,0 +1,65 @@
1
+ require 'active_record'
2
+ require 'machinist/active_record'
3
+
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => "mysql",
6
+ :database => "machinist",
7
+ :username => "root",
8
+ :password => ""
9
+ )
10
+
11
+ ActiveRecord::Schema.define(:version => 0) do
12
+ create_table :users, :force => true do |t|
13
+ t.column :username, :string
14
+ end
15
+
16
+ create_table :posts, :force => true do |t|
17
+ t.column :title, :string
18
+ t.column :author_id, :integer
19
+ t.column :body, :text
20
+ end
21
+
22
+ create_table :comments, :force => true do |t|
23
+ t.column :post_id, :integer
24
+ t.column :body, :text
25
+ end
26
+
27
+ create_table :tags, :force => true do |t|
28
+ t.column :name, :string
29
+ end
30
+
31
+ create_table :posts_tags, :id => false, :force => true do |t|
32
+ t.column :post_id, :integer
33
+ t.column :tag_id, :integer
34
+ end
35
+ end
36
+
37
+ class User < ActiveRecord::Base
38
+ validates_presence_of :username
39
+ validates_uniqueness_of :username
40
+ end
41
+
42
+ class Post < ActiveRecord::Base
43
+ has_many :comments
44
+ belongs_to :author, :class_name => "User"
45
+ has_and_belongs_to_many :tags
46
+ end
47
+
48
+ class Comment < ActiveRecord::Base
49
+ belongs_to :post
50
+ end
51
+
52
+ class Tag < ActiveRecord::Base
53
+ has_and_belongs_to_many :posts
54
+ end
55
+
56
+ module ActiveRecordEnvironment
57
+
58
+ def empty_database!
59
+ [User, Post, Comment].each do |klass|
60
+ klass.delete_all
61
+ klass.clear_blueprints!
62
+ end
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indirect-machinist
3
+ version: !ruby/object:Gem::Version
4
+ hash: -1848230037
5
+ prerelease: true
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 0
10
+ - beta3
11
+ version: 2.0.0.beta3
12
+ platform: ruby
13
+ authors:
14
+ - Pete Yandell
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-11-19 00:00:00 -10:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: activerecord
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: mysql
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: rcov
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id005
92
+ - !ruby/object:Gem::Dependency
93
+ name: rdoc
94
+ prerelease: false
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ type: :development
105
+ version_requirements: *id006
106
+ description:
107
+ email:
108
+ - pete@notahat.com
109
+ executables: []
110
+
111
+ extensions: []
112
+
113
+ extra_rdoc_files: []
114
+
115
+ files:
116
+ - .gitignore
117
+ - Gemfile
118
+ - Gemfile.lock
119
+ - MIT-LICENSE
120
+ - README.markdown
121
+ - Rakefile
122
+ - VERSION
123
+ - lib/generators/machinist/install/USAGE
124
+ - lib/generators/machinist/install/install_generator.rb
125
+ - lib/generators/machinist/install/templates/blueprints.rb
126
+ - lib/generators/machinist/install/templates/machinist.rb.erb
127
+ - lib/generators/machinist/model/model_generator.rb
128
+ - lib/machinist.rb
129
+ - lib/machinist/active_record.rb
130
+ - lib/machinist/active_record/blueprint.rb
131
+ - lib/machinist/active_record/lathe.rb
132
+ - lib/machinist/blueprint.rb
133
+ - lib/machinist/exceptions.rb
134
+ - lib/machinist/lathe.rb
135
+ - lib/machinist/machinable.rb
136
+ - lib/machinist/railtie.rb
137
+ - lib/machinist/version.rb
138
+ - machinist.gemspec
139
+ - spec/active_record_spec.rb
140
+ - spec/blueprint_spec.rb
141
+ - spec/exceptions_spec.rb
142
+ - spec/inheritance_spec.rb
143
+ - spec/machinable_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/support/active_record_environment.rb
146
+ has_rdoc: true
147
+ homepage: http://github.com/notahat/machinist
148
+ licenses: []
149
+
150
+ post_install_message:
151
+ rdoc_options: []
152
+
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ hash: 3
161
+ segments:
162
+ - 0
163
+ version: "0"
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ">"
168
+ - !ruby/object:Gem::Version
169
+ hash: 25
170
+ segments:
171
+ - 1
172
+ - 3
173
+ - 1
174
+ version: 1.3.1
175
+ requirements: []
176
+
177
+ rubyforge_project: machinist
178
+ rubygems_version: 1.3.7
179
+ signing_key:
180
+ specification_version: 3
181
+ summary: Fixtures aren't fun. Machinist is.
182
+ test_files:
183
+ - spec/active_record_spec.rb
184
+ - spec/blueprint_spec.rb
185
+ - spec/exceptions_spec.rb
186
+ - spec/inheritance_spec.rb
187
+ - spec/machinable_spec.rb
188
+ - spec/spec_helper.rb
189
+ - spec/support/active_record_environment.rb