mongoid_ar_association 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/lib/mongoid_ar_association/version.rb +3 -0
- data/lib/mongoid_ar_association.rb +140 -0
- data/mongoid_ar_association.gemspec +23 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1e3d1b381c2f6e8ce28dcd463bfd479ee1671831
|
4
|
+
data.tar.gz: c02ad3333e1de5874bf70042f98680ab8493c688
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 053c160aef5ab7e87fdedd34ad150d99f1edf41fea902f9828eb58bcdb3a09673305ee6b4ee6a10568a8b27888d75f0d0db9f23c19f20dd1d0c771fa8e173532
|
7
|
+
data.tar.gz: 15a6a79318b244d4cdb4b85371fd988864fda664411fb5caeb1f7bfa94108794cb9036446b564c358d62a8c6ff8ab6e2d5678fd7f740c0f21efb77442acaccb7
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.bundle
|
11
|
+
*.so
|
12
|
+
*.o
|
13
|
+
*.a
|
14
|
+
mkmf.log
|
15
|
+
=======
|
16
|
+
*.gem
|
17
|
+
*.rbc
|
18
|
+
/.config
|
19
|
+
/coverage/
|
20
|
+
/InstalledFiles
|
21
|
+
/pkg/
|
22
|
+
/spec/reports/
|
23
|
+
/test/tmp/
|
24
|
+
/test/version_tmp/
|
25
|
+
/tmp/
|
26
|
+
|
27
|
+
## Specific to RubyMotion:
|
28
|
+
.dat*
|
29
|
+
.repl_history
|
30
|
+
build/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalisation:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
# Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Amrit Singh
|
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,55 @@
|
|
1
|
+
# MongoidArAssociation
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mongoid_ar_association'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mongoid_ar_association
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Include the module
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
include MongoidArAssociation
|
27
|
+
```
|
28
|
+
|
29
|
+
Create association like:
|
30
|
+
* Connection methods from mongoid to mysql:
|
31
|
+
- belongs_to_record
|
32
|
+
- has_one_record
|
33
|
+
- has_many_records
|
34
|
+
|
35
|
+
* Connection methods from mysql to mongoid:
|
36
|
+
- belongs_to_document
|
37
|
+
- has_one_document
|
38
|
+
- has_many_documents
|
39
|
+
|
40
|
+
examples
|
41
|
+
```
|
42
|
+
has_many_records :addresses, class_name: "User", primary_key: :id, foreign_key: :user_id
|
43
|
+
```
|
44
|
+
|
45
|
+
```
|
46
|
+
has_many_documents :addresses, class_name: "User", primary_key: :id, foreign_key: :user_id
|
47
|
+
```
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( https://github.com/[my-github-username]/mongoid_ar_association/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
require "mongoid_ar_association/version"
|
2
|
+
|
3
|
+
module MongoidArAssociation
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# Connection methods from mongoid to mysql
|
12
|
+
def belongs_to_record(name, options = {})
|
13
|
+
field "#{name}_id", type: Integer
|
14
|
+
object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
|
15
|
+
primary_key = options[:primary_key] || object_class.primary_key
|
16
|
+
foreign_key = options[:foreign_key] || "#{name}_id"
|
17
|
+
self.instance_eval do
|
18
|
+
define_method(name) do |reload = false|
|
19
|
+
if reload
|
20
|
+
self.instance_variable_set("@#{name}", nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
if self.instance_variable_get("@#{name}").blank?
|
24
|
+
self.instance_variable_set("@#{name}", object_class.where(primary_key => self.send(foreign_key)).first)
|
25
|
+
end
|
26
|
+
|
27
|
+
self.instance_variable_get("@#{name}")
|
28
|
+
end
|
29
|
+
|
30
|
+
define_method("#{name}=(new_instance)") do
|
31
|
+
self.send("#{name}_id=", new_instance.id)
|
32
|
+
self.instance_variable_set("@#{name}", nil)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def has_many_records(name, options = {})
|
38
|
+
plural_name = name.to_s.pluralize
|
39
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
40
|
+
object_class = options[:class_name].constantize || name.to_s.singularize.titleize.delete(' ').constantize
|
41
|
+
primary_key = options[:primary_key] || 'id'
|
42
|
+
self.instance_eval do
|
43
|
+
define_method(plural_name) do |reload = false|
|
44
|
+
if reload
|
45
|
+
self.instance_variable_set("@#{name}", nil)
|
46
|
+
end
|
47
|
+
|
48
|
+
if self.instance_variable_get("@#{name}").blank?
|
49
|
+
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key).to_s))
|
50
|
+
end
|
51
|
+
|
52
|
+
self.instance_variable_get("@#{name}")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def has_one_record(name, options = {})
|
58
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
59
|
+
object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
|
60
|
+
primary_key = options[:primary_key] || 'id'
|
61
|
+
self.instance_eval do
|
62
|
+
define_method(name) do |reload = false|
|
63
|
+
if reload
|
64
|
+
self.instance_variable_set("@#{name}", nil)
|
65
|
+
end
|
66
|
+
|
67
|
+
if self.instance_variable_get("@#{name}").blank?
|
68
|
+
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key).to_s).first)
|
69
|
+
end
|
70
|
+
|
71
|
+
self.instance_variable_get("@#{name}")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Connection methods from mysql to mongoid
|
77
|
+
def belongs_to_document(name, options = {})
|
78
|
+
object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
|
79
|
+
primary_key = options[:primary_key] || self.class.primary_key
|
80
|
+
foreign_key = options[:foreign_key] || "#{name}_id"
|
81
|
+
self.instance_eval do
|
82
|
+
define_method(name) do |reload = false|
|
83
|
+
if reload
|
84
|
+
self.instance_variable_set("@#{name}", nil)
|
85
|
+
end
|
86
|
+
|
87
|
+
if self.instance_variable_get("@#{name}").blank?
|
88
|
+
self.instance_variable_set("@#{name}", object_class.where(primary_key => self.send(foreign_key)).first)
|
89
|
+
end
|
90
|
+
|
91
|
+
self.instance_variable_get("@#{name}")
|
92
|
+
end
|
93
|
+
|
94
|
+
define_method("#{name}=(new_instance)") do
|
95
|
+
self.send("#{name}_id=", new_instance.id)
|
96
|
+
self.instance_variable_set("@#{name}", nil)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def has_many_documents(name, options = {})
|
102
|
+
plural_name = name.to_s.pluralize
|
103
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
104
|
+
object_class = options[:class_name].constantize || name.to_s.singularize.titleize.delete(' ').constantize
|
105
|
+
primary_key = options[:primary_key] || self.class.primary_key
|
106
|
+
self.instance_eval do
|
107
|
+
define_method(plural_name) do |reload = false|
|
108
|
+
if reload
|
109
|
+
self.instance_variable_set("@#{name}", nil)
|
110
|
+
end
|
111
|
+
|
112
|
+
if self.instance_variable_get("@#{name}").blank?
|
113
|
+
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key)))
|
114
|
+
end
|
115
|
+
|
116
|
+
self.instance_variable_get("@#{name}")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def has_one_document(name, options = {})
|
122
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
123
|
+
object_class = options[:class_name].constantize || name.to_s.titleize.delete(' ').constantize
|
124
|
+
primary_key = options[:primary_key] || self.class.primary_key
|
125
|
+
self.instance_eval do
|
126
|
+
define_method(name) do |reload = false|
|
127
|
+
if reload
|
128
|
+
self.instance_variable_set("@#{name}", nil)
|
129
|
+
end
|
130
|
+
|
131
|
+
if self.instance_variable_get("@#{name}").blank?
|
132
|
+
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.send(primary_key)).first)
|
133
|
+
end
|
134
|
+
|
135
|
+
self.instance_variable_get("@#{name}")
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -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 'mongoid_ar_association/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid_ar_association"
|
8
|
+
spec.version = MongoidArAssociation::VERSION
|
9
|
+
spec.authors = ["Amrit Singh"]
|
10
|
+
spec.email = ["amrit0403@gmail.com"]
|
11
|
+
spec.summary = %q{Association between Mongoid and Activerecord Models}
|
12
|
+
spec.description = %q{Association between Mongoid and Activerecord Models}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_ar_association
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amrit Singh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-02 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Association between Mongoid and Activerecord Models
|
42
|
+
email:
|
43
|
+
- amrit0403@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/mongoid_ar_association.rb
|
54
|
+
- lib/mongoid_ar_association/version.rb
|
55
|
+
- mongoid_ar_association.gemspec
|
56
|
+
homepage: ''
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
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
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.4.5
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Association between Mongoid and Activerecord Models
|
80
|
+
test_files: []
|