machinist2_mongomapper 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +46 -0
- data/README.textile +15 -0
- data/Rakefile +27 -0
- data/lib/machinist/mongo_mapper.rb +25 -0
- data/lib/machinist/mongo_mapper/blueprint.rb +31 -0
- data/lib/machinist/mongo_mapper/lathe.rb +24 -0
- data/lib/machinist_mongomapper.rb +2 -0
- data/lib/machinist_mongomapper/version.rb +3 -0
- data/machinist_mongomapper.gemspec +28 -0
- data/spec/mongo_mapper_spec.rb +130 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/support/setup.rb +42 -0
- metadata +153 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
machinist2_mongomapper (0.0.1)
|
5
|
+
i18n
|
6
|
+
machinist (~> 2.0.0.beta2)
|
7
|
+
mongo_mapper (~> 0.8.6)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (3.0.3)
|
13
|
+
bson (1.1.2)
|
14
|
+
bson_ext (1.1.2)
|
15
|
+
diff-lcs (1.1.2)
|
16
|
+
i18n (0.4.2)
|
17
|
+
jnunemaker-validatable (1.8.4)
|
18
|
+
activesupport (>= 2.3.4)
|
19
|
+
machinist (2.0.0.beta2)
|
20
|
+
mongo (1.1.2)
|
21
|
+
bson (>= 1.1.1)
|
22
|
+
mongo_mapper (0.8.6)
|
23
|
+
activesupport (>= 2.3.4)
|
24
|
+
jnunemaker-validatable (~> 1.8.4)
|
25
|
+
plucky (~> 0.3.6)
|
26
|
+
plucky (0.3.6)
|
27
|
+
mongo (~> 1.1)
|
28
|
+
rspec (2.1.0)
|
29
|
+
rspec-core (~> 2.1.0)
|
30
|
+
rspec-expectations (~> 2.1.0)
|
31
|
+
rspec-mocks (~> 2.1.0)
|
32
|
+
rspec-core (2.1.0)
|
33
|
+
rspec-expectations (2.1.0)
|
34
|
+
diff-lcs (~> 1.1.2)
|
35
|
+
rspec-mocks (2.1.0)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
bson_ext (= 1.1.2)
|
42
|
+
i18n
|
43
|
+
machinist (~> 2.0.0.beta2)
|
44
|
+
machinist2_mongomapper!
|
45
|
+
mongo_mapper (~> 0.8.6)
|
46
|
+
rspec (~> 2.1.0)
|
data/README.textile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
h1. Machinist2 MongoMapper
|
2
|
+
|
3
|
+
Machinist2 adaptor for mongo_mapper
|
4
|
+
|
5
|
+
h2. Usage
|
6
|
+
|
7
|
+
Using Machinist MongoMapper is simple. Put this in your @Gemfile@:
|
8
|
+
|
9
|
+
bc. gem 'machinist2_mongomapper'
|
10
|
+
|
11
|
+
And run:
|
12
|
+
|
13
|
+
bc. bundle install
|
14
|
+
|
15
|
+
And you're all set. Be sure read "Machinist's README":http://github.com/notahat/machinist to find out how to create your objects and get this thing running.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler::GemHelper.install_tasks
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
|
10
|
+
desc "Run specs"
|
11
|
+
RSpec::Core::RakeTask.new do |t|
|
12
|
+
t.rspec_opts = %w(-fs --color)
|
13
|
+
t.ruby_opts = %w(-w)
|
14
|
+
end
|
15
|
+
|
16
|
+
rescue LoadError
|
17
|
+
task :spec do
|
18
|
+
abort "Run `rake spec:deps` to be able to run the specs"
|
19
|
+
end
|
20
|
+
|
21
|
+
namespace :spec do
|
22
|
+
desc "Ensure spec dependencies are installed"
|
23
|
+
task :deps do
|
24
|
+
sh "gem list rspec | (grep 'rspec (2.0' 1> /dev/null) || gem install rspec --no-ri --no-rdoc"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "machinist"
|
2
|
+
require "machinist/machinable"
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "mongo_mapper"
|
6
|
+
rescue LoadError
|
7
|
+
puts "mongo_mapper is not installed (gem install mongo_mapper)"
|
8
|
+
exit
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'machinist/mongo_mapper/blueprint'
|
12
|
+
require 'machinist/mongo_mapper/lathe'
|
13
|
+
|
14
|
+
module Machinist::MongoMapper
|
15
|
+
module BlueprintClass
|
16
|
+
def blueprint_class
|
17
|
+
Machinist::MongoMapper::Blueprint
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
MongoMapper::Document.append_extensions(Machinist::Machinable)
|
23
|
+
MongoMapper::Document.append_extensions(Machinist::MongoMapper::BlueprintClass)
|
24
|
+
MongoMapper::EmbeddedDocument.append_extensions(Machinist::Machinable)
|
25
|
+
MongoMapper::EmbeddedDocument.append_extensions(Machinist::MongoMapper::BlueprintClass)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Machinist::MongoMapper
|
2
|
+
class Blueprint < Machinist::Blueprint
|
3
|
+
|
4
|
+
# Make and save an object.
|
5
|
+
def make!(attributes = {})
|
6
|
+
object = make(attributes)
|
7
|
+
raise NoMethodError, "Embedded Objects cannot be make! ed" if object.class.embeddable?
|
8
|
+
object.save!
|
9
|
+
object.reload
|
10
|
+
end
|
11
|
+
|
12
|
+
# Box an object for storage in the warehouse.
|
13
|
+
def box(object)
|
14
|
+
object.id
|
15
|
+
end
|
16
|
+
|
17
|
+
# Unbox an object from the warehouse.
|
18
|
+
def unbox(id)
|
19
|
+
@klass.find(id)
|
20
|
+
end
|
21
|
+
|
22
|
+
def outside_transaction
|
23
|
+
yield
|
24
|
+
end
|
25
|
+
|
26
|
+
def lathe_class #:nodoc:
|
27
|
+
Machinist::MongoMapper::Lathe
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Machinist::MongoMapper
|
2
|
+
|
3
|
+
class Lathe < Machinist::Lathe
|
4
|
+
|
5
|
+
def make_one_value(attribute, args) #:nodoc:
|
6
|
+
if block_given?
|
7
|
+
raise_argument_error(attribute) unless args.empty?
|
8
|
+
yield
|
9
|
+
else
|
10
|
+
make_association(attribute, args)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def make_association(attribute, args) #:nodoc:
|
15
|
+
association = @klass.associations[attribute]
|
16
|
+
if association
|
17
|
+
association.klass.make(*args)
|
18
|
+
else
|
19
|
+
raise_argument_error(attribute)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "machinist_mongomapper/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "machinist2_mongomapper"
|
7
|
+
s.version = MachinistMongomapper::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Richard Hooker"]
|
10
|
+
s.email = ["hookercookerman@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{machinist2 compatible mongo_mapper adapter}
|
13
|
+
s.description = %q{does what the summary says}
|
14
|
+
|
15
|
+
s.rubyforge_project = "machinist2_mongomapper"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'mongo_mapper', '~> 0.8.6'
|
22
|
+
s.add_dependency 'machinist', '~> 2.0.0.beta2'
|
23
|
+
s.add_dependency "i18n"
|
24
|
+
|
25
|
+
# == Testing
|
26
|
+
s.add_development_dependency "rspec", "~> 2.1.0"
|
27
|
+
s.add_development_dependency "bson_ext", "1.1.2"
|
28
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
describe "Machinist::MongoMapper" do
|
3
|
+
|
4
|
+
before(:each) do
|
5
|
+
Machinist::Shop.instance.reset!
|
6
|
+
User.clear_blueprints!
|
7
|
+
Post.clear_blueprints!
|
8
|
+
Comment.clear_blueprints!
|
9
|
+
end
|
10
|
+
|
11
|
+
def fake_a_test
|
12
|
+
Machinist.reset_before_test
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
|
16
|
+
context "make" do
|
17
|
+
it "should return an unsaved object" do
|
18
|
+
Post.blueprint { }
|
19
|
+
post = Post.make
|
20
|
+
post.should be_a(Post)
|
21
|
+
post.should be_new_record
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "make!" do
|
26
|
+
it "should make and save objects" do
|
27
|
+
Post.blueprint { }
|
28
|
+
post = Post.make!
|
29
|
+
post.should be_a(Post)
|
30
|
+
post.should_not be_new_record
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should raise an exception for an invalid object" do
|
35
|
+
User.blueprint { }
|
36
|
+
lambda {
|
37
|
+
User.make!(:username => "")
|
38
|
+
}.should raise_error(MongoMapper::DocumentNotValid)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should buy objects from the shop" do
|
42
|
+
Post.blueprint { }
|
43
|
+
post_a, post_b = nil, nil
|
44
|
+
fake_a_test { post_a = Post.make! }
|
45
|
+
fake_a_test { post_b = Post.make! }
|
46
|
+
post_a.should == post_b
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not buy objects from the shop if caching is disabled" do
|
50
|
+
Machinist.configuration.cache_objects = false
|
51
|
+
Post.blueprint { }
|
52
|
+
post_a, post_b = nil, nil
|
53
|
+
fake_a_test { post_a = Post.make! }
|
54
|
+
fake_a_test { post_b = Post.make! }
|
55
|
+
post_a.should_not == post_b
|
56
|
+
Machinist.configuration.cache_objects = true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "associations support" do
|
61
|
+
it "should handle belongs_to associations" do
|
62
|
+
User.blueprint do
|
63
|
+
username { "user_#{sn}" }
|
64
|
+
end
|
65
|
+
Post.blueprint do
|
66
|
+
author
|
67
|
+
end
|
68
|
+
post = Post.make!
|
69
|
+
post.should be_a(Post)
|
70
|
+
post.should_not be_new_record
|
71
|
+
post.author.should be_a(User)
|
72
|
+
post.author.should_not be_new_record
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should handle many associations" do
|
76
|
+
Post.blueprint do
|
77
|
+
comments(3)
|
78
|
+
end
|
79
|
+
Comment.blueprint { }
|
80
|
+
post = Post.make!
|
81
|
+
post.should be_a(Post)
|
82
|
+
post.should_not be_new_record
|
83
|
+
post.should have(3).comments
|
84
|
+
post.comments.each do |comment|
|
85
|
+
comment.should be_a(Comment)
|
86
|
+
comment.should_not be_new_record
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should handle overriding associations" do
|
91
|
+
User.blueprint do
|
92
|
+
username { "user_#{sn}" }
|
93
|
+
end
|
94
|
+
Post.blueprint do
|
95
|
+
author { User.make!(:username => "post_author_#{sn}") }
|
96
|
+
end
|
97
|
+
post = Post.make!
|
98
|
+
post.should be_a(Post)
|
99
|
+
post.should_not be_new_record
|
100
|
+
post.author.should be_a(User)
|
101
|
+
post.author.should_not be_new_record
|
102
|
+
post.author.username.should =~ /^post_author_\d+$/
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "error handling" do
|
107
|
+
it "should raise an exception for an attribute with no value" do
|
108
|
+
User.blueprint { username }
|
109
|
+
lambda {
|
110
|
+
User.make
|
111
|
+
}.should raise_error(ArgumentError)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "embedded documents" do
|
116
|
+
it "should make an embed object" do
|
117
|
+
Address.blueprint { }
|
118
|
+
User.blueprint do
|
119
|
+
address { Address.make }
|
120
|
+
end
|
121
|
+
User.make!(:username => "beans").address.should be_instance_of(Address)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should raise error when #make! on an embedded object" do
|
125
|
+
lambda{
|
126
|
+
Address.make!
|
127
|
+
}.should raise_error(NoMethodError)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.unshift File.expand_path('..', __FILE__)
|
2
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler'
|
6
|
+
require "machinist_mongomapper"
|
7
|
+
require 'rspec'
|
8
|
+
|
9
|
+
::MongoMapper.database = "machinist_mongomapper"
|
10
|
+
|
11
|
+
::Machinist.configure do |config|
|
12
|
+
config.cache_objects = true
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
16
|
+
|
17
|
+
Rspec.configure do |config|
|
18
|
+
config.after(:all) { ::MongoMapper.database.collections.each { |c| c.remove } }
|
19
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Address
|
2
|
+
include MongoMapper::EmbeddedDocument
|
3
|
+
|
4
|
+
key :street, String
|
5
|
+
key :zip, String
|
6
|
+
key :country, String
|
7
|
+
end
|
8
|
+
|
9
|
+
class User
|
10
|
+
include MongoMapper::Document
|
11
|
+
|
12
|
+
key :username, String
|
13
|
+
key :password, String
|
14
|
+
key :admin, Boolean, :default => false
|
15
|
+
key :address, Address
|
16
|
+
|
17
|
+
validates_presence_of :username
|
18
|
+
validates_uniqueness_of :username
|
19
|
+
end
|
20
|
+
|
21
|
+
class Post
|
22
|
+
include MongoMapper::Document
|
23
|
+
|
24
|
+
key :title, String
|
25
|
+
key :body, String
|
26
|
+
key :author_id, ObjectId
|
27
|
+
key :published, Boolean, :default => true
|
28
|
+
|
29
|
+
belongs_to :author, :class_name => "User"
|
30
|
+
many :comments
|
31
|
+
end
|
32
|
+
|
33
|
+
class Comment
|
34
|
+
include MongoMapper::Document
|
35
|
+
|
36
|
+
key :body, String
|
37
|
+
key :post_id, ObjectId
|
38
|
+
key :author_id, String
|
39
|
+
|
40
|
+
belongs_to :post
|
41
|
+
belongs_to :author, :class_name => "User"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: machinist2_mongomapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Richard Hooker
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-22 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mongo_mapper
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 8
|
31
|
+
- 6
|
32
|
+
version: 0.8.6
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: machinist
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 2
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
- beta2
|
48
|
+
version: 2.0.0.beta2
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: i18n
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
type: :runtime
|
63
|
+
version_requirements: *id003
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: rspec
|
66
|
+
prerelease: false
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ~>
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 2
|
74
|
+
- 1
|
75
|
+
- 0
|
76
|
+
version: 2.1.0
|
77
|
+
type: :development
|
78
|
+
version_requirements: *id004
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: bson_ext
|
81
|
+
prerelease: false
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - "="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 1
|
89
|
+
- 1
|
90
|
+
- 2
|
91
|
+
version: 1.1.2
|
92
|
+
type: :development
|
93
|
+
version_requirements: *id005
|
94
|
+
description: does what the summary says
|
95
|
+
email:
|
96
|
+
- hookercookerman@gmail.com
|
97
|
+
executables: []
|
98
|
+
|
99
|
+
extensions: []
|
100
|
+
|
101
|
+
extra_rdoc_files: []
|
102
|
+
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- Gemfile.lock
|
107
|
+
- README.textile
|
108
|
+
- Rakefile
|
109
|
+
- lib/machinist/mongo_mapper.rb
|
110
|
+
- lib/machinist/mongo_mapper/blueprint.rb
|
111
|
+
- lib/machinist/mongo_mapper/lathe.rb
|
112
|
+
- lib/machinist_mongomapper.rb
|
113
|
+
- lib/machinist_mongomapper/version.rb
|
114
|
+
- machinist_mongomapper.gemspec
|
115
|
+
- spec/mongo_mapper_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/support/setup.rb
|
118
|
+
has_rdoc: true
|
119
|
+
homepage: ""
|
120
|
+
licenses: []
|
121
|
+
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
version: "0"
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
segments:
|
141
|
+
- 0
|
142
|
+
version: "0"
|
143
|
+
requirements: []
|
144
|
+
|
145
|
+
rubyforge_project: machinist2_mongomapper
|
146
|
+
rubygems_version: 1.3.7
|
147
|
+
signing_key:
|
148
|
+
specification_version: 3
|
149
|
+
summary: machinist2 compatible mongo_mapper adapter
|
150
|
+
test_files:
|
151
|
+
- spec/mongo_mapper_spec.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
- spec/support/setup.rb
|