json_api_preloader 0.0.1 → 0.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: 94b801b1c42dd0c47cc7d8c3960047ebd4bd2d1e1bbfae4dd1f61568162e5326
4
- data.tar.gz: 69bbed7dcd7583534ae224ef48d57382d65f0412e27fd0a07acc8a74a55d83fe
3
+ metadata.gz: d725299c748475bda1ff7a33b4a9a8bda504b611fe9a8934c74194d431f0e9e3
4
+ data.tar.gz: 8826dec180c6ccceecd7661b467385002fcfa91e40f419063e08a6b9ae57a7fb
5
5
  SHA512:
6
- metadata.gz: a87a21c9ac91d7d5c8eaa2a13c8dd6628633d2d17b7e367213d9fbe18c8fdb1a19fdcb810045206b3e627834be0ee9768524a0d5195fc9cee696e1a84fd13c33
7
- data.tar.gz: 6a33984af2af8b66fb24c0510cd65733011ee0a1f1f8365042f6adbcebaf350506f8ae57d579888418d62a6afb39b9162726413a279a0c41ac6fda9b59b56de2
6
+ metadata.gz: 637b8914db72bc81f99492b8392c3a3c6526b318779998d6060272c2c032c13702b50972dee14623d48f1e8c0a04b2dee991a0c69c0e1ff5ff440928dca90435
7
+ data.tar.gz: 5ed82b42166084856533a9603f8eb66a583f5203ea8f8f437b3fb9faa418c60f981055f7a81dca0d7cc8838eac992b20c9c56c4e3b5025eaae6e7a9df2a4d253
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Layout/LineLength:
5
+ Max: 120
6
+
7
+ Naming/VariableNumber:
8
+ EnforcedStyle: snake_case
9
+
10
+ Layout/FirstHashElementIndentation:
11
+ EnforcedStyle: consistent
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ ast (2.4.0)
5
+ jaro_winkler (1.5.3)
6
+ parallel (1.18.0)
7
+ parser (2.6.5.0)
8
+ ast (~> 2.4.0)
9
+ rainbow (3.0.0)
10
+ rubocop (0.75.0)
11
+ jaro_winkler (~> 1.5.1)
12
+ parallel (~> 1.10)
13
+ parser (>= 2.6)
14
+ rainbow (>= 2.2.2, < 4.0)
15
+ ruby-progressbar (~> 1.7)
16
+ unicode-display_width (>= 1.4.0, < 1.7)
17
+ ruby-progressbar (1.10.1)
18
+ unicode-display_width (1.6.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ rubocop
25
+
26
+ BUNDLED WITH
27
+ 1.17.2
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'json_api_preloader'
5
+ s.version = '0.0.2'
6
+ s.authors = ['Kiryl Karetnikau']
7
+ s.license = 'MIT'
8
+ s.email = 'kiryl.karetnikau@gmail.com'
9
+ s.homepage = 'https://github.com/koryushka/json_api_preloader'
10
+ s.summary = 'Preloads associations based on request param `included`'
11
+ s.files = `git ls-files -z`.split("\x0")
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonApiPreloader
4
+ class AssociationsBuilder
5
+ delegate :models, to: ModelsPreloader
6
+
7
+ def call
8
+ models.map do |model_name|
9
+ {
10
+ associations: model_name.constantize.reflect_on_all_associations.map { |ac| { ac.name => ac.klass.name } },
11
+ name: model_name
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonApiPreloader
4
+ class AssociationsChecker
5
+ def initialize(parent, association)
6
+ @parent = parent
7
+ @association = association
8
+ end
9
+
10
+ def call
11
+ return unless parent_klass_associations.present?
12
+
13
+ child_klass_associations&.fetch(association)
14
+ end
15
+
16
+ def self.associations
17
+ @associations ||= AssociationsBuilder.new.call
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :parent, :association
23
+
24
+ def parent_klass_associations
25
+ @parent_klass_associations ||=
26
+ self.class.associations.detect { |el| el[:name] == parent }
27
+ end
28
+
29
+ def child_klass_associations
30
+ @child_klass_associations ||=
31
+ parent_klass_associations[:associations]
32
+ .detect { |el| el.key?(association) }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonApiPreloader
4
+ module Core
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def preload_from_params_for(model_name)
9
+ class_attribute :preloader_configuration
10
+
11
+ self.preloader_configuration = {
12
+ name: model_name.constantize.name
13
+ }
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def preloaded
20
+ included = params[:include]
21
+ return {} unless included.presence
22
+
23
+ nested_resources(
24
+ included: included.split(','),
25
+ parent: preloader_configuration[:name]
26
+ )
27
+ end
28
+
29
+ def modify_params(hsh, ary, parent)
30
+ association = ary.shift
31
+ return unless association
32
+
33
+ new_parent = AssociationsChecker.new(parent, association).call
34
+ return unless new_parent
35
+
36
+ modify_params(hsh[association], ary, new_parent)
37
+ end
38
+
39
+ def nested_resources(included:, parent:)
40
+ default_hash = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
41
+ result = included.each_with_object(default_hash) do |param, hash|
42
+ array = param.split('.').map(&:to_sym)
43
+ modify_params(hash, array, parent)
44
+ end
45
+ result
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonApiPreloader
4
+ class ModelsPreloader
5
+ class << self
6
+ def models
7
+ @models ||= begin
8
+ load_models! unless eager_loaded?
9
+
10
+ model_names
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def eager_loaded?
17
+ Rails.application.config.eager_load
18
+ end
19
+
20
+ def load_models!
21
+ Dir[Rails.root.join('app/models/**/*.rb')].sort.each { |f| require f }
22
+ end
23
+
24
+ def model_names
25
+ ApplicationRecord.descendants.collect(&:name)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,39 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module JsonApiPreloader
4
- def preload_from_params_for(model_name)
5
- class_attribute :preloader_configuration
6
- self.preloader_configuration = {
7
- name: model_name
8
- }
9
- end
10
-
11
- private
12
-
13
- def preloaded(key: nil, parent:)
14
- included = params[:include]
15
- return {} unless included.presence
16
-
17
- nested_resources(included: included.split(','), parent: parent)
18
- end
19
-
20
- def modify_params(hsh, ary, parent)
21
- association = ary.shift
22
- return unless association
23
-
24
- new_parent = AssociationsChecker.new(parent, association).call
25
- return unless new_parent
26
-
27
- modify_params(hsh[association], ary, new_parent)
28
- end
29
-
30
-
31
- def nested_resources(included:, parent:)
32
- default_hash = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
33
- result = included.each_with_object(default_hash) do |param, hash|
34
- array = param.split('.').map(&:to_sym)
35
- modify_params(hash, array, parent)
36
- end
37
- result
38
- end
39
- end
3
+ require 'json_api_preloader/core'
4
+ require 'json_api_preloader/models_preloader'
5
+ require 'json_api_preloader/associations_checker'
6
+ require 'json_api_preloader/associations_builder'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_preloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kiryl Karetnikau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-02 00:00:00.000000000 Z
11
+ date: 2020-04-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: kiryl.karetnikau@gmail.com
@@ -16,9 +16,16 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - ".rubocop.yml"
19
20
  - Gemfile
21
+ - Gemfile.lock
22
+ - json_api_preloader.gemspec
20
23
  - lib/json_api_preloader.rb
21
- homepage:
24
+ - lib/json_api_preloader/associations_builder.rb
25
+ - lib/json_api_preloader/associations_checker.rb
26
+ - lib/json_api_preloader/core.rb
27
+ - lib/json_api_preloader/models_preloader.rb
28
+ homepage: https://github.com/koryushka/json_api_preloader
22
29
  licenses:
23
30
  - MIT
24
31
  metadata: {}