machinist_mongo 1.1.1 → 1.2.0

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/README.textile CHANGED
@@ -2,32 +2,23 @@ h1. Machinist Mongo
2
2
 
3
3
  It aims to replace "machinist_mongomapper":http://github.com/yeastymobs/machinist_mongomapper to provide "Machinist":http://github.com/notahat/machinist adapters not just for "MongoMapper":http://mongomapper.com but for all the others MongoDB ORMs too. Right now it supports "Mongoid":http://mongoid.com and "MongoMapper":http://mongomapper.com, but we aim to support _all_ "mongoDB":http://www.mongodb.org/ ORMs, so If you're using something other than MongoMapper or Mongoid, please be sure to write a adapter for it.
4
4
 
5
- h2. Usage
5
+ h2. For Rails 2
6
6
 
7
- Using Machinist MongoMapper is simple. Put this in your @Gemfile@:
7
+ Current Machinist Mongo versions are only compatible with Rails 3. Use 1.x versions to use Machinist Mongo with Rails 2.
8
8
 
9
- bc. gem 'machinist_mongo'
9
+ h2. For Machinist 2
10
10
 
11
- And run:
11
+ There's a machinist2 branch if you want to use Machinist Mongo with Machinist 2. It will be merge in the master branch before the Machinist Mongo 2.0.0 final version.
12
12
 
13
- bc. bundle install
13
+ h2. Usage
14
14
 
15
- Or if you’re using Rails 2.x put this in @config/environment.rb@:
15
+ Using Machinist MongoMapper is simple. Put this in your @Gemfile@:
16
16
 
17
- bc. config.gem 'machinist_mongo'
17
+ bc. gem 'machinist_mongo', :require => 'machinist/mongoid' # or mongo_mapper
18
18
 
19
19
  And run:
20
20
 
21
- bc. rake gems:install
22
-
23
- Now create your @spec/blueprints.rb@ or @test/blueprints.rb@ file as you normally would, only using "Mongoid":http://mongoid.com or "MongoMapper":http://mongomapper.com instead of ActiveRecord:
24
-
25
- bc. require 'machinist/mongo_mapper' # or mongoid
26
- require 'sham'
27
-
28
- Don't forget to require the new @blueprints.rb@ file in your @spec_helper@ (or @test_helper@):
29
-
30
- bc. require File.expand_path(File.dirname(__FILE__) + "/blueprints")
21
+ bc. bundle install
31
22
 
32
23
  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.
33
24
 
@@ -37,3 +28,5 @@ h2. Contributors
37
28
  * "Cyril Mougel":http://github.com/shingara
38
29
  * "Aubrey Holland":http://github.com/aub
39
30
  * "Jeff Kreeftmeijer":http://github.com/jeffkreeftmeijer
31
+ * "Joshua Szmajda":http://github.com/joshsz
32
+ * "Rodrigo Alvarez":http://github.com/Papipo
@@ -1,8 +1,8 @@
1
1
  require "machinist"
2
2
  require "machinist/blueprints"
3
-
4
3
  begin
5
4
  require "mongo_mapper"
5
+ require "mongo_mapper/embedded_document"
6
6
  rescue LoadError
7
7
  puts "MongoMapper is not installed (gem install mongo_mapper)"
8
8
  exit
@@ -20,17 +20,17 @@ module Machinist
20
20
  end
21
21
  end
22
22
  end
23
-
23
+
24
24
  class MongoMapperAdapter
25
25
  def self.has_association?(object, attribute)
26
26
  object.class.associations[attribute]
27
27
  end
28
-
28
+
29
29
  def self.class_for_association(object, attribute)
30
30
  association = object.class.associations[attribute]
31
31
  association && association.klass
32
32
  end
33
-
33
+
34
34
  def self.assigned_attributes_without_associations(lathe)
35
35
  attributes = {}
36
36
  lathe.assigned_attributes.each_pair do |attribute, value|
@@ -57,7 +57,7 @@ module Machinist
57
57
  end
58
58
 
59
59
  def make_unsaved(*args)
60
- returning(Machinist.with_save_nerfed { make(*args) }) do |object|
60
+ Machinist.with_save_nerfed{ make(*args) }.tap do |object|
61
61
  yield object if block_given?
62
62
  end
63
63
  end
@@ -67,8 +67,9 @@ module Machinist
67
67
  Machinist::MongoMapperAdapter.assigned_attributes_without_associations(lathe)
68
68
  end
69
69
  end
70
-
70
+
71
71
  module EmbeddedDocument
72
+
72
73
  def make(*args, &block)
