auto_preload 0.1.0 → 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: 27b495a7c7242a5cf7b8adbd58d4bd5b94b517f6c4328d0cd25ce9dd33108438
4
- data.tar.gz: dfa50abfe824c5a79ebfff08549e56e3fd9e84269a6f2bf4d7ac55885457ac99
3
+ metadata.gz: 83981d329237a816c396eeeb96058bf601531562846d5d365b664a25cbc683b6
4
+ data.tar.gz: 7090bcf8922549d45cd8410a98d09d841a17cdbd822dae21e1f69d8abd918890
5
5
  SHA512:
6
- metadata.gz: 1e499cdcc1ec269098a702eb824aa3f3a36d730b64867f74b359f5595d80e57f617942ad0decabf717baf1de01df2649b1a644ee4b521ecf22f9b4664a069692
7
- data.tar.gz: b13655e3b6423d98a2b6f26d6f91e7096be16c890a4a6449fda57e986959733d11d1f1a8cd5119dd4fcd9bff93f46d387a7bc16e4dd4b603754ab5162dbe2d8c
6
+ metadata.gz: 61bb4dcb606073730f9edbf2d8888d9e28bdafaf2e22a409866a95bd771462ca83ce08a6c1828f14c65bb540d9f3e4bd3968e04bef3df40d23c49e3ab4e2b7f8
7
+ data.tar.gz: 4fff43c826eeb13787beb5f7b1c179c7d05045ea8090a91257c91264ff15f23144d8276137d0771a5f682952916cc8feeb2073b146b74397f1e6a267fe4d7316
@@ -16,9 +16,9 @@ jobs:
16
16
  steps:
17
17
  - uses: actions/checkout@v3
18
18
  - name: Set up Ruby 3.0
19
- uses: actions/setup-ruby@v1
19
+ uses: ruby/setup-ruby@v1
20
20
  with:
21
- ruby-version: 3.0.x
21
+ ruby-version: '3.0'
22
22
 
23
23
  - name: Publish to GPR
24
24
  run: |
data/.solargraph.yml ADDED
@@ -0,0 +1,25 @@
1
+ ---
2
+ include:
3
+ - "**/*.rb"
4
+ exclude:
5
+ - spec/**/*
6
+ - test/**/*
7
+ - vendor/**/*
8
+ - ".bundle/**/*"
9
+ require:
10
+ - activesupport
11
+ - activerecord
12
+ domains: []
13
+ reporters:
14
+ - rubocop
15
+ - require_not_found
16
+ - typecheck
17
+ formatter:
18
+ rubocop:
19
+ cops: safe
20
+ except: []
21
+ only: []
22
+ extra_args: []
23
+ require_paths: []
24
+ plugins: []
25
+ max_files: 5000
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.2] - 2022-12-10
9
+
10
+ ### Added
11
+ - Documentation to all methods
12
+ - `solargraph` gem for development
13
+
14
+ ## [0.1.1] - 2022-11-30
15
+
16
+ ### Fixed
17
+ - Prevent crash when passing a non-existing association
18
+
19
+ ## [0.1.0] - 2022-11-30
20
+
21
+ ### Added
22
+
23
+ - First release
data/Gemfile CHANGED
@@ -12,6 +12,8 @@ gem "rspec", "~> 3.0"
12
12
 
13
13
  gem "rubocop", "~> 1.7"
14
14
 
15
+ gem "solargraph"
16
+
15
17
  gem "active_model_serializers", "~> 0.10"
16
18
  gem "simplecov"
17
19
  gem "yard"
@@ -1,32 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutoPreload
4
+ # Extensions to ActiveRecord::Base
4
5
  module ActiveRecord
5
6
  extend ActiveSupport::Concern
6
7
 
7
8
  included do
9
+ # @param [String, Array<String>] inclusions
10
+ # @param [Hash] options
11
+ # @return [ActiveRecord::Relation]
8
12
  scope :auto_includes, lambda { |inclusions, options = {}|
9
13
  if inclusions.present?
10
- includes(*Resolver.new(options).resolve(self, inclusions))
14
+ resolved = Resolver.new(options).resolve(self, inclusions)
15
+ resolved.empty? ? self : includes(*resolved)
11
16
  else
12
17
  self
13
18
  end
14
19
  }
20
+ # @param [String, Array<String>] inclusions
21
+ # @param [Hash] options
22
+ # @return [ActiveRecord::Relation]
15
23
  scope :auto_preload, lambda { |inclusions, options = {}|
16
24
  if inclusions.present?
17
- preload(*Resolver.new(options).resolve(self, inclusions))
25
+ resolved = Resolver.new(options).resolve(self, inclusions)
26
+ resolved.empty? ? self : preload(*resolved)
18
27
  else
19
28
  self
20
29
  end
21
30
  }
