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.
@@ -2,136 +2,135 @@ require "mongo_mysql_relations/version"
2
2
 
3
3
  module MongoMysqlRelations
4
4
  def self.included(base)
5
- base.class_eval do
6
- extend ClassMethods
7
- end
5
+ base.class_eval do
6
+ extend ClassMethods
8
7
  end
8
+ end
9
9
 
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)
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
- define_method("#{name}=(new_instance)") do
30
- self.send("#{name}_id=", new_instance.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
- 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))
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
- 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
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
- self.instance_variable_get("@#{name}")
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
- # 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)
82
- end
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
- define_method("#{name}=(new_instance)") do
92
- self.send("#{name}_id=", new_instance.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
- 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
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
- 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
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
- self.instance_variable_get("@#{name}")
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
@@ -1,3 +1,3 @@
1
1
  module MongoMysqlRelations
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mysql_relations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: