fume-aloader 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fume/aloader/association_loader.rb +12 -1
- data/lib/fume/aloader/relationship/base_single.rb +25 -0
- data/lib/fume/aloader/relationship/belongs_to.rb +2 -2
- data/lib/fume/aloader/relationship/belongs_to_polymorphic.rb +36 -6
- data/lib/fume/aloader/relationship/has_many.rb +18 -0
- data/lib/fume/aloader/relationship/has_one.rb +2 -2
- data/lib/fume/aloader/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 221e8b06fc2100e3c646ab82f32c36e527e018bc7a9873fea4c181278af483bb
|
4
|
+
data.tar.gz: 2c5c8fee904a1f32e0b3b7f55512742ddbf2dac5c427d3779142243b0d8b135d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dff7bb5abe0b7718f322efd12c965c1806e99875c71758893dc66010a3c81538ee8f4ad9becd714bf08bb1fa1dc6430c5afbb0d91862119caf850d056173b7c
|
7
|
+
data.tar.gz: 3a59ef0c3ae2867a8480edaa84100cf2eaec0f8e8212152b1b79377bf0f8a9c784783fa655d590d9de410d641b993acb964170354b1a0d5538425def5052cea4
|
@@ -33,13 +33,24 @@ module Fume::Aloader
|
|
33
33
|
# prefer use cached value
|
34
34
|
association.target = find_cached_value(record, name)
|
35
35
|
elsif association.loaded?
|
36
|
-
|
36
|
+
ensure_attribute_aloader_inited(record, name)
|
37
37
|
else
|
38
38
|
init_records_value(name)
|
39
39
|
association.target = find_cached_value(record, name)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
def ensure_attribute_aloader_inited(record, name)
|
44
|
+
relationship = Relationship.build(klass, name)
|
45
|
+
return if relationship.loader_is_inited?(record)
|
46
|
+
|
47
|
+
options = active_preset[:attributes][name] || {}
|
48
|
+
loaders = relationship.loaders_init(self.records, options[:preset])
|
49
|
+
(self.preload_values[name] || []).each do |args|
|
50
|
+
loaders.each { |it| it.preload_all(*args) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
43
54
|
def preload_all(*path, values)
|
44
55
|
path = [ path ].flatten
|
45
56
|
name = path.shift
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
module Fume::Aloader
|
4
|
+
module Relationship
|
5
|
+
class BaseSingle < Base
|
6
|
+
def loader_is_inited?(parent)
|
7
|
+
value = parent.send(reflection.name)
|
8
|
+
value.nil? || !!value.aloader
|
9
|
+
end
|
10
|
+
|
11
|
+
def loaders_init(parents, preset)
|
12
|
+
values = parents.map { |it| it.send(reflection.name) }.compact
|
13
|
+
return [] if !reflection.klass.respond_to?(:al_build)
|
14
|
+
loader = reflection.klass.al_build(values)
|
15
|
+
|
16
|
+
values.each do |value|
|
17
|
+
value.aloader = loader
|
18
|
+
end
|
19
|
+
|
20
|
+
loader.active(preset) if preset
|
21
|
+
[ loader ]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require_relative "
|
1
|
+
require_relative "base_single"
|
2
2
|
|
3
3
|
module Fume::Aloader
|
4
4
|
module Relationship
|
5
|
-
class BelongsToPolymorphic <
|
5
|
+
class BelongsToPolymorphic < BaseSingle
|
6
6
|
def get_cache_key(record)
|
7
7
|
[ record.send(reflection.join_foreign_type), record.send(reflection.join_foreign_key) ]
|
8
8
|
end
|
@@ -15,12 +15,13 @@ module Fume::Aloader
|
|
15
15
|
values_mapping = records.each_with_object({}).each do |record, hash|
|
16
16
|
type = record.read_attribute(reflection.join_foreign_type)
|
17
17
|
next if type.nil?
|
18
|
-
hash[type] ||= Set.new
|
19
18
|
|
20
19
|
value = record.read_attribute(reflection.join_foreign_key)
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
if value
|
21
|
+
hash[type] ||= Set.new
|
22
|
+
hash[type] << value
|
23
|
+
end
|
24
|
+
end.compact
|
24
25
|
|
25
26
|
values_mapping.map do |type, values|
|
26
27
|
values = [ values.to_a ]
|
@@ -39,6 +40,35 @@ module Fume::Aloader
|
|
39
40
|
values_scope
|
40
41
|
end
|
41
42
|
end
|
43
|
+
|
44
|
+
def build_values_mapping(records)
|
45
|
+
end
|
46
|
+
|
47
|
+
def loaders_init(parents, preset)
|
48
|
+
values_mapping = parents.each_with_object({}).each do |parent, hash|
|
49
|
+
type = parent.read_attribute(reflection.join_foreign_type)
|
50
|
+
next if type.nil?
|
51
|
+
|
52
|
+
value = parent.send(reflection.name)
|
53
|
+
if value
|
54
|
+
hash[type] ||= Set.new
|
55
|
+
hash[type] << value
|
56
|
+
end
|
57
|
+
end.compact
|
58
|
+
|
59
|
+
values_mapping.each do |type, values|
|
60
|
+
loader_klass = type.constantize
|
61
|
+
next if !loader_klass.respond_to?(:al_build)
|
62
|
+
loader = loader_klass.al_build(values)
|
63
|
+
|
64
|
+
values.each do |value|
|
65
|
+
value.aloader = loader
|
66
|
+
end
|
67
|
+
|
68
|
+
loader.active(preset) if preset
|
69
|
+
loader
|
70
|
+
end
|
71
|
+
end
|
42
72
|
end
|
43
73
|
end
|
44
74
|
end
|
@@ -13,6 +13,24 @@ module Fume::Aloader
|
|
13
13
|
result[key] += [ it ]
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
def loader_is_inited?(parent)
|
18
|
+
values = parent.send(reflection.name)
|
19
|
+
values.nil? || values.empty? || !!values.first.aloader
|
20
|
+
end
|
21
|
+
|
22
|
+
def loaders_init(parents, preset)
|
23
|
+
values = parents.flat_map { |it| it.send(reflection.name) }.compact
|
24
|
+
return [] if !reflection.klass.respond_to?(:al_build)
|
25
|
+
loader = reflection.klass.al_build(values)
|
26
|
+
|
27
|
+
values.each do |value|
|
28
|
+
value.aloader = loader
|
29
|
+
end
|
30
|
+
|
31
|
+
loader.active(preset) if preset
|
32
|
+
[ loader ]
|
33
|
+
end
|
16
34
|
end
|
17
35
|
end
|
18
36
|
end
|
data/lib/fume/aloader/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fume-aloader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sunteya
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 5.1.2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 5.1.2
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: simplecov
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,6 +156,7 @@ files:
|
|
142
156
|
- lib/fume/aloader/relation_addons.rb
|
143
157
|
- lib/fume/aloader/relationship.rb
|
144
158
|
- lib/fume/aloader/relationship/base.rb
|
159
|
+
- lib/fume/aloader/relationship/base_single.rb
|
145
160
|
- lib/fume/aloader/relationship/belongs_to.rb
|
146
161
|
- lib/fume/aloader/relationship/belongs_to_polymorphic.rb
|
147
162
|
- lib/fume/aloader/relationship/has_and_belongs_to_many.rb
|