machinist_mongo 1.0.1
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.textile +34 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/lib/machinist/mongo_mapper.rb +62 -0
- data/machinist_mongo.gemspec +55 -0
- data/spec/mongo_mapper_spec.rb +146 -0
- data/spec/spec_helper.rb +19 -0
- metadata +85 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Nicolas Mérouze
|
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.textile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
h1. Machinist Mongo
|
2
|
+
|
3
|
+
It aims to replace machinist_mongomapper to provide Machinist adapters not just for MongoMapper but for all the others MongoDB ORMs too. The MongoMapper is the only one implemented yet so fork and contribute please!
|
4
|
+
|
5
|
+
h2. Machinist MongoMapper
|
6
|
+
|
7
|
+
A "Machinist":http://github.com/notahat/machinist adapter for "MongoMapper":http://github.com/jnunemaker/mongomapper.
|
8
|
+
|
9
|
+
h2. Usage
|
10
|
+
|
11
|
+
Using Machinist MongoMapper is simple. Just install the gem;
|
12
|
+
|
13
|
+
bc. gem install machinist_mongo
|
14
|
+
|
15
|
+
And replace the first line of your @blueprints.rb@;
|
16
|
+
|
17
|
+
bc. require 'machinist/active_record'
|
18
|
+
|
19
|
+
with this;
|
20
|
+
|
21
|
+
bc. require 'machinist/mongo_mapper'
|
22
|
+
|
23
|
+
Now you can use Machinist with your Mongo database. Have fun!
|
24
|
+
|
25
|
+
h2. Todo
|
26
|
+
|
27
|
+
* Write a spec for EmbeddedDocument.
|
28
|
+
|
29
|
+
h2. Contributors
|
30
|
+
|
31
|
+
* "Nicolas Mérouze":http://github.com/nmerouze
|
32
|
+
* "Cyril Mougel":http://github.com/shingara
|
33
|
+
* "Aubrey Holland":http://github.com/aub
|
34
|
+
* "Jeff Kreeftmeijer":http://github.com/jeffkreeftmeijer
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
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.name = "machinist_mongo"
|
9
|
+
gem.summary = %Q{Machinist adapters for MongoDB ORMs}
|
10
|
+
gem.email = "nicolas.merouze@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/nmerouze/machinist_mongo"
|
12
|
+
gem.authors = ["Nicolas Mérouze", "Cyril Mougel"]
|
13
|
+
|
14
|
+
gem.add_dependency('machinist', '~> 1.0.4')
|
15
|
+
gem.add_dependency('mongo_mapper', '~> 0.6.1')
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Default: run specs.'
|
22
|
+
task :default => :spec
|
23
|
+
|
24
|
+
desc 'Run all the specs for the machinist plugin.'
|
25
|
+
Spec::Rake::SpecTask.new do |t|
|
26
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
t.rcov = false
|
28
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.1
|
@@ -0,0 +1,62 @@
|
|
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
|
+
end
|
58
|
+
|
59
|
+
MongoMapper::Document.append_extensions(Machinist::Blueprints::ClassMethods)
|
60
|
+
MongoMapper::Document.append_extensions(Machinist::MongoMapperExtensions::Document)
|
61
|
+
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
|
62
|
+
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Machinist::MongoMapperExtensions::EmbeddedDocument)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{machinist_mongo}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nicolas M\303\251rouze", "Cyril Mougel"]
|
12
|
+
s.date = %q{2010-01-11}
|
13
|
+
s.email = %q{nicolas.merouze@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.textile"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"LICENSE",
|
21
|
+
"README.textile",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/machinist/mongo_mapper.rb",
|
25
|
+
"machinist_mongo.gemspec",
|
26
|
+
"spec/mongo_mapper_spec.rb",
|
27
|
+
"spec/spec_helper.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/nmerouze/machinist_mongo}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.5}
|
33
|
+
s.summary = %q{Machinist adapters for MongoDB ORMs}
|
34
|
+
s.test_files = [
|
35
|
+
"spec/mongo_mapper_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<machinist>, ["~> 1.0.4"])
|
45
|
+
s.add_runtime_dependency(%q<mongo_mapper>, ["~> 0.6.1"])
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<machinist>, ["~> 1.0.4"])
|
48
|
+
s.add_dependency(%q<mongo_mapper>, ["~> 0.6.1"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<machinist>, ["~> 1.0.4"])
|
52
|
+
s.add_dependency(%q<mongo_mapper>, ["~> 0.6.1"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'machinist/mongo_mapper'
|
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 "mongo_mapper"
|
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.remove } }
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: machinist_mongo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Nicolas M\xC3\xA9rouze"
|
8
|
+
- Cyril Mougel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-01-11 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: machinist
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.0.4
|
25
|
+
version:
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mongo_mapper
|
28
|
+
type: :runtime
|
29
|
+
version_requirement:
|
30
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.6.1
|
35
|
+
version:
|
36
|
+
description:
|
37
|
+
email: nicolas.merouze@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- LICENSE
|
44
|
+
- README.textile
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.textile
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/machinist/mongo_mapper.rb
|
52
|
+
- machinist_mongo.gemspec
|
53
|
+
- spec/mongo_mapper_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/nmerouze/machinist_mongo
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Machinist adapters for MongoDB ORMs
|
83
|
+
test_files:
|
84
|
+
- spec/mongo_mapper_spec.rb
|
85
|
+
- spec/spec_helper.rb
|