deep_preloader 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92fe99d9c1c0a4b426c9827491057c3604e5b511c61c749701de5c65da7d4825
4
- data.tar.gz: efab30b69e968a8a6f45e000da724189808a91dfedb6279930daeb0058ab3557
3
+ metadata.gz: 2905039099c3093587202144bffb2241e17c0f524a7e5b391f94f6526e08b1b3
4
+ data.tar.gz: 03d23e36bbed2e1f6070d7afde2ee27764c8317e4ea28d4e4f5ef624c789d4b4
5
5
  SHA512:
6
- metadata.gz: 4fed23d8d1df1c47f8665298594adf7741243551f207462eabd74e64e86ead26a977ec39df17d791c0c88f09b22e1958d6120b65820c14b51b313b0cc29e0d7c
7
- data.tar.gz: a94f34a7290e83b7f26cbbcdba8ad51445bfd9f1474e2d9ba8f01449196b51a8e44789c8d81114c91a7e647af194331002fe1a1e48c9dedcfab1999ef9fdfc16
6
+ metadata.gz: 6cab0b0b8ac3c49d1610cd1915198fd784bdb29023d4d88f3401b75d35fe673efaa1c4578ca9e4ed74dab3bc234b78ba0c193e446f894c17ba1ec42fd3e44847
7
+ data.tar.gz: 8793d51feefa4dea2e3c8ab3f31d5756776b5b44a8edfc856c1d961f9ac46463ff8037cda4c7aec4742f6a3f56ae85c5f0c17ff8410c79b92a74b6fb012a4334
data/.circleci/config.yml CHANGED
@@ -83,18 +83,18 @@ workflows:
83
83
  version: 2.1
84
84
  build:
85
85
  jobs:
86
- - test:
87
- name: 'ruby 2.5 ActiveRecord 5.2'
88
- ruby-version: "2.5"
89
- gemfile: gemfiles/activerecord_5_2.gemfile
90
86
  - test:
91
87
  name: 'ruby 2.6 ActiveRecord 5.2'
92
88
  ruby-version: "2.6"
93
89
  gemfile: gemfiles/activerecord_5_2.gemfile
94
90
  - test:
95
- name: 'ruby 2.6 ActiveRecord 6.0-beta'
96
- ruby-version: "2.6"
97
- gemfile: gemfiles/activerecord_6_0_beta.gemfile
91
+ name: 'ruby 2.7 ActiveRecord 5.2'
92
+ ruby-version: "2.7"
93
+ gemfile: gemfiles/activerecord_5_2.gemfile
94
+ - test:
95
+ name: 'ruby 3.0 ActiveRecord 6.1'
96
+ ruby-version: "3.0"
97
+ gemfile: gemfiles/activerecord_6_1.gemfile
98
98
  - publish:
99
99
  filters:
