flickwerk 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: 8e651efd8959c74f5444b298eee3a2cd6025be555990fab13877af551bfb23c3
4
+ data.tar.gz: a204baaa6ca697496c95cb17afa877c1be9c68f2e7bd8160ee68c94ddd41bfb3
5
+ SHA512:
6
+ metadata.gz: e05aeceb1d30f4763a4f0fe31d064c3eed9251a51da3a8498ec2cdb92d1ebd658017ea39204b7553c2734d8f254b9a9fed19703e9c309b508e2bfc930789e0ba
7
+ data.tar.gz: c960be6af80cf1d42b710c20c3815284197b93fad2ec1a688730dd2f058f5386c6d2416f4ba5a4ec45e6203538be2cfbcb8eb2d7defede7ea4c290ec163be5e9
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-11-27
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Martin Meyerhoff
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,206 @@
1
+ # Flickwerk
2
+
3
+ A Railtie for loading autoloadable patches using Zeitwerk. This speeds up Rails bootup and reload time considerable, because other solutions out there load the patches and the associated classes on application bootup, and this gem defers loading the patches until the patched classes are autoloaded.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bundle add flickwerk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Include Flickwerk in your `application.rb` or `engine.rb` file, depending on whether you want to add patches to external libraries from an engine or from your host app.
14
+
15
+ ```rb
16
+ # config/application.rb
17
+ # ...
18
+ class MyApplication < Rails::Application
19
+ include Flickwerk
20
+
21
+ # ...
22
+ end
23
+ ```
24
+
25
+ Then, add patches to `app/patches/models` or `app/patches/controllers` that can be autoloaded.
26
+
27
+ ```rb
28
+ # app/patches/models/user_patch.rb
29
+ module UserPatch
30
+ def name
31
+ "Engelbert Humperdinck"
32
+ end
33
+
34
+ User.prepend self
35
+ end
36
+ ```
37
+
38
+ Now, all your users will have Engelbert Humperdinck's name.
39
+
40
+ You can use Ruby's `prepended` hook in order to run class methods. That's useful for adding scopes or ActiveRecord relationships.
41
+
42
+ ```rb
43
+ # app/patches/models/spree_order_patch.rb
44
+
45
+ module SpreeOrderPatch
46
+ def self.prepended(base)
47
+ base.has_many :goodies
48
+ end
49
+
50
+ Spree::Order.prepend self
51
+ end
52
+ ```
53
+
54
+ If you application now has a class `Goodie` with a foreign key to the `spree_orders` table, you can check on those goodies using the familiar `order.goodies` in your application.
55
+
56
+
57
+ ### Using Flickwerk in Engines
58
+
59
+ You can also include Flickwerk in your `engine.rb` file and add patches to your gem.
60
+
61
+ ```rb
62
+ # lib/my_gem/engine.rb
63
+
64
+ class MyGem::Engine < Rails::Engine
65
+ include Flickwerk
66
+ # ...
67
+ end
68
+ ```
69
+
70
+ It's advisable to namespace your patches so they do not conflict with patches added elsewhere:
71
+
72
+ ```rb
73
+ # app/patches/modes/my_engine/spree_order_patch.rb
74
+ module MyEngine::SpreeOrderPatch
75
+ def self.prepended(base)
76
+ base.scope :with_goodies, -> { joins(:goodies).where.not(goodie_id: nil) }
77
+ end
78
+
79
+ Spree::Order.prepend self
80
+ end
81
+ ```
82
+
83
+ ### Using `super`
84
+
85
+ Sometimes you want to modify the original result of a method dynamically rather than just overwrite it. Because your patches use `Module.prepend`, this can be easily accomplished, even if you want to modify class methods:
86
+
87
+ ```rb
88
+ # app/models/spree_order_patch.rb
89
+
90
+ module SpreeOrderPatch
91
+ module ClassMethods
92
+ def ransackable_associations
93
+ super + ["goodies"]
94
+ end
95
+ end
96
+
97
+ def self.prepended(base)
98
+ base.extend ClassMethods
99
+ end
100
+
101
+ def total
102
+ super - goodies.sum(&:amount)
103
+ end
104
+
105
+ Spree::Order.prepend self
106
+ end
107
+ ```
108
+
109
+ ### Manual patches
110
+
111
+ Flickwerk will look for the string `.prepend` in your patches in order to know which class should be patched. In some circumstances, the name of the class being patched is the result of a method call though, as in the case of patching Solidus' configurable user class. In these cases, you'll have to register the patch with Flickwerk manually:
112
+
113
+ ```rb
114
+ # app/patches/models/my_gem/user_class_patch.rb
115
+
116
+ module MyGem::UserClassPatch
117
+ def name
118
+ "Dr. #{super}"
119
+ end
120
+ end
121
+ ```
122
+
123
+ ```rb
124
+ # lib/my_gem/engine.rb
125
+
126
+ require "flickwerk"
127
+
128
+ class MyGem::Engine < Rails::Engine
129
+ include Flickwerk
130
+
131
+ initializer "my_gem.patch_user_class", before: "flickwerk.add_patches" do
132
+ Flickwerk.patch(Spree.class_variable_get(:@@user_class), "MyGem::UserClassPatch"
133
+ end
134
+ end
135
+ ```
136
+
137
+
138
+ ### Patching non-autoloadable classes
139
+
140
+ Classes that are not autoloaded (very generally, that's everything in the `lib` folder) cannot be loaded using the mechanism Flickwerk provides. Put any patches you have for these into your own `lib` folder and `require` them at your discretion:
141
+
142
+ ```rb
143
+ # lib/my_gem/spree_patch.rb
144
+
145
+ module MyGem::SpreePatch
146
+ module ClassMethods
147
+ def river_name
148
+ "Havel"
149
+ end
150
+ end
151
+
152
+ def self.prepended(base)
153
+ base.extend ClassMethods
154
+ end
155
+
156
+ Spree.prepend self
157
+ end
158
+ ```
159
+
160
+ Require these in a config.to_prepare block in your application
161
+
162
+ ```rb
163
+ # lib/my_gem/engine.rb
164
+
165
+ class MyGem::Engine
166
+ # ...
167
+ initializer "load_lib_patches" do
168
+ config.to_prepare do
169
+ root.join("lib/patches").glob("**/*.rb").each do |f|
170
+ require f
171
+ end
172
+ end
173
+ end
174
+ # ...
175
+ end
176
+ ```
177
+
178
+ ## Considerations
179
+
180
+ ### Aren't patches bad?
181
+
182
+ Patches are often not the ideal solution. It's often preferable to fix any bugs upstream, or to use a specialized object just in those contexts where necessary. However, in some ecosystems, like the Spree/Solidus ones, core classes are extended in extension gems, and especially things like adding associations can be impossible on anything than the correct ActiveRecord class.
183
+
184
+ Choose your patches carefully, and always look for better solutions than a patch. Sometimes, they're a good, pragmatic solution though.
185
+
186
+ ### Why don't you call these monkey patches?
187
+
188
+ Because there are no monkeys involved in writing them - to my knowledge, this is all the work of human beings.
189
+
190
+ ## Development
191
+
192
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
193
+
194
+ 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).
195
+
196
+ ## Contributing
197
+
198
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flickwerk. 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]/flickwerk/blob/main/CODE_OF_CONDUCT.md).
199
+
200
+ ## License
201
+
202
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
203
+
204
+ ## Code of Conduct
205
+
206
+ Everyone interacting in the Flickwerk project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/flickwerk/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 "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flickwerk
4
+ class PatchFinder
5
+ DECORATED_CLASS_PATTERN = /(?:::)?(?<decorated_class>[\w.:]+)\.prepend[\s(]/
6
+
7
+ attr_reader :path, :autoloader
8
+
9
+ def initialize(path, autoloader: Rails.autoloaders.main)
10
+ @autoloader = autoloader
11
+ @path = path
12
+ end
13
+
14
+ def call
15
+ path.glob("**/*_patch.rb") do |patch_path|
16
+ # Match all the classes that are prepended in the file
17
+ matches = File.read(patch_path).scan(DECORATED_CLASS_PATTERN).flatten
18
+
19
+ # Don't do a thing if there's no prepending.
20
+ raise Flickwerk::Error, "No classes prepended in #{patch_path}" if matches.empty?
21
+
22
+ # For each unique match, make sure we load the decorator when the base class is loaded
23
+ matches.uniq.each do |decorated_class|
24
+ # Zeitwerk tells us which constant it expects a file to provide.
25
+ decorator_constant = autoloader.cpath_expected_at(patch_path)
26
+ Flickwerk.patch(decorated_class, with: decorator_constant)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flickwerk
4
+ class PatchLoader
5
+ def self.call(autoloader: Rails.autoloaders.main)
6
+ Flickwerk.patches.each do |class_name, decorators|
7
+ autoloader.on_load(class_name) do
8
+ decorators.each(&:constantize)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flickwerk/patch_finder"
4
+ require "flickwerk/patch_loader"
5
+
6
+ class Flickwerk::Railtie < Rails::Railtie
7
+ initializer "flickwerk.add_paths", before: :set_autoload_paths do |app|
8
+ Flickwerk.patch_paths.each do |path|
9
+ app.config.autoload_paths << path
10
+ end
11
+ end
12
+
13
+ initializer "flickwerk.add_patches", after: :setup_main_autoloader do
14
+ Flickwerk.patch_paths.each do |path|
15
+ Flickwerk::PatchFinder.new(path).call
16
+ end
17
+ Flickwerk::PatchLoader.call
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flickwerk
4
+ VERSION = "0.1.0"
5
+ end
data/lib/flickwerk.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "flickwerk/version"
4
+ require "active_support/core_ext/module/attribute_accessors"
5
+ require "flickwerk/railtie" if defined?(Rails)
6
+
7
+ module Flickwerk
8
+ class Error < StandardError; end
9
+
10
+ mattr_accessor :patch_paths, default: []
11
+ mattr_accessor :patches, default: Hash.new([])
12
+
13
+ def self.included(engine)
14
+ engine.root.glob("app/patches/*").each do |path|
15
+ engine.patch_paths << path
16
+ end
17
+ end
18
+
19
+ def self.patch(class_name, with:)
20
+ patches[class_name] += [with]
21
+ end
22
+ end
data/sig/flickwerk.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Flickwerk
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flickwerk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Meyerhoff
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description: Support for lazy loading patches in Rails engines
28
+ email:
29
+ - mamhoff@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".standard.yml"
35
+ - CHANGELOG.md
36
+ - CODE_OF_CONDUCT.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/flickwerk.rb
41
+ - lib/flickwerk/patch_finder.rb
42
+ - lib/flickwerk/patch_loader.rb
43
+ - lib/flickwerk/railtie.rb
44
+ - lib/flickwerk/version.rb
45
+ - sig/flickwerk.rbs
46
+ homepage: https://github.com/friendlycart/flickwerk
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/friendlycart/flickwerk
51
+ source_code_uri: https://github.com/friendlycart/flickwerk
52
+ changelog_uri: https://github.com/friendlycart/flickwerk/blob/main/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.4.19
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Support for lazy loading patches in Rails engines
72
+ test_files: []