pire_loader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e69575b7173c853934208c31e7f8939fbf87484cd3b0ca449fd1c00ffda3d196
4
+ data.tar.gz: 94f13a68d718f6ed86096b6b672625e55d25f687c5d74d070d6af80c2515b4e7
5
+ SHA512:
6
+ metadata.gz: d3be086dd43f1612a919e5acbc887a8d5802cfdc2cea5765e1cdae5319c7e9341e804f467d13fce96f84901bd7fa7958af73f849c72f7676d512fd1d726fda15
7
+ data.tar.gz: 33eff3ccee66ed7832bf709fbe66d87c78cb8dffbe8f6521ea54079ca1c006f24dd0c1b5f7fd6b00e8307b32eab04c1e9e4fb0b96794c7392f53c4c811693996
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pire_loader.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pire_loader (0.1.0)
5
+ activerecord
6
+ activesupport
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (7.0.1)
12
+ activesupport (= 7.0.1)
13
+ activerecord (7.0.1)
14
+ activemodel (= 7.0.1)
15
+ activesupport (= 7.0.1)
16
+ activesupport (7.0.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ ast (2.4.2)
22
+ concurrent-ruby (1.1.9)
23
+ diff-lcs (1.5.0)
24
+ i18n (1.8.11)
25
+ concurrent-ruby (~> 1.0)
26
+ minitest (5.15.0)
27
+ parallel (1.21.0)
28
+ parser (3.1.0.0)
29
+ ast (~> 2.4.1)
30
+ rainbow (3.1.1)
31
+ rake (13.0.6)
32
+ regexp_parser (2.2.0)
33
+ rexml (3.2.5)
34
+ rspec (3.10.0)
35
+ rspec-core (~> 3.10.0)
36
+ rspec-expectations (~> 3.10.0)
37
+ rspec-mocks (~> 3.10.0)
38
+ rspec-core (3.10.1)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-expectations (3.10.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-mocks (3.10.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-support (3.10.3)
47
+ rubocop (1.25.0)
48
+ parallel (~> 1.10)
49
+ parser (>= 3.1.0.0)
50
+ rainbow (>= 2.2.2, < 4.0)
51
+ regexp_parser (>= 1.8, < 3.0)
52
+ rexml
53
+ rubocop-ast (>= 1.15.1, < 2.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 1.4.0, < 3.0)
56
+ rubocop-ast (1.15.1)
57
+ parser (>= 3.0.1.1)
58
+ ruby-progressbar (1.11.0)
59
+ tzinfo (2.0.4)
60
+ concurrent-ruby (~> 1.0)
61
+ unicode-display_width (2.1.0)
62
+
63
+ PLATFORMS
64
+ x86_64-darwin-19
65
+
66
+ DEPENDENCIES
67
+ pire_loader!
68
+ rake (~> 13.0)
69
+ rspec (~> 3.0)
70
+ rubocop (~> 1.21)
71
+
72
+ BUNDLED WITH
73
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Mehmet Emin INAC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # PireLoader
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pire_loader`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pire_loader'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pire_loader
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pire_loader.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/delegation"
4
+
5
+ module PireLoader
6
+ module Loaders
7
+ class Singular
8
+ def self.preload(association)
9
+ new(association).preload
10
+ end
11
+
12
+ def initialize(association)
13
+ @association = association
14
+ end
15
+
16
+ def preload
17
+ owner_records.each do |record|
18
+ join_value = record[join_foreign_key]
19
+
20
+ record.association(reflection.name).target = associated_records[join_value]
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :association
27
+
28
+ delegate :reflection, to: :association, private: true
29
+ delegate :foreign_key, :join_foreign_key, to: :reflection, private: true
30
+
31
+ def associated_records
32
+ @associated_records ||= association.scope
33
+ .except(:limit)
34
+ .rewhere(foreign_key => join_values)
35
+ .to_a
36
+ .index_by(&foreign_key.to_sym)
37
+ end
38
+
39
+ def owner_records
40
+ @owner_records ||= association.owner.parent_relation.records
41
+ end
42
+
43
+ def join_values
44
+ owner_records.map { |record| record[join_foreign_key] }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PireLoader
4
+ module Patches
5
+ module Association
6
+ def lazy_load?
7
+ owner.lazy_load?(reflection.name)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ ActiveRecord::Associations::Association.prepend(PireLoader::Patches::Association)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PireLoader
4
+ module Patches
5
+ module Associations
6
+ module SingularAssociation
7
+ def find_target
8
+ return super unless lazy_load?
9
+
10
+ PireLoader::Loaders::Singular.preload(self) && target
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ ActiveRecord::Associations::SingularAssociation.prepend(PireLoader::Patches::Associations::SingularAssociation)
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PireLoader
4
+ module Patches
5
+ module Base
6
+ def self.prepended(base)
7
+ base.attr_accessor :parent_relation
8
+ end
9
+
10
+ def lazy_load?(association)
11
+ return unless parent_relation
12
+
13
+ parent_relation.lazy_load_values.include?(association)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveRecord::Base.prepend(PireLoader::Patches::Base)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PireLoader
4
+ module Patches
5
+ module Querying
6
+ delegate :lazy_load, :lazy_load!, to: :all
7
+ end
8
+ end
9
+ end
10
+
11
+ ActiveRecord::Querying.prepend(PireLoader::Patches::Querying)
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PireLoader
4
+ module Patches
5
+ module Relation
6
+ #######################
7
+ # Lazy load interface #
8
+ #######################
9
+ def lazy_load(*args)
10
+ spawn.lazy_load!(*args)
11
+ end
12
+
13
+ def lazy_load!(*args) # :nodoc:
14
+ # args.compact_blank!
15
+ args.flatten!
16
+
17
+ self.lazy_load_values |= args
18
+ self
19
+ end
20
+
21
+ def lazy_load_values
22
+ @values.fetch(:lazy_load, [])
23
+ end
24
+
25
+ def lazy_load_values=(value)
26
+ assert_mutability!
27
+ @values[:lazy_load] = value
28
+ end
29
+ #######################
30
+ # Lazy load interface #
31
+ #######################
32
+
33
+ ###########################
34
+ # Parent relation methods #
35
+ ###########################
36
+ def parent_relation
37
+ @values.fetch(:parent_relation, nil)
38
+ end
39
+
40
+ def parent_relation=(value)
41
+ @values[:parent_relation] = value
42
+ end
43
+ ###########################
44
+ # Parent relation methods #
45
+ ###########################
46
+
47
+ ###########################
48
+ # After load methods #
49
+ ###########################
50
+ def after_load_callback
51
+ @values.fetch(:after_load, nil)
52
+ end
53
+
54
+ def after_load(&blk)
55
+ @values[:after_load] = blk
56
+ end
57
+ ###########################
58
+ # After load methods #
59
+ ###########################
60
+
61
+ ### Loading
62
+ def load(*)
63
+ super.tap do
64
+ inject_self
65
+ run_after_load
66
+ end
67
+ end
68
+
69
+ def inject_self
70
+ @records.each { |record| record.parent_relation = self } if lazy_load_values.present?
71
+ end
72
+
73
+ # This can also modify the @records if the callback
74
+ # returns a new collection!
75
+ def run_after_load
76
+ return unless after_load_callback
77
+
78
+ new_records = after_load_callback.call(@records)
79
+ @records = new_records.freeze if new_records.present?
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ ActiveRecord::Relation.prepend(PireLoader::Patches::Relation)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PireLoader
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ require_relative "pire_loader/version"
6
+ require_relative "pire_loader/loaders/singular"
7
+ require_relative "pire_loader/patches/association"
8
+ require_relative "pire_loader/patches/querying"
9
+ require_relative "pire_loader/patches/relation"
10
+ require_relative "pire_loader/patches/associations/singular_association"
11
+ require_relative "pire_loader/patches/base"
12
+
13
+ module PireLoader
14
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pire_loader/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pire_loader"
7
+ spec.version = PireLoader::VERSION
8
+ spec.authors = ["Mehmet Emin INAC"]
9
+ spec.email = ["mehmetemininac@gmail.com"]
10
+
11
+ spec.summary = "Lazy and Async preloading for ActiveRecord associations"
12
+ spec.homepage = "https://github.com/meinac/pire_loader"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ spec.add_dependency "activesupport"
31
+ spec.add_dependency "activerecord"
32
+
33
+ # For more information and examples about making a new gem, check out our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pire_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mehmet Emin INAC
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - mehmetemininac@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/pire_loader.rb
56
+ - lib/pire_loader/loaders/singular.rb
57
+ - lib/pire_loader/patches/association.rb
58
+ - lib/pire_loader/patches/associations/singular_association.rb
59
+ - lib/pire_loader/patches/base.rb
60
+ - lib/pire_loader/patches/querying.rb
61
+ - lib/pire_loader/patches/relation.rb
62
+ - lib/pire_loader/version.rb
63
+ - pire_loader.gemspec
64
+ homepage: https://github.com/meinac/pire_loader
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://github.com/meinac/pire_loader
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.6.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.1.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Lazy and Async preloading for ActiveRecord associations
88
+ test_files: []