manufactory 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +3 -0
- data/README.textile +287 -2
- data/lib/manufactory.rb +29 -90
- data/lib/manufactory/adapters/activerecord.rb +90 -0
- data/lib/manufactory/adapters/datamapper.rb +89 -0
- data/lib/manufactory/adapters/generic_model.rb +38 -0
- data/lib/manufactory/adapters/object.rb +2 -29
- data/lib/manufactory/adapters/sequel.rb +62 -0
- data/lib/manufactory/dsl.rb +89 -0
- data/spec/manufactory/adapters/activerecord_spec.rb +196 -0
- data/spec/manufactory/adapters/datamapper_spec.rb +134 -0
- data/spec/manufactory/adapters/generic_model_spec.rb +0 -0
- data/spec/manufactory/adapters/log/test.log +437 -0
- data/spec/manufactory/adapters/object_spec.rb +11 -0
- data/spec/manufactory/adapters/sequel_spec.rb +146 -0
- data/spec/manufactory/dsl_spec.rb +0 -0
- data/spec/manufactory_spec.rb +97 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/stubs/db/schema.rb +20 -0
- metadata +14 -2
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "spec_helper"
|
4
|
+
require "manufactory/adapters/object"
|
5
|
+
|
6
|
+
describe Manufactory do
|
7
|
+
Post = Struct.new(:title, :body, :published)
|
8
|
+
it "should play nice with just attributes, so it calls initialize without any arguments" do
|
9
|
+
Post.should_receive(:new).with(no_args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'manufactory/sequel'
|
3
|
+
|
4
|
+
# We have to define this here because Sequel needs a DB connection
|
5
|
+
# setup before you can define models
|
6
|
+
DB = Sequel.sqlite(:logger => Logger.new(File.dirname(__FILE__) + "/log/test.log"))
|
7
|
+
|
8
|
+
DB.create_table :people do
|
9
|
+
primary_key :id
|
10
|
+
String :name
|
11
|
+
String :type
|
12
|
+
String :password
|
13
|
+
Boolean :admin, :default => false
|
14
|
+
end
|
15
|
+
|
16
|
+
DB.create_table :posts do
|
17
|
+
primary_key :id
|
18
|
+
String :title
|
19
|
+
String :body
|
20
|
+
Boolean :published, :default => true
|
21
|
+
end
|
22
|
+
|
23
|
+
DB.create_table :comments do
|
24
|
+
primary_key :id
|
25
|
+
Integer :post_id
|
26
|
+
Integer :author_id
|
27
|
+
String :body
|
28
|
+
end
|
29
|
+
|
30
|
+
module ManufactorySequelSpecs
|
31
|
+
|
32
|
+
class Person < Sequel::Model
|
33
|
+
set_restricted_columns :password
|
34
|
+
end
|
35
|
+
|
36
|
+
class Post < Sequel::Model
|
37
|
+
one_to_many :comments, :class => "ManufactorySequelSpecs::Comment"
|
38
|
+
end
|
39
|
+
|
40
|
+
class Comment < Sequel::Model
|
41
|
+
many_to_one :post, :class => "ManufactorySequelSpecs::Post"
|
42
|
+
many_to_one :author, :class => "ManufactorySequelSpecs::Person"
|
43
|
+
end
|
44
|
+
|
45
|
+
describe Manufactory, "Sequel adapter" do
|
46
|
+
|
47
|
+
before(:each) do
|
48
|
+
Person.blueprints.clear
|
49
|
+
Post.blueprints.clear
|
50
|
+
Comment.blueprints.clear
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "make method" do
|
54
|
+
it "should save the constructed object" do
|
55
|
+
Person.blueprint { }
|
56
|
+
person = Person.make
|
57
|
+
person.should_not be_new
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should create and object through a many_to_one association" do
|
61
|
+
Post.blueprint { }
|
62
|
+
Comment.blueprint { post }
|
63
|
+
Comment.make.post.class.should == Post
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should create an object through many_to_one association with a class_name attribute" do
|
67
|
+
Person.blueprint { }
|
68
|
+
Comment.blueprint { author }
|
69
|
+
Comment.make.author.class.should == Person
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should allow setting a protected attribute in the blueprint" do
|
73
|
+
Person.blueprint do
|
74
|
+
password "Test"
|
75
|
+
end
|
76
|
+
Person.make.password.should == "Test"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should allow overriding a protected attribute" do
|
80
|
+
Person.blueprint do
|
81
|
+
password "Test"
|
82
|
+
end
|
83
|
+
Person.make(:password => "New").password.should == "New"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should allow setting the id attribute in a blueprint" do
|
87
|
+
Person.blueprint { id 12345 }
|
88
|
+
Person.make.id.should == 12345
|
89
|
+
end
|
90
|
+
|
91
|
+
# it "should allow setting the type attribute in a blueprint" do
|
92
|
+
# Person.blueprint { type "Person" }
|
93
|
+
# Person.make.type.should == "Person"
|
94
|
+
# end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "plan method" do
|
98
|
+
it "should not save the constructed object" do
|
99
|
+
lambda {
|
100
|
+
Person.blueprint { }
|
101
|
+
person = Person.plan
|
102
|
+
}.should_not change(Person,:count)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should return a regular attribute in the hash" do
|
106
|
+
Post.blueprint { title "Test" }
|
107
|
+
post = Post.plan
|
108
|
+
post[:title].should == "Test"
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should create an object through a many_to_one association, and return its id" do
|
112
|
+
Post.blueprint { }
|
113
|
+
Comment.blueprint { post }
|
114
|
+
lambda {
|
115
|
+
comment = Comment.plan
|
116
|
+
comment[:post].should be_nil
|
117
|
+
comment[:post_id].should_not be_nil
|
118
|
+
}.should change(Post, :count).by(1)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Note that building up an unsaved object graph using just the
|
123
|
+
# association methods is not possible in Sequel, so
|
124
|
+
# make_unsaved will break in amusing ways unless you manually
|
125
|
+
# override the setters.
|
126
|
+
#
|
127
|
+
# From sequel-talk "Sequel does not have such an API and will not be adding one"
|
128
|
+
# Feb 17
|
129
|
+
describe "make_unsaved method" do
|
130
|
+
it "should not save the constructed object" do
|
131
|
+
Person.blueprint { }
|
132
|
+
person = Person.make_unsaved
|
133
|
+
person.should be_new
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should save objects made within a passed-in block" do
|
137
|
+
Post.blueprint { }
|
138
|
+
Comment.blueprint { }
|
139
|
+
comment = nil
|
140
|
+
post = Post.make_unsaved { comment = Comment.make }
|
141
|
+
post.should be_new
|
142
|
+
comment.should_not be_new
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
File without changes
|
data/spec/manufactory_spec.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative "spec_helper"
|
4
|
-
require "manufactory/adapters/
|
4
|
+
require "manufactory/adapters/generic_model"
|
5
5
|
|
6
|
-
|
6
|
+
HashStruct.extend(Manufactory::GenericModelMixin)
|
7
|
+
Post = HashStruct.generate(:title, :body, :published)
|
7
8
|
|
8
9
|
describe Manufactory do
|
9
10
|
before(:each) do
|
@@ -40,8 +41,102 @@ describe Manufactory do
|
|
40
41
|
Post.make(:first).published.should be_true
|
41
42
|
end
|
42
43
|
end
|
44
|
+
|
45
|
+
describe "monkey patching" do
|
46
|
+
it "should " do
|
47
|
+
Post.blueprint { published true }
|
48
|
+
Post.blueprint { published false }
|
49
|
+
Post.make.published.should be_false
|
50
|
+
end
|
51
|
+
end
|
43
52
|
|
44
53
|
it "should raise for make on a class with no blueprint" do
|
45
54
|
lambda { Post.make }.should raise_error(RuntimeError)
|
46
55
|
end
|
56
|
+
|
57
|
+
# it "should default to calling Sham for an attribute in the blueprint" do
|
58
|
+
# Sham.clear
|
59
|
+
# Sham.name { "Fred" }
|
60
|
+
# Person.blueprint { name }
|
61
|
+
# Person.make.name.should == "Fred"
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# it "should override an attribute from the blueprint with a passed-in attribute" do
|
65
|
+
# Person.blueprint do
|
66
|
+
# name "Fred"
|
67
|
+
# end
|
68
|
+
# Person.make(:name => "Bill").name.should == "Bill"
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# it "should allow overridden attribute names to be strings" do
|
72
|
+
# Person.blueprint do
|
73
|
+
# name "Fred"
|
74
|
+
# end
|
75
|
+
# Person.make("name" => "Bill").name.should == "Bill"
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# it "should not call a block in the blueprint if that attribute is passed in" do
|
79
|
+
# block_called = false
|
80
|
+
# Person.blueprint do
|
81
|
+
# name { block_called = true; "Fred" }
|
82
|
+
# end
|
83
|
+
# Person.make(:name => "Bill").name.should == "Bill"
|
84
|
+
# block_called.should be_false
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# it "should call a passed-in block with the object being constructed" do
|
88
|
+
# Person.blueprint { }
|
89
|
+
# block_called = false
|
90
|
+
# Person.make do |person|
|
91
|
+
# block_called = true
|
92
|
+
# person.class.should == Person
|
93
|
+
# end
|
94
|
+
# block_called.should be_true
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# it "should provide access to the object being constructed from within the blueprint" do
|
98
|
+
# person = nil
|
99
|
+
# Person.blueprint { person = object }
|
100
|
+
# Person.make
|
101
|
+
# person.class.should == Person
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# it "should allow reading of a previously assigned attribute from within the blueprint" do
|
105
|
+
# Post.blueprint do
|
106
|
+
# title "Test"
|
107
|
+
# body { title }
|
108
|
+
# end
|
109
|
+
# Post.make.body.should == "Test"
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# describe "blueprint inheritance" do
|
113
|
+
# it "should inherit blueprinted attributes from the parent class" do
|
114
|
+
# Dad.blueprint { name "Fred" }
|
115
|
+
# Son.blueprint { }
|
116
|
+
# Son.make.name.should == "Fred"
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# it "should override blueprinted attributes in the child class" do
|
120
|
+
# Dad.blueprint { name "Fred" }
|
121
|
+
# Son.blueprint { name "George" }
|
122
|
+
# Dad.make.name.should == "Fred"
|
123
|
+
# Son.make.name.should == "George"
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# it "should inherit from blueprinted attributes in ancestor class" do
|
127
|
+
# Grandpa.blueprint { name "Fred" }
|
128
|
+
# Son.blueprint { }
|
129
|
+
# Grandpa.make.name.should == "Fred"
|
130
|
+
# lambda { Dad.make }.should raise_error(RuntimeError)
|
131
|
+
# Son.make.name.should == "Fred"
|
132
|
+
# end
|
133
|
+
#
|
134
|
+
# it "should follow inheritance for named blueprints correctly" do
|
135
|
+
# Dad.blueprint { name "John" }
|
136
|
+
# Dad.blueprint(:special) { name "Paul" }
|
137
|
+
# Son.blueprint { }
|
138
|
+
# Son.blueprint(:special) { }
|
139
|
+
# Son.make(:special).name.should == "John"
|
140
|
+
# end
|
141
|
+
# end
|
47
142
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :people, :force => true do |t|
|
3
|
+
t.column :name, :string
|
4
|
+
t.column :type, :string
|
5
|
+
t.column :password, :string
|
6
|
+
t.column :admin, :boolean, :default => false
|
7
|
+
end
|
8
|
+
|
9
|
+
create_table :posts, :force => true do |t|
|
10
|
+
t.column :title, :string
|
11
|
+
t.column :body, :text
|
12
|
+
t.column :published, :boolean, :default => true
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :comments, :force => true do |t|
|
16
|
+
t.column :post_id, :integer
|
17
|
+
t.column :author_id, :integer
|
18
|
+
t.column :body, :text
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: manufactory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
|
@@ -30,13 +30,25 @@ extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
31
31
|
|
32
32
|
files:
|
33
|
+
- lib/manufactory/adapters/activerecord.rb
|
33
34
|
- lib/manufactory/adapters/datamapper.rb
|
35
|
+
- lib/manufactory/adapters/generic_model.rb
|
34
36
|
- lib/manufactory/adapters/object.rb
|
37
|
+
- lib/manufactory/adapters/sequel.rb
|
38
|
+
- lib/manufactory/dsl.rb
|
35
39
|
- lib/manufactory/sham.rb
|
36
40
|
- lib/manufactory.rb
|
41
|
+
- spec/manufactory/adapters/activerecord_spec.rb
|
42
|
+
- spec/manufactory/adapters/datamapper_spec.rb
|
43
|
+
- spec/manufactory/adapters/generic_model_spec.rb
|
44
|
+
- spec/manufactory/adapters/log/test.log
|
45
|
+
- spec/manufactory/adapters/object_spec.rb
|
46
|
+
- spec/manufactory/adapters/sequel_spec.rb
|
47
|
+
- spec/manufactory/dsl_spec.rb
|
37
48
|
- spec/manufactory/sham_spec.rb
|
38
49
|
- spec/manufactory_spec.rb
|
39
50
|
- spec/spec_helper.rb
|
51
|
+
- spec/stubs/db/schema.rb
|
40
52
|
- CHANGELOG
|
41
53
|
- LICENSE
|
42
54
|
- Rakefile
|
@@ -68,6 +80,6 @@ rubyforge_project: manufactory
|
|
68
80
|
rubygems_version: 1.3.5
|
69
81
|
signing_key:
|
70
82
|
specification_version: 3
|
71
|
-
summary: Manufactory is a minimalistic and clean rewrite of
|
83
|
+
summary: Manufactory is a minimalistic and clean rewrite of manufactory with some improvements.
|
72
84
|
test_files: []
|
73
85
|
|