31
+ # @param [String, Array<String>] inclusions
32
+ # @param [Hash] options
33
+ # @return [ActiveRecord::Relation]
22
34
  scope :auto_eager_load, lambda { |inclusions, options = {}|
23
35
  if inclusions.present?
24
- eager_load(*Resolver.new(options).resolve(self, inclusions))
36
+ resolved = Resolver.new(options).resolve(self, inclusions)
37
+ resolved.empty? ? self : eager_load(*resolved)
25
38
  else
26
39
  self
27
40
  end
28
41
  }
29
42
 
43
+ # @return [nil, Array<Symbol>]
30
44
  class_attribute :auto_preloadable
31
45
  end
32
46
  end
@@ -4,6 +4,8 @@ module AutoPreload
4
4
  module Adapters
5
5
  # This class takes a model and finds all the preloadable associations.
6
6
  class ActiveRecord
7
+ # @param [ActiveRecord::Base] model
8
+ # @return [Array<ActiveRecord::Reflection>] The preloadable associations.
7
9
  def resolve_preloadables(model, _options = {})
8
10
  if model.auto_preloadable
9
11
  model.auto_preloadable.map { |w| model.reflect_on_association(w) }.compact
@@ -24,6 +24,11 @@ module AutoPreload
24
24
  end.compact
25
25
  end
26
26
 
27
+ # @param model [ActiveRecord::Base]
28
+ # @param options [Hash]
29
+ # @option options [Boolean] :root
30
+ # @option options [ActiveModel::Serializer] :serializer
31
+ # @return [ActiveModel::Serializer]
27
32
  def resolve_serializer(model, options = {})
28
33
  if options[:root]
29
34
  options[:serializer] || ActiveModel::Serializer.serializer_for(model)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutoPreload
4
+ # This module contains the adapters used to parse JSON::API include strings.
4
5
  module Adapters
5
6
  extend ActiveSupport::Autoload
6
7
 
@@ -3,7 +3,7 @@
3
3
  module AutoPreload
4
4
  # This class handles the gem configurations.
5
5
  class Config
6
- # @attr_writr [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] adapter The adapter to use.
6
+ # @return [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] The adapter to use.
7
7
  attr_writer :adapter
8
8
 
9
9
  # @return [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] The adapter to use.
@@ -3,8 +3,13 @@
3
3
  module AutoPreload
4
4
  # This class parses a string in the format "articles,comments" and returns an array of symbols.
5
5
  class Resolver
6
+ # Default max number of iterations
7
+ # @return [Integer]
6
8
  MAX_ITERATIONS = 100
7
9
 
10
+ # @param options [Hash]
11
+ # @option options [Integer] :max_iterations
12
+ # @option options [AutoPreload::Adapters::ActiveRecord, AutoPreload::Adapters::Serializer] :adapter
8
13
  def initialize(options = {})
9
14
  @iterations = 0
10
15
  @options = options
@@ -43,7 +48,7 @@ module AutoPreload
43
48
  objects.present? ? (symbols << objects) : symbols
44
49
  end
45
50
 
46
- # @param list [Array<Hash>]
51
+ # @param objects [Array<Hash>]
47
52
  # @return [Hash]
48
53
  def merge(objects)
49
54
  objects.reduce({}) do |result, object|
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AutoPreload
4
- VERSION = "0.1.0"
4
+ # @return [String] The version of the gem.
5
+ VERSION = "0.1.2"
5
6
  end
data/lib/auto_preload.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require "active_support"
4
4
  require "active_record"
5
5
 
6
+ # Provides methods to run `preload`/`includes`/`eager_load` on your model
7
+ # from a JSON::API include string.
6
8
  module AutoPreload
7
9
  extend ActiveSupport::Autoload
8
10
 
@@ -11,13 +13,18 @@ module AutoPreload
11
13
  autoload :Resolver
12
14
  autoload :Config
13
15
 
16
+ # @yield [AutoPreload::Config]
17
+ # @return [AutoPreload::Config]
14
18
  def self.configure
15
19
  yield(config)
16
20
  end
17
21
 
22
+ # @return [AutoPreload::Config]
18
23
  def self.config
19
24
  @config ||= Config.new
20
25
  end
21
26
  end
22
27
 
23
- ActiveRecord::Base.include AutoPreload::ActiveRecord
28
+ # rubocop:disable Lint/SendWithMixinArgument
29
+ ActiveRecord::Base.send(:include, AutoPreload::ActiveRecord)
30
+ # rubocop:enable Lint/SendWithMixinArgument
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_preload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mònade
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-30 00:00:00.000000000 Z
11
+ date: 2022-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -64,6 +64,8 @@ files:
64
64
  - ".gitignore"
65
65
  - ".rspec"
66
66
  - ".rubocop.yml"
67
+ - ".solargraph.yml"
68
+ - CHANGELOG.md
67
69
  - Gemfile
68
70
  - LICENSE
69
71
  - README.md