73
74
  lathe = Lathe.run(Machinist::MongoMapperAdapter, self.new, *args)
74
75
  lathe.object(&block)
@@ -12,16 +12,20 @@ module Machinist
12
12
  class Lathe
13
13
  def assign_attribute(key, value)
14
14
  assigned_attributes[key.to_sym] = value
15
- @object.process(key => value)
15
+ if @object.respond_to?("#{key}=")
16
+ @object.send("#{key}=", value)
17
+ else
18
+ @object.process(key => value)
19
+ end
16
20
  end
17
21
  end
18
-
22
+
19
23
  class MongoidAdapter
20
24
  class << self
21
25
  def has_association?(object, attribute)
22
26
  object.class.associations[attribute.to_s]
23
27
  end
24
-
28
+
25
29
  def class_for_association(object, attribute)
26
30
  association = object.class.associations[attribute.to_s]
27
31
  association && association.klass
@@ -31,17 +35,17 @@ module Machinist
31
35
  attributes = {}
32
36
  lathe.assigned_attributes.each_pair do |attribute, value|
33
37
  association = lathe.object.class.associations[attribute.to_s]
34
- if association && (association.macro == :belongs_to_related) && !value.nil?
38
+ if association && (association.macro == :referenced_in) && !value.nil?
35
39
  attributes[association.foreign_key.to_sym] = value.id
36
40
  else
37
41
  attributes[attribute] = value
38
42
  end
39
43
  end
40
- attributes
41
- end
44
+ attributes
45
+ end
42
46
  end
43
47
  end
44
-
48
+
45
49
  module MongoidExtensions
46
50
  module Document
47
51
  def make(*args, &block)
@@ -52,13 +56,13 @@ module Machinist
52
56
  end
53
57
  lathe.object(&block)
54
58
  end
55
-
59
+
56
60
  def make_unsaved(*args)
57
- returning(Machinist.with_save_nerfed { make(*args) }) do |object|
61
+ Machinist.with_save_nerfed { make(*args) }.tap do |object|
58
62
  yield object if block_given?
59
63
  end
60
64
  end
61
-
65
+
62
66
  def plan(*args)
63
67
  lathe = Lathe.run(Machinist::MongoidAdapter, self.new, *args)
64
68
  Machinist::MongoidAdapter.assigned_attributes_without_associations(lathe)
@@ -68,4 +72,4 @@ module Machinist
68
72
  end
69
73
 
70
74
  Mongoid::Document::ClassMethods.send(:include, Machinist::Blueprints::ClassMethods)
71
- Mongoid::Document::ClassMethods.send(:include, Machinist::MongoidExtensions::Document)
75
+ Mongoid::Document::ClassMethods.send(:include, Machinist::MongoidExtensions::Document)
@@ -1,11 +1,11 @@
1
- require File.dirname(__FILE__) + "/spec_helper"
1
+ require "spec_helper"
2
2
  require "machinist/mongo_mapper"
3
3
 
4
4
  Spec::MongoMapper.configure!
5
5
 
6
6
  class Address
7
7
  include MongoMapper::EmbeddedDocument
8
-
8
+
9
9
  key :street, String
10
10
  key :zip, String
11
11
  key :country, String
@@ -13,9 +13,8 @@ end
13
13
 
14
14
  class Person
15
15
  include MongoMapper::Document
16
-
16
+
17
17
  key :name, String
18
- key :type, String
19
18
  key :password, String
20
19
  key :admin, Boolean, :default => false
21
20
  key :address, Address
@@ -23,26 +22,26 @@ end
23
22
 
24
23
  class Post
25
24
  include MongoMapper::Document
26
-
25
+
27
26
  key :title, String
28
27
  key :body, String
29
28
  key :published, Boolean, :default => true
30
-
29
+
31
30
  many :comments
32
31
  end
33
32
 
34
33
  class Comment
35
34
  include MongoMapper::Document
36
-
35
+
37
36
  key :body, String
38
37
  key :post_id, String
39
38
  key :author_id, String
40
-
39
+
41
40
  belongs_to :post
42
41
  belongs_to :author, :class_name => "Person"
43
42
  end
44
43
 
45
- describe Machinist, "MongoMapper::Document adapter" do
44
+ describe Machinist, "MongoMapper::Document adapter" do
46
45
 
47
46
  before(:each) do
48
47
  Person.clear_blueprints!
@@ -62,13 +61,13 @@ describe Machinist, "MongoMapper::Document adapter" do
62
61
  Comment.blueprint { post }
63
62
  Comment.make.post.class.should == Post
64
63
  end
65
-
64
+
66
65
  it "should create an object through belongs_to association with a class_name attribute" do
