machinist_mongo 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
@@ -9,6 +9,17 @@ rescue LoadError
9
9
  end
10
10
 
11
11
  module Machinist
12
+
13
+ class Lathe
14
+ def assign_attribute(key, value)
15
+ assigned_attributes[key.to_sym] = value
16
+ if @object.respond_to? "#{key}="
17
+ @object.send("#{key}=", value)
18
+ else
19
+ @object[key] = value
20
+ end
21
+ end
22
+ end
12
23
 
13
24
  class MongoMapperAdapter
14
25
  def self.has_association?(object, attribute)
@@ -69,4 +80,4 @@ end
69
80
  MongoMapper::Document.append_extensions(Machinist::Blueprints::ClassMethods)
70
81
  MongoMapper::Document.append_extensions(Machinist::MongoMapperExtensions::Document)
71
82
  MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
72
- MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument)
83
+ MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument)
@@ -0,0 +1,71 @@
1
+ require "machinist"
2
+ require "machinist/blueprints"
3
+
4
+ begin
5
+ require "mongoid"
6
+ rescue LoadError
7
+ puts "Mongoid is not installed (gem install mongoid)"
8
+ exit
9
+ end
10
+
11
+ module Machinist
12
+ class Lathe
13
+ def assign_attribute(key, value)
14
+ assigned_attributes[key.to_sym] = value
15
+ @object.process(key => value)
16
+ end
17
+ end
18
+
19
+ class MongoidAdapter
20
+ class << self
21
+ def has_association?(object, attribute)
22
+ object.class.associations[attribute.to_s]
23
+ end
24
+
25
+ def class_for_association(object, attribute)
26
+ association = object.class.associations[attribute.to_s]
27
+ association && association.klass
28
+ end
29
+
30
+ def assigned_attributes_without_associations(lathe)
31
+ attributes = {}
32
+ lathe.assigned_attributes.each_pair do |attribute, value|
33
+ association = lathe.object.class.associations[attribute.to_s]
34
+ if association && (association.macro == :belongs_to_related) && !value.nil?
35
+ attributes[association.foreign_key.to_sym] = value.id
36
+ else
37
+ attributes[attribute] = value
38
+ end
39
+ end
40
+ attributes
41
+ end
42
+ end
43
+ end
44
+
45
+ module MongoidExtensions
46
+ module Document
47
+ def make(*args, &block)
48
+ lathe = Lathe.run(Machinist::MongoidAdapter, self.new, *args)
49
+ unless Machinist.nerfed? || embedded
50
+ lathe.object.save!
51
+ lathe.object.reload
52
+ end
53
+ lathe.object(&block)
54
+ end
55
+
56
+ def make_unsaved(*args)
57
+ returning(Machinist.with_save_nerfed { make(*args) }) do |object|
58
+ yield object if block_given?
59
+ end
60
+ end
61
+
62
+ def plan(*args)
63
+ lathe = Lathe.run(Machinist::MongoidAdapter, self.new, *args)
64
+ Machinist::MongoidAdapter.assigned_attributes_without_associations(lathe)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ Mongoid::Document::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
71
+ Mongoid::Document::ClassMethods.send(:include, Machinist::MongoidExtensions::Document)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{machinist_mongo}
8
- s.version = "1.0.2"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nicolas M\303\251rouze", "Cyril Mougel"]
12
- s.date = %q{2010-01-11}
12
+ s.date = %q{2010-03-31}
13
13
  s.email = %q{nicolas.merouze@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -22,17 +22,20 @@ Gem::Specification.new do |s|
22
22
  "Rakefile",
23
23
  "VERSION",
24
24
  "lib/machinist/mongo_mapper.rb",
25
+ "lib/machinist/mongoid.rb",
25
26
  "machinist_mongo.gemspec",
26
27
  "spec/mongo_mapper_spec.rb",
28
+ "spec/mongoid_spec.rb",
27
29
  "spec/spec_helper.rb"
28
30
  ]
