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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12208e51420d3bf1332bd4b482170903d823458bdd62064bbc6a90c7b6e8af8a
4
- data.tar.gz: 86ef2d545231f0164d7d0c05e8279d924c00819ffb1395707157020274234493
3
+ metadata.gz: 221e8b06fc2100e3c646ab82f32c36e527e018bc7a9873fea4c181278af483bb
4
+ data.tar.gz: 2c5c8fee904a1f32e0b3b7f55512742ddbf2dac5c427d3779142243b0d8b135d
5
5
  SHA512:
6
- metadata.gz: 2c974e163a5d91eddd8c933e54abfa56f3e0ae665d3a887113d343070e689560ec5b54d7d044eca8586341e364746890335e07aeeff501d21bf011c27952da45
7
- data.tar.gz: 215584ac4d4589fa16ffacfadebaa29d82ab91018234a8418daf6842c9c70ba528f5db6322c50fee3f0bed7326dda986e77f2600dd083555ab21a1b51433c7ca
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
- # ignore
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 "base"
1
+ require_relative "base_single"
2
2
 
3
3
  module Fume::Aloader
4
4
  module Relationship
5
- class BelongsTo < Base
5
+ class BelongsTo < BaseSingle
6
6
  def get_cache_key(record)
7
7
  record.send(reflection.join_foreign_key)
8
8
  end
@@ -1,8 +1,8 @@
1
- require_relative "base"
1
+ require_relative "base_single"
2
2
 
3
3
  module Fume::Aloader
4
4
  module Relationship
5
- class BelongsToPolymorphic < Base
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
- hash[type] << value if !value.nil?
22
- end
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
@@ -1,8 +1,8 @@
1
- require_relative "base"
1
+ require_relative "base_single"
2
2
 
3
3
  module Fume::Aloader
4
4
  module Relationship
5
- class HasOne < Base
5
+ class HasOne < BaseSingle
6
6
  def get_cache_key(record)
7
7
  record.send(reflection.join_foreign_key)
8
8
  end
@@ -1,5 +1,5 @@
1
1
  module Fume
2
2
  module Aloader
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
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.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-01 00:00:00.000000000 Z
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