machinist_mongomapper 0.9.4

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.
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
@@ -0,0 +1,5 @@
1
+ Machinist adapter for [MongoMapper](http://github.com/jnunemaker/mongomapper).
2
+
3
+ ## TODO
4
+
5
+ * Spec for EmbeddedDocument.
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_mongomapper"
9
+ gem.summary = %Q{Machinist adapter for MongoMapper}
10
+ gem.email = "dev@yeastymobs.com"
11
+ gem.homepage = "http://github.com/yeastymobs/machinist_mongomapper"
12
+ gem.authors = ["Nicolas Mérouze", "Vincent Hellot", "Mathieu Fosse"]
13
+
14
+ gem.add_dependency('notahat-machinist', '~> 1.0.3')
15
+ gem.add_dependency('mongomapper', '~> 0.4.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
+ 0.9.4
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ config.after_initialize do
2
+ require 'machinist/mongomapper' if Rails.env =~ /^test|cucumber$/
3
+ end
4
+
@@ -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{machinist_mongomapper}
8
+ s.version = "0.9.4"
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"]
12
+ s.date = %q{2009-10-05}
13
+ s.email = %q{dev@yeastymobs.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/yeastymobs/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 = 3
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.4.1"])
47
+ else
48
+ s.add_dependency(%q<notahat-machinist>, ["~> 1.0.3"])
49
+ s.add_dependency(%q<mongomapper>, ["~> 0.4.1"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<notahat-machinist>, ["~> 1.0.3"])
53
+ s.add_dependency(%q<mongomapper>, ["~> 0.4.1"])
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
@@ -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 "mongomapper"
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,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: machinist_mongomapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.4
5
+ platform: ruby
6
+ authors:
7
+ - "Nicolas M\xC3\xA9rouze"
8
+ - Vincent Hellot
9
+ - Mathieu Fosse
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-10-05 00:00:00 +02:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: notahat-machinist
19
+ type: :runtime
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.3
26
+ version:
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongomapper
29
+ type: :runtime
30
+ version_requirement:
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.4.1
36
+ version:
37
+ description:
38
+ email: dev@yeastymobs.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.md
46
+ files:
47
+ - .gitignore
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - VERSION
52
+ - init.rb
53
+ - lib/machinist/mongomapper.rb
54
+ - machinist_mongomapper.gemspec
55
+ - spec/mongomapper_spec.rb
56
+ - spec/spec_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/yeastymobs/machinist_mongomapper
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Machinist adapter for MongoMapper
85
+ test_files:
86
+ - spec/mongomapper_spec.rb
87
+ - spec/spec_helper.rb