67
66
  Person.blueprint { }
68
67
  Comment.blueprint { author }
69
68
  Comment.make.author.class.should == Person
70
69
  end
71
-
70
+
72
71
  it "should create an object through belongs_to association using a named blueprint" do
73
72
  Post.blueprint { }
74
73
  Post.blueprint(:dummy) do
@@ -86,13 +85,13 @@ describe Machinist, "MongoMapper::Document adapter" do
86
85
  person = Person.plan
87
86
  Person.count.should == person_count
88
87
  end
89
-
88
+
90
89
  it "should return a regular attribute in the hash" do
91
90
  Post.blueprint { title "Test" }
92
91
  post = Post.plan
93
92
  post[:title].should == "Test"
94
93
  end
95
-
94
+
96
95
  it "should create an object through a belongs_to association, and return its id" do
97
96
  Post.blueprint { }
98
97
  Comment.blueprint { post }
@@ -103,7 +102,7 @@ describe Machinist, "MongoMapper::Document adapter" do
103
102
  comment[:post_id].should_not be_nil
104
103
  end
105
104
 
106
- context "attribute assignment" do
105
+ context "attribute assignment" do
107
106
  it "should allow assigning a value to an attribute" do
108
107
  Post.blueprint { title "1234" }
109
108
  post = Post.make
data/spec/mongoid_spec.rb CHANGED
@@ -9,18 +9,17 @@ class Address
9
9
  field :street
10
10
  field :zip
11
11
  field :country
12
- belongs_to :person, :inverse_of => :address
12
+ embedded_in :person, :inverse_of => :address
13
13
  end
14
14
 
15
15
  class Person
16
16
  include Mongoid::Document
17
17
 
18
- field :name
19
- field :type
18
+ field :name, :accessible => false
20
19
  field :password
21
20
  field :admin, :type => Boolean, :default => false
22
21
 
23
- has_one :address
22
+ embeds_one :address
24
23
  end
25
24
 
26
25
  class Post
@@ -59,19 +58,19 @@ describe Machinist, "Mongoid::Document adapter" do
59
58
  person.should_not be_new_record
60
59
  end
61
60
 
62
- it "should create an object through belongs_to association" do
61
+ it "should create an object through embedded_in association" do
63
62
  Post.blueprint { }
64
63
  Comment.blueprint { post }
65
64
  Comment.make.post.class.should == Post
66
65
  end
67
66
 
68
- it "should create an object through belongs_to association with a class_name attribute" do
67
+ it "should create an object through embedded_in association with a class_name attribute" do
69
68
  Person.blueprint { }
70
69
  Comment.blueprint { author }
71
70
  Comment.make.author.class.should == Person
72
71
  end
73
72
 
74
- it "should create an object through belongs_to association using a named blueprint" do
73
+ it "should create an object through embedded_in association using a named blueprint" do
75
74
  Post.blueprint { }
76
75
  Post.blueprint(:dummy) do
77
76
  title { 'Dummy Post' }
@@ -79,6 +78,13 @@ describe Machinist, "Mongoid::Document adapter" do
79
78
  Comment.blueprint { post(:dummy) }
80
79
  Comment.make.post.title.should == 'Dummy Post'
81
80
  end
81
+
82
+ it "should be able to set attributes which are marked as inaccessible" do
83
+ Person.blueprint do
84
+ name { 'Foobar User' }
85
+ end
86
+ Person.make.name.should == 'Foobar User'
87
+ end
82
88
  end
83
89
 
84
90
  describe "plan method" do
@@ -95,7 +101,7 @@ describe Machinist, "Mongoid::Document adapter" do
95
101
  post[:title].should == "Test"
96
102
  end
97
103
 
98
- it "should create an object through a belongs_to association, and return its id" do
104
+ it "should create an object through a embedded_in association, and return its id" do
99
105
  Post.blueprint { }
100
106
  Comment.blueprint { post }
101
107
  post_count = Post.count
@@ -167,4 +173,4 @@ describe Machinist, "Mongoid::Document adapter" do
167
173
  end
168
174
  end
169
175
 
170
- end
176
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
2
2
  require "rubygems"
3
- require "spec"
3
+ require "rspec"
4
4
  require "sham"
5
5
 
6
6
  module Spec
@@ -8,24 +8,24 @@ module Spec
8
8
  def self.configure!
9
9
  ::MongoMapper.database = "machinist_mongomapper"
10
10
 
11
- Spec::Runner.configure do |config|
11
+ ::Rspec.configure do |config|
12
12
  config.before(:each) { Sham.reset }
