tchandy-machinist_mongomapper 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.md +5 -0
- data/Rakefile +29 -0
- data/VERSION +1 -0
- data/init.rb +4 -0
- data/lib/machinist/mongomapper.rb +71 -0
- data/machinist_mongomapper.gemspec +55 -0
- data/spec/mongomapper_spec.rb +146 -0
- data/spec/spec_helper.rb +19 -0
- metadata +88 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Yeasty Mobs
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.version = "0.9.7"
|
9
|
+
gem.name = "tchandy-machinist_mongomapper"
|
10
|
+
gem.summary = %Q{Machinist adapter for MongoMapper}
|
11
|
+
gem.email = "tchandy@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/tchandy/machinist_mongomapper"
|
13
|
+
gem.authors = ["Nicolas Mérouze", "Vincent Hellot", "Mathieu Fosse", "Thiago Pradi"]
|
14
|
+
|
15
|
+
gem.add_dependency('notahat-machinist', '>= 1.0.3')
|
16
|
+
gem.add_dependency('mongomapper', '>= 0.6.0')
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Default: run specs.'
|
23
|
+
task :default => :spec
|
24
|
+
|
25
|
+
desc 'Run all the specs for the machinist plugin.'
|
26
|
+
Spec::Rake::SpecTask.new do |t|
|
27
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
t.rcov = false
|
29
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.9.3
|
data/init.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'machinist'
|
2
|
+
require 'machinist/blueprints'
|
3
|
+
|
4
|
+
module Machinist
|
5
|
+
|
6
|
+
class MongoMapperAdapter
|
7
|
+
def self.has_association?(object, attribute)
|
8
|
+
object.class.associations[attribute]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.class_for_association(object, attribute)
|
12
|
+
association = object.class.associations[attribute]
|
13
|
+
association && association.klass
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.assigned_attributes_without_associations(lathe)
|
17
|
+
attributes = {}
|
18
|
+
lathe.assigned_attributes.each_pair do |attribute, value|
|
19
|
+
association = lathe.object.class.associations[attribute]
|
20
|
+
if association && association.belongs_to?
|
21
|
+
attributes[association.foreign_key.to_sym] = value.id
|
22
|
+
else
|
23
|
+
attributes[attribute] = value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module MongoMapperExtensions
|
31
|
+
module Document
|
32
|
+
def make(*args, &block)
|
33
|
+
lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
|
34
|
+
lathe.object.save! unless Machinist.nerfed?
|
35
|
+
lathe.object(&block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def make_unsaved(*args)
|
39
|
+
returning(Machinist.with_save_nerfed { make(*args) }) do |object|
|
40
|
+
yield object if block_given?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def plan(*args)
|
45
|
+
lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
|
46
|
+
Machinist::MongoMapperAdapter.assigned_attributes_without_associations(lathe)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module EmbeddedDocument
|
51
|
+
def make(*args, &block)
|
52
|
+
lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
|
53
|
+
lathe.object(&block)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module MongoMapperAssociationsProxyExtensions
|
59
|
+
def nil?
|
60
|
+
self.klass.nil?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
MongoMapper::Associations::Proxy.send(:include, Machinist::MongoMapperAssociationsProxyExtensions)
|
68
|
+
MongoMapper::Document::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
|
69
|
+
MongoMapper::Document::ClassMethods.send(:include, Machinist::MongoMapperExtensions::Document)
|
70
|
+
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
|
71
|
+
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{tchandy-machinist_mongomapper}
|
8
|
+
s.version = "0.9.7"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nicolas M\303\251rouze", "Vincent Hellot", "Mathieu Fosse", "Thiago Pradi"]
|
12
|
+
s.date = %q{2009-09-24}
|
13
|
+
s.email = %q{tchandy@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"LICENSE",
|
21
|
+
"README.md",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"init.rb",
|
25
|
+
"lib/machinist/mongomapper.rb",
|
26
|
+
"machinist_mongomapper.gemspec",
|
27
|
+
"spec/mongomapper_spec.rb",
|
28
|
+
"spec/spec_helper.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/tchandy/machinist_mongomapper}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.5}
|
34
|
+
s.summary = %q{Machinist adapter for MongoMapper}
|
35
|
+
s.test_files = [
|
36
|
+
"spec/mongomapper_spec.rb",
|
37
|
+
"spec/spec_helper.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 7
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<notahat-machinist>, [">= 1.0.3"])
|
46
|
+
s.add_runtime_dependency(%q<mongomapper>, [">= 0.6.0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<notahat-machinist>, [">= 1.0.3"])
|
49
|
+
s.add_dependency(%q<mongomapper>, [">= 0.6.0"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<notahat-machinist>, [">= 1.0.3"])
|
53
|
+
s.add_dependency(%q<mongomapper>, [">= 0.6.0"])
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'machinist/mongomapper'
|
3
|
+
|
4
|
+
class Address
|
5
|
+
include MongoMapper::EmbeddedDocument
|
6
|
+
|
7
|
+
key :street, String
|
8
|
+
key :zip, String
|
9
|
+
key :country, String
|
10
|
+
end
|
11
|
+
|
12
|
+
class Person
|
13
|
+
include MongoMapper::Document
|
14
|
+
|
15
|
+
key :name, String
|
16
|
+
key :type, String
|
17
|
+
key :password, String
|
18
|
+
key :admin, Boolean, :default => false
|
19
|
+
key :address, Address
|
20
|
+
end
|
21
|
+
|
22
|
+
class Post
|
23
|
+
include MongoMapper::Document
|
24
|
+
|
25
|
+
key :title, String
|
26
|
+
key :body, String
|
27
|
+
key :published, Boolean, :default => true
|
28
|
+
|
29
|
+
many :comments
|
30
|
+
end
|
31
|
+
|
32
|
+
class Comment
|
33
|
+
include MongoMapper::Document
|
34
|
+
|
35
|
+
key :body, String
|
36
|
+
key :post_id, String
|
37
|
+
key :author_id, String
|
38
|
+
|
39
|
+
belongs_to :post
|
40
|
+
belongs_to :author, :class_name => "Person"
|
41
|
+
end
|
42
|
+
|
43
|
+
describe Machinist, "MongoMapper::Document adapter" do
|
44
|
+
|
45
|
+
before(:each) do
|
46
|
+
Person.clear_blueprints!
|
47
|
+
Post.clear_blueprints!
|
48
|
+
Comment.clear_blueprints!
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "make method" do
|
52
|
+
it "should save the constructed object" do
|
53
|
+
Person.blueprint { }
|
54
|
+
person = Person.make
|
55
|
+
person.should_not be_new_record
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should create an object through belongs_to association" do
|
59
|
+
Post.blueprint { }
|
60
|
+
Comment.blueprint { post }
|
61
|
+
Comment.make.post.class.should == Post
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should create an object through belongs_to association with a class_name attribute" do
|
65
|
+
Person.blueprint { }
|
66
|
+
Comment.blueprint { author }
|
67
|
+
Comment.make.author.class.should == Person
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "plan method" do
|
72
|
+
it "should not save the constructed object" do
|
73
|
+
person_count = Person.count
|
74
|
+
Person.blueprint { }
|
75
|
+
person = Person.plan
|
76
|
+
Person.count.should == person_count
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return a regular attribute in the hash" do
|
80
|
+
Post.blueprint { title "Test" }
|
81
|
+
post = Post.plan
|
82
|
+
post[:title].should == "Test"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should create an object through a belongs_to association, and return its id" do
|
86
|
+
Post.blueprint { }
|
87
|
+
Comment.blueprint { post }
|
88
|
+
post_count = Post.count
|
89
|
+
comment = Comment.plan
|
90
|
+
Post.count.should == post_count + 1
|
91
|
+
comment[:post].should be_nil
|
92
|
+
comment[:post_id].should_not be_nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "make_unsaved method" do
|
97
|
+
it "should not save the constructed object" do
|
98
|
+
Person.blueprint { }
|
99
|
+
person = Person.make_unsaved
|
100
|
+
person.should be_new_record
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should not save associated objects" do
|
104
|
+
pending
|
105
|
+
# Post.blueprint { }
|
106
|
+
# Comment.blueprint { post }
|
107
|
+
# comment = Comment.make_unsaved
|
108
|
+
# comment.post.should be_new_record
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should save objects made within a passed-in block" do
|
112
|
+
Post.blueprint { }
|
113
|
+
Comment.blueprint { }
|
114
|
+
comment = nil
|
115
|
+
post = Post.make_unsaved { comment = Comment.make }
|
116
|
+
post.should be_new_record
|
117
|
+
comment.should_not be_new_record
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
describe Machinist, "MongoMapper::EmbeddedDocument adapter" do
|
124
|
+
|
125
|
+
before(:each) do
|
126
|
+
Person.clear_blueprints!
|
127
|
+
Address.clear_blueprints!
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "make method" do
|
131
|
+
it "should construct object" do
|
132
|
+
Address.blueprint { }
|
133
|
+
address = Address.make
|
134
|
+
address.should be_instance_of(Address)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should make an embed object" do
|
138
|
+
Address.blueprint { }
|
139
|
+
Person.blueprint do
|
140
|
+
address { Address.make }
|
141
|
+
end
|
142
|
+
Person.make.address.should be_instance_of(Address)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
2
|
+
require "rubygems"
|
3
|
+
require "spec"
|
4
|
+
require "sham"
|
5
|
+
|
6
|
+
begin
|
7
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../../mongomapper/lib"
|
8
|
+
rescue
|
9
|
+
gem "mongomapper"
|
10
|
+
end
|
11
|
+
|
12
|
+
require "mongo_mapper"
|
13
|
+
|
14
|
+
MongoMapper.database = "machinist_mongomapper"
|
15
|
+
|
16
|
+
Spec::Runner.configure do |config|
|
17
|
+
config.before(:each) { Sham.reset }
|
18
|
+
config.after(:all) { MongoMapper.database.collections.each { |c| c.clear } }
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tchandy-machinist_mongomapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Nicolas M\xC3\xA9rouze"
|
8
|
+
- Vincent Hellot
|
9
|
+
- Mathieu Fosse
|
10
|
+
- Thiago Pradi
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
|
15
|
+
date: 2009-11-18 00:00:00 -02:00
|
16
|
+
default_executable:
|
17
|
+
dependencies:
|
18
|
+
- !ruby/object:Gem::Dependency
|
19
|
+
name: notahat-machinist
|
20
|
+
type: :runtime
|
21
|
+
version_requirement:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.3
|
27
|
+
version:
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: mongomapper
|
30
|
+
type: :runtime
|
31
|
+
version_requirement:
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 0.6.0
|
37
|
+
version:
|
38
|
+
description:
|
39
|
+
email: tchandy@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- init.rb
|
54
|
+
- lib/machinist/mongomapper.rb
|
55
|
+
- machinist_mongomapper.gemspec
|
56
|
+
- spec/mongomapper_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/tchandy/machinist_mongomapper
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Machinist adapter for MongoMapper
|
86
|
+
test_files:
|
87
|
+
- spec/mongomapper_spec.rb
|
88
|
+
- spec/spec_helper.rb
|