mongo_mysql_relations 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_mysql_relations.rb +99 -100
- data/lib/mongo_mysql_relations/version.rb +1 -1
- metadata +1 -1
@@ -2,136 +2,135 @@ require "mongo_mysql_relations/version"
|
|
2
2
|
|
3
3
|
module MongoMysqlRelations
|
4
4
|
def self.included(base)
|
5
|
-
|
6
|
-
|
7
|
-
end
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
8
7
|
end
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
if self.instance_variable_get("@#{name}").blank?
|
23
|
-
self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send("#{name}_id")).first)
|
24
|
-
end
|
25
|
-
|
26
|
-
self.instance_variable_get("@#{name}")
|
10
|
+
module ClassMethods
|
11
|
+
##################################################################################
|
12
|
+
# Connection methods from mongoid to mysql
|
13
|
+
def to_mysql_belongs_to(name, options = {})
|
14
|
+
field "#{name}_id", type: Integer
|
15
|
+
object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
|
16
|
+
self.instance_eval do
|
17
|
+
define_method(name) do |reload = false|
|
18
|
+
if reload
|
19
|
+
self.instance_variable_set("@#{name}", nil)
|
27
20
|
end
|
28
21
|
|
29
|
-
|
30
|
-
self.send("#{name}_id
|
31
|
-
self.instance_variable_set("@#{name}", nil)
|
22
|
+
if self.instance_variable_get("@#{name}").blank?
|
23
|
+
self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send("#{name}_id")).first)
|
32
24
|
end
|
25
|
+
|
26
|
+
self.instance_variable_get("@#{name}")
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method("#{name}=(new_instance)") do
|
30
|
+
self.send("#{name}_id=", new_instance.id)
|
31
|
+
self.instance_variable_set("@#{name}", nil)
|
33
32
|
end
|
34
33
|
end
|
34
|
+
end
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
self.instance_variable_get("@#{name}")
|
36
|
+
def to_mysql_has_many(name, options = {})
|
37
|
+
plural_name = name.to_s.pluralize
|
38
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
39
|
+
object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
|
40
|
+
self.instance_eval do
|
41
|
+
define_method(plural_name) do |reload = false|
|
42
|
+
if reload
|
43
|
+
self.instance_variable_set("@#{name}", nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
if self.instance_variable_get("@#{name}").blank?
|
47
|
+
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id.to_s))
|
51
48
|
end
|
49
|
+
|
50
|
+
self.instance_variable_get("@#{name}")
|
52
51
|
end
|
53
52
|
end
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
if self.instance_variable_get("@#{name}").blank?
|
65
|
-
self.instance_variable_set("@#{name}", object_class.first(foreign_key => self.id.to_s))
|
66
|
-
end
|
55
|
+
def to_mysql_has_one(name, options = {})
|
56
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
57
|
+
object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
|
58
|
+
self.instance_eval do
|
59
|
+
define_method(name) do |reload = false|
|
60
|
+
if reload
|
61
|
+
self.instance_variable_set("@#{name}", nil)
|
62
|
+
end
|
67
63
|
|
68
|
-
|
64
|
+
if self.instance_variable_get("@#{name}").blank?
|
65
|
+
self.instance_variable_set("@#{name}", object_class.first(foreign_key => self.id.to_s))
|
69
66
|
end
|
67
|
+
|
68
|
+
self.instance_variable_get("@#{name}")
|
70
69
|
end
|
71
70
|
end
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
if self.instance_variable_get("@#{name}").blank?
|
85
|
-
self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send("#{name}_id")).first)
|
86
|
-
end
|
87
|
-
|
88
|
-
self.instance_variable_get("@#{name}")
|
71
|
+
end
|
72
|
+
##################################################################################
|
73
|
+
|
74
|
+
##################################################################################
|
75
|
+
# Connection methods from mysql to mongoid
|
76
|
+
def from_mysql_belongs_to(name, options = {})
|
77
|
+
object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
|
78
|
+
self.instance_eval do
|
79
|
+
define_method(name) do |reload = false|
|
80
|
+
if reload
|
81
|
+
self.instance_variable_set("@#{name}", nil)
|
89
82
|
end
|
90
83
|
|
91
|
-
|
92
|
-
self.send("#{name}_id
|
93
|
-
self.instance_variable_set("@#{name}", nil)
|
84
|
+
if self.instance_variable_get("@#{name}").blank?
|
85
|
+
self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send("#{name}_id")).first)
|
94
86
|
end
|
87
|
+
|
88
|
+
self.instance_variable_get("@#{name}")
|
89
|
+
end
|
90
|
+
|
91
|
+
define_method("#{name}=(new_instance)") do
|
92
|
+
self.send("#{name}_id=", new_instance.id)
|
93
|
+
self.instance_variable_set("@#{name}", nil)
|
95
94
|
end
|
96
95
|
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def from_mysql_has_many(name, options = {})
|
99
|
+
plural_name = name.to_s.pluralize
|
100
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
101
|
+
object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
|
102
|
+
self.instance_eval do
|
103
|
+
define_method(plural_name) do |reload = false|
|
104
|
+
if reload
|
105
|
+
self.instance_variable_set("@#{name}", nil)
|
106
|
+
end
|
97
107
|
|
98
|
-
|
99
|
-
|
100
|
-
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
101
|
-
object_class = options[:class] || name.to_s.singularize.titleize.delete(' ').constantize
|
102
|
-
self.instance_eval do
|
103
|
-
define_method(plural_name) do |reload = false|
|
104
|
-
if reload
|
105
|
-
self.instance_variable_set("@#{name}", nil)
|
106
|
-
end
|
107
|
-
|
108
|
-
if self.instance_variable_get("@#{name}").blank?
|
109
|
-
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id))
|
110
|
-
end
|
111
|
-
|
112
|
-
self.instance_variable_get("@#{name}")
|
108
|
+
if self.instance_variable_get("@#{name}").blank?
|
109
|
+
self.instance_variable_set("@#{name}", object_class.where(foreign_key => self.id))
|
113
110
|
end
|
111
|
+
|
112
|
+
self.instance_variable_get("@#{name}")
|
114
113
|
end
|
115
114
|
end
|
115
|
+
end
|
116
116
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
if self.instance_variable_get("@#{name}").blank?
|
127
|
-
self.instance_variable_set("@#{name}", object_class.first(foreign_key => self.id))
|
128
|
-
end
|
117
|
+
def from_mysql_has_one(name, options = {})
|
118
|
+
foreign_key = options[:foreign_key] || "#{self.name.underscore}_id"
|
119
|
+
object_class = options[:class] || name.to_s.titleize.delete(' ').constantize
|
120
|
+
self.instance_eval do
|
121
|
+
define_method(name) do |reload = false|
|
122
|
+
if reload
|
123
|
+
self.instance_variable_set("@#{name}", nil)
|
124
|
+
end
|
129
125
|
|
130
|
-
|
126
|
+
if self.instance_variable_get("@#{name}").blank?
|
127
|
+
self.instance_variable_set("@#{name}", object_class.first(foreign_key => self.id))
|
131
128
|
end
|
129
|
+
|
130
|
+
self.instance_variable_get("@#{name}")
|
132
131
|
end
|
133
132
|
end
|
134
|
-
##################################################################################
|
135
133
|
end
|
134
|
+
##################################################################################
|
136
135
|
end
|
137
136
|
end
|