13
13
  config.after(:all) { ::MongoMapper.database.collections.each { |c| c.remove } }
14
14
  end
15
15
  end
16
16
  end
17
-
17
+
18
18
  module Mongoid
19
19
  def self.configure!
20
20
  ::Mongoid.configure do |config|
21
21
  config.master = Mongo::Connection.new.db("machinist_mongoid")
22
22
  config.allow_dynamic_fields = true
23
23
  end
24
-
25
- Spec::Runner.configure do |config|
24
+
25
+ ::Rspec.configure do |config|
26
26
  config.before(:each) { Sham.reset }
27
27
  config.after(:all) { ::Mongoid.master.collections.each { |c| c.remove } }
28
28
  end
29
29
  end
30
30
  end
31
- end
31
+ end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: machinist_mongo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
- - 1
9
- - 1
10
- version: 1.1.1
7
+ - 2
8
+ - 0
9
+ version: 1.2.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - "Nicolas M\xC3\xA9rouze"
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-07-09 00:00:00 +02:00
18
+ date: 2011-01-12 00:00:00 +01:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
@@ -27,12 +26,11 @@ dependencies:
27
26
  requirements:
28
27
  - - ~>
29
28
  - !ruby/object:Gem::Version
30
- hash: 31
31
29
  segments:
32
30
  - 1
33
31
  - 0
34
- - 4
35
- version: 1.0.4
32
+ - 6
33
+ version: 1.0.6
36
34
  type: :runtime
37
35
  version_requirements: *id001
38
36
  description:
@@ -45,14 +43,10 @@ extra_rdoc_files:
45
43
  - LICENSE
46
44
  - README.textile
47
45
  files:
48
- - .gitignore
49
46
  - LICENSE
50
- - README.textile
51
- - Rakefile
52
- - VERSION
53
47
  - lib/machinist/mongo_mapper.rb
54
48
  - lib/machinist/mongoid.rb
55
- - machinist_mongo.gemspec
49
+ - README.textile
56
50
  - spec/mongo_mapper_spec.rb
57
51
  - spec/mongoid_spec.rb
58
52
  - spec/spec_helper.rb
@@ -70,7 +64,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
64
  requirements:
71
65
  - - ">="
72
66
  - !ruby/object:Gem::Version
73
- hash: 3
74
67
  segments:
75
68
  - 0
76
69
  version: "0"
@@ -79,7 +72,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
72
  requirements:
80
73
  - - ">="
81
74
  - !ruby/object:Gem::Version
82
- hash: 3
83
75
  segments:
84
76
  - 0
85
77
  version: "0"
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- .DS_Store
2
- pkg
data/Rakefile DELETED
@@ -1,28 +0,0 @@
1
- # encoding: utf-8
2
- require 'rubygems'
3
- require 'rake'
4
- require 'spec/rake/spectask'
5
-
6
- begin
7
- require 'jeweler'
8
- Jeweler::Tasks.new do |gem|
9
- gem.name = "machinist_mongo"
10
- gem.summary = %Q{Machinist adapters for MongoDB ORMs}
11
- gem.email = "nicolas.merouze@gmail.com"
12
- gem.homepage = "http://github.com/nmerouze/machinist_mongo"
13
- gem.authors = ["Nicolas Mérouze", "Cyril Mougel"]
14
-
15
- gem.add_dependency('machinist', '~> 1.0.4')
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 DELETED
@@ -1 +0,0 @@
1
- 1.1.1
@@ -1,55 +0,0 @@
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.1.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-07-09}
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
- "lib/machinist/mongoid.rb",
26
- "machinist_mongo.gemspec",
27
- "spec/mongo_mapper_spec.rb",
28
- "spec/mongoid_spec.rb",
29
- "spec/spec_helper.rb"
30
- ]
31
- s.homepage = %q{http://github.com/nmerouze/machinist_mongo}
32
- s.rdoc_options = ["--charset=UTF-8"]
33
- s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.7}
35
- s.summary = %q{Machinist adapters for MongoDB ORMs}
36
- s.test_files = [
37
- "spec/mongo_mapper_spec.rb",
38
- "spec/mongoid_spec.rb",
39
- "spec/spec_helper.rb"
40
- ]
41
-
42
- if s.respond_to? :specification_version then
43
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
- s.specification_version = 3
45
-
46
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_runtime_dependency(%q<machinist>, ["~> 1.0.4"])
48
- else
49
- s.add_dependency(%q<machinist>, ["~> 1.0.4"])
50
- end
51
- else
52
- s.add_dependency(%q<machinist>, ["~> 1.0.4"])
53
- end
54
- end
55
-