29
31
  s.homepage = %q{http://github.com/nmerouze/machinist_mongo}
30
32
  s.rdoc_options = ["--charset=UTF-8"]
31
33
  s.require_paths = ["lib"]
32
- s.rubygems_version = %q{1.3.5}
34
+ s.rubygems_version = %q{1.3.6}
33
35
  s.summary = %q{Machinist adapters for MongoDB ORMs}
34
36
  s.test_files = [
35
37
  "spec/mongo_mapper_spec.rb",
38
+ "spec/mongoid_spec.rb",
36
39
  "spec/spec_helper.rb"
37
40
  ]
38
41
 
@@ -102,6 +102,20 @@ describe Machinist, "MongoMapper::Document adapter" do
102
102
  comment[:post].should be_nil
103
103
  comment[:post_id].should_not be_nil
104
104
  end
105
+
106
+ context "attribute assignment" do
107
+ it "should allow assigning a value to an attribute" do
108
+ Post.blueprint { title "1234" }
109
+ post = Post.make
110
+ post.title.should == "1234"
111
+ end
112
+
113
+ it "should allow arbitrary attributes on the base model in its blueprint" do
114
+ Post.blueprint { foo "bar" }
115
+ post = Post.make
116
+ post.foo.should == "bar"
117
+ end
118
+ end
105
119
  end
106
120
 
107
121
  describe "make_unsaved method" do
@@ -152,6 +166,12 @@ describe Machinist, "MongoMapper::EmbeddedDocument adapter" do
152
166
  end
153
167
  Person.make.address.should be_instance_of(Address)
154
168
  end
169
+
170
+ it "should allow arbitrary attributes on the base model in its blueprint" do
171
+ Address.blueprint { foo "bar" }
172
+ addr = Address.make
173
+ addr.foo.should == "bar"
174
+ end
155
175
  end
156
176
 
157
- end
177
+ end
@@ -0,0 +1,170 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+ require "machinist/mongoid"
3
+
4
+ Spec::Mongoid.configure!
5
+
6
+ class Address
7
+ include Mongoid::Document
8
+
9
+ field :street
10
+ field :zip
11
+ field :country
12
+ belongs_to :person, :inverse_of => :address
13
+ end
14
+
15
+ class Person
16
+ include Mongoid::Document
17
+
18
+ field :name
19
+ field :type
20
+ field :password
21
+ field :admin, :type => Boolean, :default => false
22
+
23
+ has_one :address
24
+ end
25
+
26
+ class Post
27
+ include Mongoid::Document
28
+
29
+ field :title
30
+ field :body
31
+ field :published, :type => Boolean, :default => true
32
+
33
+ has_many_related :comments
34
+ end
35
+
36
+ class Comment
37
+ include Mongoid::Document
38
+
39
+ field :body
40
+ field :post_id
41
+ field :author_id
42
+
43
+ belongs_to_related :post
44
+ belongs_to_related :author, :class_name => "Person"
45
+ end
46
+
47
+ describe Machinist, "Mongoid::Document adapter" do
48
+
49
+ before(:each) do
50
+ Person.clear_blueprints!
51
+ Post.clear_blueprints!
52
+ Comment.clear_blueprints!
53
+ end
54
+
55
+ describe "make method" do
56
+ it "should save the constructed object" do
57
+ Person.blueprint { }
58
+ person = Person.make
59
+ person.should_not be_new_record
60
+ end
61
+
62
+ it "should create an object through belongs_to association" do
63
+ Post.blueprint { }
64
+ Comment.blueprint { post }
65
+ Comment.make.post.class.should == Post
66
+ end
67
+
68
+ it "should create an object through belongs_to association with a class_name attribute" do
69
+ Person.blueprint { }
70
+ Comment.blueprint { author }
71
+ Comment.make.author.class.should == Person
72
+ end
73
+
74
+ it "should create an object through belongs_to association using a named blueprint" do
75
+ Post.blueprint { }
76
+ Post.blueprint(:dummy) do
77
+ title { 'Dummy Post' }
78
+ end
79
+ Comment.blueprint { post(:dummy) }
80
+ Comment.make.post.title.should == 'Dummy Post'
81
+ end
82
+ end
83
+
84
+ describe "plan method" do
85
+ it "should not save the constructed object" do
86
+ person_count = Person.count
87
+ Person.blueprint { }
88
+ person = Person.plan
89
+ Person.count.should == person_count
90
+ end
91
+
92
+ it "should return a regular attribute in the hash" do
93
+ Post.blueprint { title "Test" }
94
+ post = Post.plan
95
+ post[:title].should == "Test"
96
+ end
97
+
98
+ it "should create an object through a belongs_to association, and return its id" do
99
+ Post.blueprint { }
100
+ Comment.blueprint { post }
101
+ post_count = Post.count
102
+ comment = Comment.plan
103
+ Post.count.should == post_count + 1
104
+ comment[:post].should be_nil
105
+ comment[:post_id].should_not be_nil
106
+ end
107
+
108
+ context "attribute assignment" do
109
+ it "should allow assigning a value to an attribute" do
110
+ Post.blueprint { title "1234" }
111
+ post = Post.make
112
+ post.title.should == "1234"
113
+ end
114
+
115
+ it "should allow arbitrary attributes on the base model in its blueprint" do
116
+ Post.blueprint { foo "bar" }
117
+ post = Post.make
118
+ post.foo.should == "bar"
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "make_unsaved method" do
124
+ it "should not save the constructed object" do
125
+ Person.blueprint { }
126
+ person = Person.make_unsaved
127
+ person.should be_new_record
128
+ end
129
+
130
+ it "should not save associated objects" do
131
+ pending
132
+ # Post.blueprint { }
133
+ # Comment.blueprint { post }
134
+ # comment = Comment.make_unsaved
135
+ # comment.post.should be_new_record
136
+ end
137
+
138
+ it "should save objects made within a passed-in block" do
139
+ Post.blueprint { }
140
+ Comment.blueprint { }
141
+ comment = nil
142
+ post = Post.make_unsaved { comment = Comment.make }
143
+ post.should be_new_record
144
+ comment.should_not be_new_record
145
+ end
146
+ end
147
+
148
+ describe "make method with embedded documents" do
149
+ it "should construct object" do
150
+ Address.blueprint { }
151
+ address = Address.make
152
+ address.should be_instance_of(Address)
153
+ end
154
+
155
+ it "should make an embed object" do
156
+ Address.blueprint { }
157
+ Person.blueprint do
158
+ address { Address.make }
159
+ end
160
+ Person.make.address.should be_instance_of(Address)
161
+ end
162
+
163
+ it "should allow arbitrary attributes on the base model in its blueprint" do
164
+ Address.blueprint { foo "bar" }
165
+ addr = Address.make
166
+ addr.foo.should == "bar"
167
+ end
168
+ end
169
+
170
+ end
data/spec/spec_helper.rb CHANGED
@@ -14,4 +14,18 @@ module Spec
14
14
  end
15
15
  end
16
16
  end
17
+
18
+ module Mongoid
19
+ def self.configure!
20
+ ::Mongoid.configure do |config|
21
+ config.master = Mongo::Connection.new.db("machinist_mongoid")
22
+ config.allow_dynamic_fields = true
23
+ end
24
+
25
+ Spec::Runner.configure do |config|
26
+ config.before(:each) { Sham.reset }
27
+ config.after(:all) { ::Mongoid.master.collections.each { |c| c.remove } }
28
+ end
29
+ end
30
+ end
17
31
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: machinist_mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 0
9
+ version: 1.1.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - "Nicolas M\xC3\xA9rouze"
@@ -10,19 +15,23 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2010-01-11 00:00:00 +01:00
18
+ date: 2010-03-31 00:00:00 +02:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
17
22
  name: machinist
18
- type: :runtime
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
21
25
  requirements:
22
26
  - - ~>
23
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 4
24
32
  version: 1.0.4
25
- version:
33
+ type: :runtime
34
+ version_requirements: *id001
26
35
  description:
27
36
  email: nicolas.merouze@gmail.com
28
37
  executables: []
@@ -39,8 +48,10 @@ files:
39
48
  - Rakefile
40
49
  - VERSION
41
50
  - lib/machinist/mongo_mapper.rb
51
+ - lib/machinist/mongoid.rb
42
52
  - machinist_mongo.gemspec
43
53
  - spec/mongo_mapper_spec.rb
54
+ - spec/mongoid_spec.rb
44
55
  - spec/spec_helper.rb
45
56
  has_rdoc: true
46
57
  homepage: http://github.com/nmerouze/machinist_mongo
@@ -55,21 +66,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
66
  requirements:
56
67
  - - ">="
57
68
  - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
58
71
  version: "0"
59
- version:
60
72
  required_rubygems_version: !ruby/object:Gem::Requirement
61
73
  requirements:
62
74
  - - ">="
63
75
  - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
64
78
  version: "0"
65
- version:
66
79
  requirements: []
67
80
 
68
81
  rubyforge_project:
69
- rubygems_version: 1.3.5
82
+ rubygems_version: 1.3.6
70
83
  signing_key:
71
84
  specification_version: 3
72
85
  summary: Machinist adapters for MongoDB ORMs
73
86
  test_files:
74
87
  - spec/mongo_mapper_spec.rb
88
+ - spec/mongoid_spec.rb
75
89
  - spec/spec_helper.rb