Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jcambass/sane_patch
Making monkey patches sane again
https://github.com/jcambass/sane_patch
monkey-patching patch ruby rubygems
Last synced: 18 days ago
JSON representation
Making monkey patches sane again
- Host: GitHub
- URL: https://github.com/jcambass/sane_patch
- Owner: Jcambass
- License: mit
- Created: 2019-05-23T08:22:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-18T12:53:22.000Z (about 3 years ago)
- Last Synced: 2024-10-18T22:11:14.636Z (28 days ago)
- Topics: monkey-patching, patch, ruby, rubygems
- Language: Ruby
- Homepage: https://joel.am/blog/introducing-sane-patch
- Size: 795 KB
- Stars: 65
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# SanePatch [![Gem Version](https://badge.fury.io/rb/sane_patch.svg)](https://badge.fury.io/rb/sane_patch) ![Gem Downloads](https://img.shields.io/gem/dt/sane_patch) [![Build Status](https://travis-ci.com/Jcambass/sane_patch.svg?branch=master)](https://travis-ci.com/Jcambass/sane_patch)
SanePatch is a simple and non intrusive helper that aims to make monkey patching a little bit safer.
It achieves this by only applying your patches to a specific version of a gem and raising a exception if the gem version changed. This means that you will always double check that your patches still work after upgrading gems. No surprises anymore!
## But wait.. Isn't monkey patching bad?
As with many things in life there is no pure good or bad. Monkey patching can be dangerous in certain situations and should be avoided sometimes but there are reasons to use it.
Good reasons to monkey patch a gem could be:
- Fixing a bug in a broken gem until a new version of it is released.
- Performance optimizing a specific method of a gem that is used in a hot code path.## Installation
Add this line to your application's Gemfile:
```ruby
gem 'sane_patch', '~> 1.0'
```And then execute:
$ bundle
## Usage
The usage of SanePatch is straight forward:
```ruby
SanePatch.patch('', '') do
# Apply your patches here the same way as usual.
end
```A more specific example:
```ruby
Greeter.greet # => 'Hello'# Let's patch the `Greeter.greet` method to output 'Hello Folks'
module GreeterPatch
def greet
"#{super} Folks"
end
end# We currently have version 1.1.0 of the greeter gem
SanePatch.patch('greeter', '1.1.0') do
Greeter.prepend(GreeterPatch)
endGreeter.greet # => 'Hello Folks'
```If somebody updates the gem version the patch will raise as soon as its code path is executed:
```
SanePatch::Errors::IncompatibleVersion:
It looks like the greeter gem was upgraded.
There are patches in place that need to be verified.
Make sure that the patch at initializers/greeter_patch.rb:8 is still needed and working.
```Setting the `raise_error` keyword argument to `false` will skip the execution of the block but will not raise an error. (The default value for the keyword is `true`.)
### Complex version constraints
SanePatch supports all [version constraints](http://docs.seattlerb.org/rubygems/Gem/Requirement.html) you know and love from RubyGems.
```ruby
SanePatch.patch('greeter', '~> 1.1.0') { # your patch }
SanePatch.patch('greeter', '> 1.1.0') { # your patch }
SanePatch.patch('greeter', '< 1.1.0') { # your patch }
```It even supports version ranges based on multiple constraints:
```ruby
SanePatch.patch('greeter', '> 1.0.0', '< 1.1.0') { # your patch }
```This is especially useful if you patch a bug where you know the affected gem versions.
### Providing additional information
If you patch a known bug in a gem it might be useful to provide additional information why the patch is needed and when it can be removed.
```ruby
Greeter.silence # => nilmodule GreeterPatch
def silence
''
end
enddetails = <<-MSG
The `silence` method should output an empty string rather than nil.
This is a known issue and will be fixed in the next release.
See: https://github.com/Jcambass/greeter/issues/45
MSGSanePatch.patch('greeter', '1.1.0', details: details) do
Greeter.prepend(GreeterPatch)
endGreeter.silence # => ''
```The additionally provided details will also show up in the exception message.
```
SanePatch::Errors::IncompatibleVersion:
It looks like the greeter gem was upgraded.
There are patches in place that need to be verified.
Make sure that the patch at initializers/greeter_patch.rb:8 is still needed and working.
Details:
The `silence` method should output an empty string rather than nil.
This is a known issue and will be fixed in the next release.
See: https://github.com/Jcambass/greeter/issues/45
```### Logging support
The `logger` keyword argument can be used to supply a logger instance. SanePatch will pass the exception message to that object's `#warn` method when the version constraint is not satisfied:
```ruby
SanePatch.patch('greeter', '1.1.0', logger: Logger.new(STDOUT)) { # your patch }
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Jcambass/sane_patch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the SanePatch project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Jcambass/sane_patch/blob/master/CODE_OF_CONDUCT.md).