EmbedCallbacks
This gem makes it easy to create callbacks.
Installation
Add this line to your application's Gemfile:
gem 'simple_callbacker', github: 'hotoolong/embed_callbacks'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install specific_install
$ gem specific_install [email protected]:hotoolong/embed_callbacks.git main
Usage
embed_callback provides the following callbacks.
beforeis called the operation before the specified method.afteris called processing after the specified method.aroundis called processing before and after the specified method.rescueis called if the specified method produces an error.ensureis always called if the given method completes.
It also lets you decide whether or not to run the process according to the conditions. The conditions can be if and unless.
before example
require 'embed_callbacks'
class Sample
include EmbedCallbacks
set_callback :target, :before, :before_callback
def target
puts 'target'
end
def before_callback
puts 'before_callback'
end
end
sample = Sample.new
sample.target
#=> before_callback
#=> target
after example
require 'embed_callbacks'
class Sample
include EmbedCallbacks
set_callback :target, :after, :after_callback
def target
puts 'target'
end
def after_callback
puts 'after_callback'
end
end
sample = Sample.new
sample.target
#=> target
#=> after_callback
around example
require 'embed_callbacks'
class Sample
include EmbedCallbacks
set_callback :target, :around, :around_callback
def target
puts 'target'
end
def around_callback
puts 'around_callback'
end
end
sample = Sample.new
sample.target
#=> around_callback
#=> target
#=> around_callback
rescue example
require 'embed_callbacks'
class Sample
include EmbedCallbacks
set_callback :target, :rescue, :rescue_callback
def target
raise 'target'
end
def rescue_callback
puts 'rescue_callback'
end
end
sample = Sample.new
sample.target
#=> rescue_callback
#=> RuntimeError (target)
ensure example
require 'embed_callbacks'
class Sample
include EmbedCallbacks
set_callback :target, :ensure, :ensure_callback
def target
puts 'target'
end
def ensure_callback
puts 'ensure_callback'
end
end
sample = Sample.new
sample.target
#=> target
#=> ensure_callback
condition example
require 'embed_callbacks'
class Sample
include EmbedCallbacks
attr_accessor :check_flag
set_callback :target, :before, :before_callback, if: ->(record) { record.check_flag }
def target
puts 'target'
end
def before_callback
puts 'before_callback'
end
end
sample = Sample.new
sample.check_flag = false
sample.target
#=> target
sample.check_flag = true
sample.target
#=> before_callback
#=> target
Development
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.
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 tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/hotoolong/embed_callbacks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the EmbedCallbacks project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.