mm_eager_includer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/mongo_mapper/eager_includer.rb +192 -0
- metadata +5 -7
- data/lib/mongo_mapper/eager_include.rb +0 -194
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OGNkMGFmNzY2ZjFmMDI1YjA3YzhkNmI5NTk0Y2E4Mzg4YTk0YTBkYg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODY2YTdlY2YwMGE4MTk4Y2EzMGU4ZmM1MjI3MTY4MjRlNDZmNzY2Yw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
M2NhNmI1NjRhZjc3NzUwM2ZhOGU4NTIzMDMxNjE0YTRiNWY1ZjNlMmY3MmM3
|
10
|
+
YTZjNGU5MzMyMTFkYjU2YjBmNjBjOGFlNWRmMDQ1MzhmNTA5N2QxYzA0NTA3
|
11
|
+
ZDI0ZDBjMmRiYmZhYjk0YmZjZjIxYWUwMjgyMDlmZDE3MWU1OTA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjVkMmY1NTI2YjFjYWVhOTQ1ZTdmMzk3MGE5NDNiZDlkNTA2MDU5NTU1YzA4
|
14
|
+
YTQyNzMyY2ZkNDdjMjcxM2RkNGExMTZkZmE1YTc5OTM5Mzg5NjZhODg3OGI4
|
15
|
+
YTkzNzY3YWQ2YmUwZGY1ODYyYmM5MWQ4ZDJlNzE4NWM0YzFiMWQ=
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
class MongoMapper::EagerIncluder
|
4
|
+
class << self
|
5
|
+
def enabled?
|
6
|
+
(@enabled == true || @enabled == false) ? @enabled : true
|
7
|
+
end
|
8
|
+
|
9
|
+
def enabled=(bool)
|
10
|
+
@enabled = bool
|
11
|
+
end
|
12
|
+
|
13
|
+
def eager_include(record_or_records, *association_names, &block)
|
14
|
+
association_names.each do |association_name|
|
15
|
+
new(record_or_records, association_name).eager_include(&block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def write_to_cache(object_id, association_name, value)
|
20
|
+
cache[association_name] ||= {}
|
21
|
+
cache[association_name][object_id] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
def read_from_cache(object_id, association_name)
|
25
|
+
cache[association_name][object_id]
|
26
|
+
end
|
27
|
+
|
28
|
+
def clear_cache!
|
29
|
+
@cache = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def cache
|
35
|
+
@cache ||= {}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(record_or_records, association_name)
|
40
|
+
@records = Array(record_or_records)
|
41
|
+
|
42
|
+
if @records.length == 0
|
43
|
+
return
|
44
|
+
end
|
45
|
+
|
46
|
+
@association_name = association_name.to_sym
|
47
|
+
@association = @records.first.associations[association_name]
|
48
|
+
if !@association
|
49
|
+
raise "Could not find association `#{association_name}` on instance of #{@records.first.class}"
|
50
|
+
end
|
51
|
+
|
52
|
+
@proxy_class = @association.proxy_class
|
53
|
+
end
|
54
|
+
|
55
|
+
def enabled?
|
56
|
+
self.class.enabled?
|
57
|
+
end
|
58
|
+
|
59
|
+
def eager_include(&block)
|
60
|
+
return if !enabled?
|
61
|
+
|
62
|
+
if @records.length == 0
|
63
|
+
return
|
64
|
+
end
|
65
|
+
|
66
|
+
if @proxy_class == MongoMapper::Plugins::Associations::ManyDocumentsProxy
|
67
|
+
eager_include_has_many(&block)
|
68
|
+
elsif @proxy_class == MongoMapper::Plugins::Associations::BelongsToProxy
|
69
|
+
eager_include_belongs_to(&block)
|
70
|
+
elsif @proxy_class == MongoMapper::Plugins::Associations::OneProxy
|
71
|
+
eager_include_has_one(&block)
|
72
|
+
elsif @proxy_class == MongoMapper::Plugins::Associations::InArrayProxy
|
73
|
+
eager_include_has_many_in(&block)
|
74
|
+
else
|
75
|
+
raise NotImplementedError, "#{@proxy_class} not supported yet!"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def setup_association(record, association_name, value)
|
82
|
+
association_name = association_name.to_sym
|
83
|
+
|
84
|
+
self.class.write_to_cache(record.object_id, association_name, value)
|
85
|
+
|
86
|
+
code = <<-CODE
|
87
|
+
def #{association_name}
|
88
|
+
MongoMapper::EagerIncluder.read_from_cache(object_id, :#{association_name})
|
89
|
+
end
|
90
|
+
CODE
|
91
|
+
|
92
|
+
record.instance_eval(code, __FILE__, __LINE__)
|
93
|
+
end
|
94
|
+
|
95
|
+
def foreign_keys
|
96
|
+
@association.options[:in]
|
97
|
+
end
|
98
|
+
|
99
|
+
def foreign_key
|
100
|
+
@association_name.to_s.foreign_key
|
101
|
+
end
|
102
|
+
|
103
|
+
def primary_key
|
104
|
+
@association.options[:foreign_key] || @records.first.class.name.foreign_key
|
105
|
+
end
|
106
|
+
|
107
|
+
def eager_include_has_many(&block)
|
108
|
+
ids = @records.map { |el| el.id }.uniq
|
109
|
+
proxy_records = @association.klass.where({
|
110
|
+
primary_key => {
|
111
|
+
'$in' => ids
|
112
|
+
}
|
113
|
+
}).all
|
114
|
+
|
115
|
+
@records.each do |record|
|
116
|
+
matching_proxy_records = proxy_records.select do |proxy_record|
|
117
|
+
record_or_records = proxy_record.send(primary_key)
|
118
|
+
if record_or_records.is_a?(Array)
|
119
|
+
record_or_records.include?(record.id)
|
120
|
+
else
|
121
|
+
record_or_records == record.id
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
setup_association(record, @association_name, matching_proxy_records)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def eager_include_has_one(&block)
|
130
|
+
ids = @records.map { |el| el.id }.uniq
|
131
|
+
proxy_records = @association.klass.where({
|
132
|
+
primary_key => ids
|
133
|
+
})
|
134
|
+
|
135
|
+
if block
|
136
|
+
proxy_records = block.call(proxy_records)
|
137
|
+
end
|
138
|
+
|
139
|
+
proxy_records = proxy_records.all
|
140
|
+
|
141
|
+
@records.each do |record|
|
142
|
+
matching_proxy_record = proxy_records.detect do |proxy_record|
|
143
|
+
proxy_record.send(primary_key) == record.id
|
144
|
+
end
|
145
|
+
|
146
|
+
setup_association(record, @association_name, matching_proxy_record)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def eager_include_belongs_to(&block)
|
151
|
+
ids = @records.map { |el| el.send(foreign_key) }.uniq
|
152
|
+
|
153
|
+
proxy_records = @association.klass.where({
|
154
|
+
:_id => {
|
155
|
+
'$in' => ids
|
156
|
+
}
|
157
|
+
})
|
158
|
+
|
159
|
+
if block
|
160
|
+
proxy_records = block.call(proxy_records)
|
161
|
+
end
|
162
|
+
|
163
|
+
proxy_records = proxy_records.all
|
164
|
+
|
165
|
+
@records.each do |record|
|
166
|
+
matching_proxy_record = proxy_records.detect do |proxy_record|
|
167
|
+
proxy_record.id == record.send(foreign_key)
|
168
|
+
end
|
169
|
+
|
170
|
+
setup_association(record, @association_name, matching_proxy_record)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def eager_include_has_many_in(&block)
|
175
|
+
ids = @records.map { |el| el.send(foreign_keys) }.flatten.uniq
|
176
|
+
proxy_records = @association.klass.where({
|
177
|
+
'_id' => {
|
178
|
+
'$in' => ids
|
179
|
+
}
|
180
|
+
}).all
|
181
|
+
|
182
|
+
@records.each do |record|
|
183
|
+
proxy_record_ids = record.send(foreign_keys)
|
184
|
+
|
185
|
+
matching_proxy_records = proxy_record_ids.map do |proxy_record_id|
|
186
|
+
proxy_records.detect { |proxy_record| proxy_record.id == proxy_record_id }
|
187
|
+
end
|
188
|
+
|
189
|
+
setup_association(record, @association_name, matching_proxy_records)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mm_eager_includer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Scott Taylor
|
@@ -18,30 +17,29 @@ executables: []
|
|
18
17
|
extensions: []
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
21
|
-
- lib/mongo_mapper/
|
20
|
+
- lib/mongo_mapper/eager_includer.rb
|
22
21
|
homepage: http://github.com/GoLearnUp/eager_include_mm
|
23
22
|
licenses:
|
24
23
|
- MIT
|
24
|
+
metadata: {}
|
25
25
|
post_install_message:
|
26
26
|
rdoc_options: []
|
27
27
|
require_paths:
|
28
28
|
- lib
|
29
29
|
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
-
none: false
|
31
30
|
requirements:
|
32
31
|
- - ! '>='
|
33
32
|
- !ruby/object:Gem::Version
|
34
33
|
version: '0'
|
35
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
-
none: false
|
37
35
|
requirements:
|
38
36
|
- - ! '>='
|
39
37
|
- !ruby/object:Gem::Version
|
40
38
|
version: '0'
|
41
39
|
requirements: []
|
42
40
|
rubyforge_project:
|
43
|
-
rubygems_version:
|
41
|
+
rubygems_version: 2.2.2
|
44
42
|
signing_key:
|
45
|
-
specification_version:
|
43
|
+
specification_version: 4
|
46
44
|
summary: Eager include associations with mongo mapper
|
47
45
|
test_files: []
|
@@ -1,194 +0,0 @@
|
|
1
|
-
require 'mongo_mapper'
|
2
|
-
|
3
|
-
module MongoMapper
|
4
|
-
class EagerIncluder
|
5
|
-
class << self
|
6
|
-
def enabled?
|
7
|
-
(@enabled == true || @enabled == false) ? @enabled : true
|
8
|
-
end
|
9
|
-
|
10
|
-
def enabled=(bool)
|
11
|
-
@enabled = bool
|
12
|
-
end
|
13
|
-
|
14
|
-
def eager_include(record_or_records, *association_names, &block)
|
15
|
-
association_names.each do |association_name|
|
16
|
-
new(record_or_records, association_name).eager_include(&block)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def write_to_cache(object_id, association_name, value)
|
21
|
-
cache[association_name] ||= {}
|
22
|
-
cache[association_name][object_id] = value
|
23
|
-
end
|
24
|
-
|
25
|
-
def read_from_cache(object_id, association_name)
|
26
|
-
cache[association_name][object_id]
|
27
|
-
end
|
28
|
-
|
29
|
-
def clear_cache!
|
30
|
-
@cache = {}
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def cache
|
36
|
-
@cache ||= {}
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def initialize(record_or_records, association_name)
|
41
|
-
@records = Array(record_or_records)
|
42
|
-
|
43
|
-
if @records.length == 0
|
44
|
-
return
|
45
|
-
end
|
46
|
-
|
47
|
-
@association_name = association_name.to_sym
|
48
|
-
@association = @records.first.associations[association_name]
|
49
|
-
if !@association
|
50
|
-
raise "Could not find association `#{association_name}` on instance of #{@records.first.class}"
|
51
|
-
end
|
52
|
-
|
53
|
-
@proxy_class = @association.proxy_class
|
54
|
-
end
|
55
|
-
|
56
|
-
def enabled?
|
57
|
-
self.class.enabled?
|
58
|
-
end
|
59
|
-
|
60
|
-
def eager_include(&block)
|
61
|
-
return if !enabled?
|
62
|
-
|
63
|
-
if @records.length == 0
|
64
|
-
return
|
65
|
-
end
|
66
|
-
|
67
|
-
if @proxy_class == MongoMapper::Plugins::Associations::ManyDocumentsProxy
|
68
|
-
eager_include_has_many(&block)
|
69
|
-
elsif @proxy_class == MongoMapper::Plugins::Associations::BelongsToProxy
|
70
|
-
eager_include_belongs_to(&block)
|
71
|
-
elsif @proxy_class == MongoMapper::Plugins::Associations::OneProxy
|
72
|
-
eager_include_has_one(&block)
|
73
|
-
elsif @proxy_class == MongoMapper::Plugins::Associations::InArrayProxy
|
74
|
-
eager_include_has_many_in(&block)
|
75
|
-
else
|
76
|
-
raise NotImplementedError, "#{@proxy_class} not supported yet!"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
|
82
|
-
def setup_association(record, association_name, value)
|
83
|
-
association_name = association_name.to_sym
|
84
|
-
|
85
|
-
self.class.write_to_cache(record.object_id, association_name, value)
|
86
|
-
|
87
|
-
code = <<-CODE
|
88
|
-
def #{association_name}
|
89
|
-
MongoMapper::EagerIncluder.read_from_cache(object_id, :#{association_name})
|
90
|
-
end
|
91
|
-
CODE
|
92
|
-
|
93
|
-
record.instance_eval(code, __FILE__, __LINE__)
|
94
|
-
end
|
95
|
-
|
96
|
-
def foreign_keys
|
97
|
-
@association.options[:in]
|
98
|
-
end
|
99
|
-
|
100
|
-
def foreign_key
|
101
|
-
@association_name.to_s.foreign_key
|
102
|
-
end
|
103
|
-
|
104
|
-
def primary_key
|
105
|
-
@association.options[:foreign_key] || @records.first.class.name.foreign_key
|
106
|
-
end
|
107
|
-
|
108
|
-
def eager_include_has_many(&block)
|
109
|
-
ids = @records.map { |el| el.id }.uniq
|
110
|
-
proxy_records = @association.klass.where({
|
111
|
-
primary_key => {
|
112
|
-
'$in' => ids
|
113
|
-
}
|
114
|
-
}).all
|
115
|
-
|
116
|
-
@records.each do |record|
|
117
|
-
matching_proxy_records = proxy_records.select do |proxy_record|
|
118
|
-
record_or_records = proxy_record.send(primary_key)
|
119
|
-
if record_or_records.is_a?(Array)
|
120
|
-
record_or_records.include?(record.id)
|
121
|
-
else
|
122
|
-
record_or_records == record.id
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
setup_association(record, @association_name, matching_proxy_records)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def eager_include_has_one(&block)
|
131
|
-
ids = @records.map { |el| el.id }.uniq
|
132
|
-
proxy_records = @association.klass.where({
|
133
|
-
primary_key => ids
|
134
|
-
})
|
135
|
-
|
136
|
-
if block
|
137
|
-
proxy_records = block.call(proxy_records)
|
138
|
-
end
|
139
|
-
|
140
|
-
proxy_records = proxy_records.all
|
141
|
-
|
142
|
-
@records.each do |record|
|
143
|
-
matching_proxy_record = proxy_records.detect do |proxy_record|
|
144
|
-
proxy_record.send(primary_key) == record.id
|
145
|
-
end
|
146
|
-
|
147
|
-
setup_association(record, @association_name, matching_proxy_record)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def eager_include_belongs_to(&block)
|
152
|
-
ids = @records.map { |el| el.send(foreign_key) }.uniq
|
153
|
-
|
154
|
-
proxy_records = @association.klass.where({
|
155
|
-
:_id => {
|
156
|
-
'$in' => ids
|
157
|
-
}
|
158
|
-
})
|
159
|
-
|
160
|
-
if block
|
161
|
-
proxy_records = block.call(proxy_records)
|
162
|
-
end
|
163
|
-
|
164
|
-
proxy_records = proxy_records.all
|
165
|
-
|
166
|
-
@records.each do |record|
|
167
|
-
matching_proxy_record = proxy_records.detect do |proxy_record|
|
168
|
-
proxy_record.id == record.send(foreign_key)
|
169
|
-
end
|
170
|
-
|
171
|
-
setup_association(record, @association_name, matching_proxy_record)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
def eager_include_has_many_in(&block)
|
176
|
-
ids = @records.map { |el| el.send(foreign_keys) }.flatten.uniq
|
177
|
-
proxy_records = @association.klass.where({
|
178
|
-
'_id' => {
|
179
|
-
'$in' => ids
|
180
|
-
}
|
181
|
-
}).all
|
182
|
-
|
183
|
-
@records.each do |record|
|
184
|
-
proxy_record_ids = record.send(foreign_keys)
|
185
|
-
|
186
|
-
matching_proxy_records = proxy_record_ids.map do |proxy_record_id|
|
187
|
-
proxy_records.detect { |proxy_record| proxy_record.id == proxy_record_id }
|
188
|
-
end
|
189
|
-
|
190
|
-
setup_association(record, @association_name, matching_proxy_records)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|