machinist-mongoid 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +34 -0
- data/Rakefile +1 -0
- data/lib/machinist/mongoid/version.rb +5 -0
- data/lib/machinist/mongoid.rb +89 -0
- data/machinist-mongoid.gemspec +23 -0
- data/spec/mongoid_spec.rb +136 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/mongoid.yml +8 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 117eaff98c32d0739aa64484647c3087cd099cb3
|
4
|
+
data.tar.gz: 580c778ede35414aa8fb41b2d0f80a7c2d895f3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7372767b1b655ed9c0ef467a4a4aae7872f6831cd3822e7dcc1751da060a78edba6aef7affefa1bcebea3308b0ffbf2aa46d01b97c68be442ce6d30267d4b289
|
7
|
+
data.tar.gz: e3be69d29bfec9e521c2dcbf6b8d45d2f4cb30bf067947a87c85822ee0d050b0be8da0f512de68ac8f20a8fddecc855bf0b1b4cb93f6f80be0c11ee0c36c69ed
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Blake Chambers
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Machinist::Mongoid
|
2
|
+
|
3
|
+
This library is intended to support machinist 2.0 factories within Mongoid 3.x
|
4
|
+
codebases. It's inspired by the existing [machinist_mongo][mm] gem that doesn't
|
5
|
+
support mongoid 3.x.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'machinist-mongoid'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install machinist-mongoid
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
32
|
+
|
33
|
+
|
34
|
+
[mm]: https://github.com/nmerouze/machinist_mongo
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "machinist/mongoid/version"
|
2
|
+
require "machinist"
|
3
|
+
require "machinist/machinable"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "mongoid"
|
7
|
+
rescue LoadError
|
8
|
+
puts "Mongoid is not installed (gem install mongoid)"
|
9
|
+
exit
|
10
|
+
end
|
11
|
+
|
12
|
+
module Machinist
|
13
|
+
|
14
|
+
module Mongoid
|
15
|
+
|
16
|
+
module Machinable
|
17
|
+
extend ActiveSupport::Concern
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
include Machinist::Machinable
|
21
|
+
def blueprint_class
|
22
|
+
Machinist::Mongoid::Blueprint
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Blueprint < Machinist::Blueprint
|
28
|
+
|
29
|
+
def make!(attributes = {})
|
30
|
+
object = make(attributes)
|
31
|
+
object.save!
|
32
|
+
object.reload
|
33
|
+
end
|
34
|
+
|
35
|
+
def lathe_class #:nodoc:
|
36
|
+
Machinist::Mongoid::Lathe
|
37
|
+
end
|
38
|
+
|
39
|
+
def outside_transaction
|
40
|
+
yield
|
41
|
+
end
|
42
|
+
|
43
|
+
def box(object)
|
44
|
+
object.id
|
45
|
+
end
|
46
|
+
|
47
|
+
# Unbox an object from the warehouse.
|
48
|
+
def unbox(id)
|
49
|
+
@klass.find(id)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Lathe < Machinist::Lathe
|
54
|
+
def make_one_value(attribute, args) #:nodoc:
|
55
|
+
if block_given?
|
56
|
+
raise_argument_error(attribute) unless args.empty?
|
57
|
+
yield
|
58
|
+
else
|
59
|
+
make_association(attribute, args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def make_association(attribute, args) #:nodoc:
|
64
|
+
association = @klass.associations[attribute.to_s]
|
65
|
+
if association
|
66
|
+
association.klass.make(*args)
|
67
|
+
else
|
68
|
+
raise_argument_error(attribute)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def assign_attribute(key, value)
|
73
|
+
@assigned_attributes[key.to_sym] = value
|
74
|
+
if @object.respond_to?("#{key}=")
|
75
|
+
@object.send("#{key}=", value)
|
76
|
+
else
|
77
|
+
@object[key] = value
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
module Mongoid #:nodoc:
|
85
|
+
module Document #:nodoc:
|
86
|
+
include Machinist::Mongoid::Machinable
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'machinist/mongoid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "machinist-mongoid"
|
8
|
+
spec.version = Machinist::Mongoid::VERSION
|
9
|
+
spec.authors = ["Blake Chambers"]
|
10
|
+
spec.email = ["chambb1@gmail.com"]
|
11
|
+
spec.description = %q{Machinist for Mongoid 3.x}
|
12
|
+
spec.summary = %q{Machinist for Mongoid 3.x}
|
13
|
+
spec.homepage = "http://github.com/blakechambers/machinist-mongoid"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
require "machinist/mongoid"
|
3
|
+
|
4
|
+
MachinistTestHelper.configure!
|
5
|
+
|
6
|
+
class Address
|
7
|
+
include Mongoid::Document
|
8
|
+
|
9
|
+
field :street
|
10
|
+
field :zip
|
11
|
+
field :country
|
12
|
+
embedded_in :person, :inverse_of => :address
|
13
|
+
end
|
14
|
+
|
15
|
+
class Person
|
16
|
+
include Mongoid::Document
|
17
|
+
|
18
|
+
field :name
|
19
|
+
field :password
|
20
|
+
field :admin, :type => Boolean, :default => false
|
21
|
+
|
22
|
+
embeds_one :address
|
23
|
+
end
|
24
|
+
|
25
|
+
class Post
|
26
|
+
include Mongoid::Document
|
27
|
+
|
28
|
+
field :title
|
29
|
+
field :body
|
30
|
+
field :published, :type => Boolean, :default => true
|
31
|
+
|
32
|
+
has_many :comments
|
33
|
+
end
|
34
|
+
|
35
|
+
class Comment
|
36
|
+
include Mongoid::Document
|
37
|
+
|
38
|
+
field :body
|
39
|
+
field :post_id
|
40
|
+
field :author_id
|
41
|
+
|
42
|
+
belongs_to :post
|
43
|
+
belongs_to :author, :class_name => "Person"
|
44
|
+
end
|
45
|
+
|
46
|
+
describe Machinist, "Mongoid::Document adapter" do
|
47
|
+
|
48
|
+
before(:each) do
|
49
|
+
Person.clear_blueprints!
|
50
|
+
Post.clear_blueprints!
|
51
|
+
Comment.clear_blueprints!
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "make method" do
|
55
|
+
it "should save the constructed object" do
|
56
|
+
Person.blueprint { }
|
57
|
+
person = Person.make!
|
58
|
+
person.should_not be_new_record
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should create an object through embedded_in association" do
|
62
|
+
Post.blueprint { }
|
63
|
+
Comment.blueprint { post }
|
64
|
+
Comment.make.post.should be_instance_of(Post)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should create an object through embedded_in association with a class_name attribute" do
|
68
|
+
Person.blueprint { }
|
69
|
+
Comment.blueprint { author }
|
70
|
+
Comment.make.author.should be_instance_of(Person)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should create an object through embedded_in association using a named blueprint" do
|
74
|
+
Post.blueprint { }
|
75
|
+
Post.blueprint(:dummy) do
|
76
|
+
title { 'Dummy Post' }
|
77
|
+
end
|
78
|
+
Comment.blueprint { post(:dummy) }
|
79
|
+
Comment.make.post.title.should == 'Dummy Post'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "plan method" do
|
84
|
+
context "attribute assignment" do
|
85
|
+
it "should allow assigning a value to an attribute" do
|
86
|
+
Post.blueprint { title {"1234"} }
|
87
|
+
post = Post.make!
|
88
|
+
post.title.should == "1234"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should allow arbitrary attributes on the base model in its blueprint" do
|
92
|
+
Post.blueprint { foo {"bar"} }
|
93
|
+
post = Post.make!
|
94
|
+
post.foo.should == "bar"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "make_unsaved method" do
|
100
|
+
it "should not save the constructed object" do
|
101
|
+
Person.blueprint { }
|
102
|
+
person = Person.make
|
103
|
+
person.should be_new_record
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should not save associated objects" do
|
107
|
+
Post.blueprint { }
|
108
|
+
Comment.blueprint { post }
|
109
|
+
comment = Comment.make
|
110
|
+
comment.post.should be_new_record
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "make method with embedded documents" do
|
115
|
+
it "should construct object" do
|
116
|
+
Address.blueprint { }
|
117
|
+
address = Address.make
|
118
|
+
address.should be_instance_of(Address)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should make an embed object" do
|
122
|
+
Address.blueprint { }
|
123
|
+
Person.blueprint do
|
124
|
+
address { Address.make }
|
125
|
+
end
|
126
|
+
Person.make.address.should be_instance_of(Address)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should allow arbitrary attributes on the base model in its blueprint" do
|
130
|
+
Address.blueprint { foo {"bar"} }
|
131
|
+
addr = Address.make
|
132
|
+
addr.foo.should == "bar"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
2
|
+
require "rubygems"
|
3
|
+
require "rspec"
|
4
|
+
#require "sham"
|
5
|
+
|
6
|
+
require 'bundler/setup'
|
7
|
+
|
8
|
+
module MachinistTestHelper
|
9
|
+
def self.configure!
|
10
|
+
::Mongoid.load!(File.join([File.dirname(__FILE__), "support/mongoid.yml"]), :test)
|
11
|
+
|
12
|
+
::RSpec.configure do |config|
|
13
|
+
config.after(:all) { ::Mongoid.default_session.collections.each { |coll| coll.drop } }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: machinist-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blake Chambers
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Machinist for Mongoid 3.x
|
42
|
+
email:
|
43
|
+
- chambb1@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/machinist/mongoid.rb
|
55
|
+
- lib/machinist/mongoid/version.rb
|
56
|
+
- machinist-mongoid.gemspec
|
57
|
+
- spec/mongoid_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
- spec/support/mongoid.yml
|
60
|
+
homepage: http://github.com/blakechambers/machinist-mongoid
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.0.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Machinist for Mongoid 3.x
|
84
|
+
test_files:
|
85
|
+
- spec/mongoid_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/support/mongoid.yml
|