100
100
  branches:
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  gemfiles/*.gemfile.lock
11
+ /nix/gem
@@ -3,6 +3,6 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rspec_junit_formatter"
6
- gem "activerecord", "~> 6.0.0.beta"
6
+ gem "activerecord", "~> 6.1.0"
7
7
 
8
8
  gemspec path: "../"
@@ -12,23 +12,7 @@ class DeepPreloader
12
12
  worker = PreloadWorker.new(lock: lock)
13
13
  spec = Spec.parse(spec) unless spec.is_a?(AbstractSpec)
14
14
 
15
- models_by_class = Array.wrap(models).group_by(&:class)
16
-
17
- case spec
18
- when Spec
19
- unless models_by_class.size == 1
20
- raise ArgumentError.new("Provided multiple model types to non-polymorphic preload spec")
21
- end
22
-
23
- model_class, models = models_by_class.first
24
- worker.add_associations_from_spec(models, model_class, spec)
25
- when PolymorphicSpec
26
- models_by_class.each do |model_class, models|
27
- model_spec = spec.for_type(model_class)
28
- next unless model_spec
29
- worker.add_associations_from_spec(models, model_class, model_spec)
30
- end
31
- end
15
+ worker.add_associations_from_spec(models, spec)
32
16
 
33
17
  worker.run!
34
18
  models
@@ -40,23 +24,49 @@ class DeepPreloader
40
24
  @worklist = {}
41
25
  end
42
26
 
43
- def add_associations_from_spec(models, model_class, spec)
44
- spec.association_specs.each do |association_name, child_spec|
45
- association_reflection = model_class.reflect_on_association(association_name)
46
- if association_reflection.nil?
47
- raise ArgumentError.new("Preloading error: couldn't find association #{association_name} on model class #{model_class.name}")
27
+ def add_associations_from_spec(models, spec)
28
+ models = Array.wrap(models)
29
+
30
+ # A polymorphic spec expects models of several types, and defines which
31
+ # associations to preload for each of them.
32
+ if spec.polymorphic?
33
+ # We expect models to be of different types, and to have different subspecs for each.
34
+ models_by_class = models.group_by(&:class)
35
+
36
+ models_by_class.each do |model_class, class_models|
37
+ model_spec = spec.for_type(model_class)
38
+ next unless model_spec
39
+
40
+ add_associations_from_spec(class_models, model_spec)
48
41
  end
42
+ else
43
+ # A non-polymorphic spec implies that the models are all of the same type
44
+ model_class = models.first.class
49
45
 
50
- if association_reflection.polymorphic?
51
- add_polymorphic_association(models, association_reflection, child_spec)
52
- else
53
- add_association(models, association_reflection, child_spec)
46
+ unless models.all? { |m| m.is_a?(model_class) }
47
+ raise ArgumentError.new('Provided multiple model types to a non-polymorphic preload spec')
48
+ end
49
+
50
+ spec.association_specs.each do |association_name, child_spec|
51
+ association_reflection = model_class.reflect_on_association(association_name)
52
+ if association_reflection.nil?
53
+ raise ArgumentError.new("Preloading error: couldn't find association #{association_name} on model class #{model_class.name}")
54
+ end
55
+
56
+ # A polymorphic association links to many different model types
57
+ # (discriminated in the referrer), so must be loaded separately per
58
+ # target model type.
59
+ if association_reflection.polymorphic?
60
+ add_polymorphic_association(models, association_reflection, child_spec)
61
+ else
62
+ add_association(models, association_reflection, child_spec)
63
+ end
54
64
  end
55
65
  end
56
66
  end
57
67
 
58
68
  def run!
59
- while(@worklist.present?)
69
+ while @worklist.present?
60
70
  context, entries = @worklist.shift
61
71
  ActiveRecord::Base.logger&.debug("Preloading children in context #{context}") if DEBUG
62
72
 
@@ -96,12 +106,10 @@ class DeepPreloader
96
106
  children = entry.children
97
107
  child_spec = entry.child_spec
98
108
  next unless child_spec && children.present?
99
- child_class = children.first.class # children of a given parent are all of the same type
100
- add_associations_from_spec(children, child_class, child_spec)
101
- end
102
109
 
110
+ add_associations_from_spec(children, child_spec)
111
+ end
103
112
  end
104
-
105
113
  end
106
114
 
107
115
  private
@@ -242,8 +250,9 @@ class DeepPreloader
242
250
  target = targets
243
251
  else
244
252
  if targets.size > 1
245
- raise RuntimeError.new("Internal preloader error: attempted to attach multiple children to a singular association")
253
+ raise RuntimeError.new('Internal preloader error: attempted to attach multiple children to a singular association')
246
254
  end
255
+
247
256
  target = targets.first
248
257
  end
249
258
 
@@ -253,7 +262,6 @@ class DeepPreloader
253
262
  association.loaded!
254
263
  association.target = target
255
264
  targets.each { |t| association.set_inverse_instance(t) }
256
- targets
257
265
  end
258
266
 
259
267
  def parent_key_column
@@ -267,5 +275,4 @@ class DeepPreloader
267
275
  end
268
276
  end
269
277
  end
270
-
271
278
  end
@@ -2,4 +2,8 @@ class DeepPreloader::AbstractSpec
2
2
  def for_type(clazz)
3
3
  raise ArgumentError.new("Cannot type dispatch on non-polymorphic preload spec #{self.inspect}")
4
4
  end
5
+
6
+ def polymorphic?
7
+ false
8
+ end
5
9
  end
@@ -18,6 +18,10 @@ class DeepPreloader::PolymorphicSpec < DeepPreloader::AbstractSpec
18
18
  @specs_by_type = specs_by_type
19
19
  end
20
20
 
21
+ def polymorphic?
22
+ true
23
+ end
24
+
21
25
  def for_type(clazz)
22
26
  specs_by_type[clazz.name]
23
27
  end
@@ -1,3 +1,3 @@
1
1
  class DeepPreloader
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
data/nix/generate.rb ADDED
@@ -0,0 +1,42 @@
1
+ #! /usr/bin/env nix-shell
2
+ #! nix-shell -i ruby -p ruby -p bundler -p bundix
3
+ # frozen_string_literal: true
4
+
5
+ # Bundix doesn't support `gemspec` directive in Gemfiles, as it doesn't copy the
6
+ # gemspec (and its dependencies) into the store.
7
+ # This workaround is from https://github.com/manveru/bundix/issues/10#issuecomment-405879379
8
+
9
+ require 'shellwords'
10
+ require 'uri'
11
+
12
+ def sh(*args)
13
+ warn args.shelljoin
14
+ system(*args) || raise
15
+ end
16
+
17
+ sh 'bundle', 'lock'
18
+
19
+ require 'fileutils'
20
+ require 'bundler'
21
+
22
+ lockfile = Bundler::LockfileParser.new(File.read('Gemfile.lock'))
23
+ gems = lockfile.specs.select { |spec| spec.source.is_a?(Bundler::Source::Rubygems) }
24
+ sources = [URI('https://rubygems.org/')] | gems.map(&:source).flat_map(&:remotes)
25
+
26
+ FileUtils.mkdir_p 'nix/gem'
27
+ Dir.chdir 'nix/gem' do
28
+ ['Gemfile', 'Gemfile.lock', 'gemset.nix'].each do |f|
29
+ File.delete(f) if File.exist?(f)
30
+ end
31
+
32
+ File.open('Gemfile', 'w') do |gemfile|
33
+ sources.each { |source| gemfile.puts "source #{source.to_s.inspect}" }
34
+ gemfile.puts
35
+
36
+ gems.each do |gem|
37
+ gemfile.puts "gem #{gem.name.inspect}, #{gem.version.to_s.inspect}"
38
+ end
39
+ end
40
+
41
+ sh 'bundix', '-l'
42
+ end
data/shell.nix ADDED
@@ -0,0 +1,11 @@
1
+ with (import <nixpkgs> {});
2
+ let
3
+ env = bundlerEnv {
4
+ name = "bundler-env";
5
+ gemdir = ./nix/gem;
6
+ ruby = ruby_3_0;
7
+ };
8
+ in stdenv.mkDerivation {
9
+ name = "shell";
10
+ buildInputs = [ env ];
11
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_preloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - iKnow Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-21 00:00:00.000000000 Z
11
+ date: 2021-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -184,12 +184,14 @@ files:
184
184
  - bin/setup
185
185
  - deep_preloader.gemspec
186
186
  - gemfiles/activerecord_5_2.gemfile
187
- - gemfiles/activerecord_6_0_beta.gemfile
187
+ - gemfiles/activerecord_6_1.gemfile
188
188
  - lib/deep_preloader.rb
189
189
  - lib/deep_preloader/abstract_spec.rb
190
190
  - lib/deep_preloader/polymorphic_spec.rb
191
191
  - lib/deep_preloader/spec.rb
192
192
  - lib/deep_preloader/version.rb
193
+ - nix/generate.rb
194
+ - shell.nix
193
195
  homepage: http://github.com/iknow/deep_preloader
194
196
  licenses:
195
197
  - MIT
@@ -209,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
209
211
  - !ruby/object:Gem::Version
210
212
  version: '0'
211
213
  requirements: []
212
- rubygems_version: 3.0.3
214
+ rubygems_version: 3.0.3.1
213
215
  signing_key:
214
216
  specification_version: 4
215
217
  summary: Explicit preloader for ActiveRecord