activerecord-quick_read 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: 656180046eae2a146addc56cd7a34f30ee384b7b65e150b707c72367a9d0511b
4
+ data.tar.gz: 1707b51dc4c6d5907e52c7f3cb18e7d48d4880d1dec5aaa561fafc3eb7630310
5
+ SHA512:
6
+ metadata.gz: ef53068404c54d01b8e8663373c49fbd86c895c34394c766c4e309cb75ff11cd57dc339cc7a221bb28c222bfa9563a365cce4ff76287a578561e1f13cc2fcf5a
7
+ data.tar.gz: 330c61105dee608b320471810dbc62725ec7fc2e0cf6a71bb49d7be8cf1ce6098800c435212f66f60e772d5673bf72184152d6fe6e56eaca85f3ae72e16b85ff
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ quick_reads
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-21
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ryan.buckley@envoy.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
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 activerecord-quick_read.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-quick_read (0.1.0)
5
+ activerecord (>= 5, <= 8)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.0.4.3)
11
+ activesupport (= 7.0.4.3)
12
+ activerecord (7.0.4.3)
13
+ activemodel (= 7.0.4.3)
14
+ activesupport (= 7.0.4.3)
15
+ activesupport (7.0.4.3)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ concurrent-ruby (1.2.2)
22
+ diff-lcs (1.5.0)
23
+ i18n (1.12.0)
24
+ concurrent-ruby (~> 1.0)
25
+ json (2.6.3)
26
+ language_server-protocol (3.17.0.3)
27
+ minitest (5.18.0)
28
+ parallel (1.23.0)
29
+ parser (3.2.2.0)
30
+ ast (~> 2.4.1)
31
+ rainbow (3.1.1)
32
+ rake (13.0.6)
33
+ regexp_parser (2.8.0)
34
+ rexml (3.2.5)
35
+ rspec (3.12.0)
36
+ rspec-core (~> 3.12.0)
37
+ rspec-expectations (~> 3.12.0)
38
+ rspec-mocks (~> 3.12.0)
39
+ rspec-core (3.12.2)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-expectations (3.12.3)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-mocks (3.12.5)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.12.0)
47
+ rspec-support (3.12.0)
48
+ rubocop (1.50.2)
49
+ json (~> 2.3)
50
+ parallel (~> 1.10)
51
+ parser (>= 3.2.0.0)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ regexp_parser (>= 1.8, < 3.0)
54
+ rexml (>= 3.2.5, < 4.0)
55
+ rubocop-ast (>= 1.28.0, < 2.0)
56
+ ruby-progressbar (~> 1.7)
57
+ unicode-display_width (>= 2.4.0, < 3.0)
58
+ rubocop-ast (1.28.0)
59
+ parser (>= 3.2.1.0)
60
+ rubocop-performance (1.16.0)
61
+ rubocop (>= 1.7.0, < 2.0)
62
+ rubocop-ast (>= 0.4.0)
63
+ ruby-progressbar (1.13.0)
64
+ standard (1.27.0)
65
+ language_server-protocol (~> 3.17.0.2)
66
+ rubocop (~> 1.50.2)
67
+ rubocop-performance (~> 1.16.0)
68
+ tzinfo (2.0.6)
69
+ concurrent-ruby (~> 1.0)
70
+ unicode-display_width (2.4.2)
71
+
72
+ PLATFORMS
73
+ arm64-darwin-22
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ activerecord-quick_read!
78
+ rake (~> 13.0)
79
+ rspec (~> 3.0)
80
+ standard (~> 1.3)
81
+
82
+ BUNDLED WITH
83
+ 2.4.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Ryan Buckley
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,70 @@
1
+ # ActiveRecord::QuickRead
2
+
3
+ Makes rails go _faster_. Improve read times 4x!
4
+
5
+ ### How does it work?
6
+
7
+ Skips active record instantiation, query just the results and use them as plain hashes, converted
8
+ to structs with the same attributes as your model. The structs will gracefully upgrade to full
9
+ version of your model, in the event that you need to update them.
10
+
11
+ ## Installation
12
+
13
+
14
+ Install the gem and add to the application's Gemfile by executing:
15
+
16
+ $ bundle add activerecord-quick_read
17
+
18
+ If bundler is not being used to manage dependencies, install the gem by executing:
19
+
20
+ $ gem install activerecord-quick_read
21
+
22
+ ## Usage
23
+
24
+ Use `#quick_read` on your ActiveRecord relations (or `#quick_reads` for all scoped records)
25
+ Use `#quick_build` on your ActiveRecord models
26
+
27
+ First, add quickness to your model:
28
+ ```
29
+ class Report < ApplicationRecord
30
+ extend ActiveRecord::QuickRead
31
+ end
32
+ ```
33
+
34
+ Then you're ready to start going faster!
35
+
36
+ It's as simple as:
37
+
38
+ ```ruby
39
+ Report.where(id: params[:id]).quick_read
40
+ ```
41
+
42
+ Load your batches 4x quicker:
43
+ ```ruby
44
+ Report.all.in_batches { |batch| batch.quick_reads.map(&:to_h) }
45
+ ```
46
+
47
+ Gracefully upgrade to a full-fledged ActiveRecord object, and even issue `update` statements:
48
+ ```ruby
49
+ Report.all.quick_reads.each { |report| report.update(status: "done") if report.message = ?? }
50
+ ```
51
+
52
+ It just works.
53
+
54
+ ## Development
55
+
56
+ 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.
57
+
58
+ 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).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord-quick_read. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/activerecord-quick_read/blob/main/CODE_OF_CONDUCT.md).
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
67
+
68
+ ## Code of Conduct
69
+
70
+ Everyone interacting in the ActiveRecord::QuickRead project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/activerecord-quick_read/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/activerecord/quick_read/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activerecord-quick_read"
7
+ spec.version = ActiveRecord::QuickRead::VERSION
8
+ spec.authors = ["Ryan Buckley"]
9
+ spec.email = ["ridiculous@hey.com"]
10
+
11
+ spec.summary = "Makes rails go _faster_. Improve read times 4x!"
12
+ spec.description = "Makes rails go _faster_. Improve read times 4x!"
13
+ spec.homepage = "https://github.com/ridiculous/activerecord-quick_reads"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/ridiculous/activerecord-quick_reads"
21
+ spec.metadata["changelog_uri"] = "https://github.com/ridiculous/activerecord-quick_reads/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency "activerecord", ">= 5", "<= 8"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveRecord
2
+ module QuickRead
3
+ class Railtie < Rails::Railtie
4
+ config.after_initialize do
5
+ QuickRead.define_lite_structs
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module QuickRead
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+ require "active_record"
3
+
4
+ require_relative "quick_read/version"
5
+ require_relative "quick_read/railtie" if defined?(Rails)
6
+
7
+ module ActiveRecord
8
+ module QuickRead
9
+ # Defines a "Lite" class for the current model, in the same namespace, with it's columns defined as attributes
10
+ # Instances of the class are returned when calling #quick_read or #quick_reads on the models relation
11
+ # The class is optimized for faster read times, by using less memory and skipping AR initialization
12
+ # When the subject is asked to save or update, it'll gracefully upgrade to a **full working version**
13
+ # It does this by hooking into method missing and delegating to the (lazily loaded) underlying model
14
+
15
+ # Initialize the Lite models to be returned when using #quick_reads or called directly with #quick_build
16
+ #
17
+ # @param [ActiveRecord::Base] model - class to get Lite functionality
18
+ def self.extended(model)
19
+ super
20
+ return if !model.table_name || model.abstract_class
21
+
22
+ QuickRead.define_lite_struct(model)
23
+ end
24
+
25
+ def self.models
26
+ @models ||= []
27
+ end
28
+
29
+ def self.define_lite_structs
30
+ return if QuickRead.models.empty?
31
+
32
+ ActiveRecord::Base.logger.debug("QuickRead") { "Defining #{QuickRead.models.size} quick models" }
33
+ time = Benchmark.realtime do
34
+ while model = models.pop
35
+ define_lite_struct(model)
36
+ end
37
+ end
38
+ ActiveRecord::Base.logger.debug("QuickRead") { "Defining quick models took #{time.round(4)}s" }
39
+ end
40
+
41
+ def self.define_lite_struct(model)
42
+ model.const_set(:Lite, Struct.new(*model.column_names.map(&:to_sym)) { include(LiteBase) })
43
+ end
44
+
45
+ #
46
+ # !!! Extension for the model class
47
+ #
48
+
49
+ # This would be used when extended onto the ApplicationRecord, which many models inherit from
50
+ # Add the model to the queue to be run after initialization, since the models aren't fully defined yet
51
+ # wait until connections are available, after rails init
52
+ def inherited(model)
53
+ QuickRead.models << model
54
+ super
55
+ end
56
+
57
+ def quick_build(attrs = {})
58
+ klass = const_get(:Lite)
59
+ klass.new(*attrs.symbolize_keys!.values_at(*klass.members))
60
+ end
61
+
62
+ def quick_reads
63
+ connection.select_all(current_scope.to_sql).map { |attrs| quick_build(attrs) }
64
+ end
65
+
66
+ def quick_read
67
+ current_scope.limit(1).quick_reads.first
68
+ end
69
+
70
+ # Instantiate a new ActiveRecord object from a plain hash, marked as persisted, no changes, typecast
71
+ # @param [Hash] attributes
72
+ # @return ApplicationRecord subclass
73
+ def from_hash(attributes)
74
+ allocate.init_with_attributes(attributes_builder.build_from_database(attributes.stringify_keys!))
75
+ end
76
+
77
+ module LiteBase
78
+ def method_missing(name, *args, &block)
79
+ return super unless subject.respond_to?(name)
80
+
81
+ subject.send(name, *args, &block)
82
+ end
83
+
84
+ def respond_to_missing?(*args)
85
+ subject.respond_to?(*args) || super
86
+ end
87
+
88
+ def subject
89
+ @subject ||= model.from_hash(to_h)
90
+ end
91
+
92
+ # Since the struct that includes this module is defined within the model's namespace
93
+ def model
94
+ self.class.module_parent
95
+ end
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-quick_read
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Buckley
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ description: Makes rails go _faster_. Improve read times 4x!
34
+ email:
35
+ - ridiculous@hey.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".rspec"
41
+ - ".ruby-gemset"
42
+ - ".ruby-version"
43
+ - ".standard.yml"
44
+ - CHANGELOG.md
45
+ - CODE_OF_CONDUCT.md
46
+ - Gemfile
47
+ - Gemfile.lock
48
+ - LICENSE.txt
49
+ - README.md
50
+ - Rakefile
51
+ - activerecord-quick_read.gemspec
52
+ - lib/activerecord/quick_read.rb
53
+ - lib/activerecord/quick_read/railtie.rb
54
+ - lib/activerecord/quick_read/version.rb
55
+ homepage: https://github.com/ridiculous/activerecord-quick_reads
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ homepage_uri: https://github.com/ridiculous/activerecord-quick_reads
60
+ source_code_uri: https://github.com/ridiculous/activerecord-quick_reads
61
+ changelog_uri: https://github.com/ridiculous/activerecord-quick_reads/CHANGELOG.md
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.6.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.3.7
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Makes rails go _faster_. Improve read times 4x!
81
+ test_files: []