Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kanety/ii_callback
https://github.com/kanety/ii_callback
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kanety/ii_callback
- Owner: kanety
- License: mit
- Created: 2022-04-16T01:06:24.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-04-16T02:43:26.000Z (almost 3 years ago)
- Last Synced: 2024-12-25T04:04:42.958Z (23 days ago)
- Language: Ruby
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# IICallback
A callback class for activerecord.
## Dependencies
* ruby 2.3+
* activesupport 5.0+
* activerecord 5.0+## Installation
Add this line to your application's Gemfile:
```ruby
gem 'ii_callback'
```Then execute:
$ bundle
## Usage
Prepare callback:
```ruby
class SaveCallback < IICallback::Base
def after_save
puts 'called after save'
end
end
```Prepare model:
```ruby
class Item < ActiveRecord::Base
after_save SaveCallback
end
```Use as follows:
```ruby
Item.new.save
#=> called after save
```### Basis
A callback class is based on activerecord's one, but some points are different:
* A callback method like `after_save` takes no arguments. Instead of them, you can access activerecord instance by `@record` attribute.
* You can use only instance method as a callback method. Class method like `self.after_save` is used internally in `IICallback::Base`, so you can not define it by yourself.A callback class has following attributes:
```ruby
class SaveCallback < IICallback::Base
def after_save
puts "record: #{@record}"
puts "method: #{@method}"
end
endItem.new.save
#=> record: #
# method: after_save
```#### Coactors
You can chain multiple callback classes by using `coact`. For example:
```ruby
class Save1Callback < IICallback::Base
def after_save
puts "called after save 1"
end
endclass Save2Callback < IICallback::Base
def after_save
puts "called after save 2"
end
endclass SaveCallback < IICallback::Base
coact Save1Callback, Save2Callback
endItem.new.save
#=> called after save 1
# called after save 2
```See [coactive](https://github.com/kanety/coactive) for more `coact` examples:
#### Callback hooks
Following hooks are available in callback class:
* before_all, around_all, after_all
* before_call, around_call, after_call*_all wraps all coactors, and *_call wraps callback method. That is, before_all is called before running all coactors, and before_call is called before running callback method. For example:
```ruby
class SaveCallback < IICallback::Base
before_call :set_variabledef set_variable
@variable = "some value"
enddef after_save
puts @variable
end
endItem.new.save
#=> some value
```### Logging
Callback supports instrumentation hook supplied by `ActiveSupport::Notifications`.
You can enable log subscriber as follows:```ruby
IICallback::LogSubscriber.attach_to :ii_callback
```This subscriber will write logs in debug mode as the following example:
```
Calling ItemCallback with # method=before_save>
...
Called ItemCallback (Duration: 9.9ms, Allocations: 915)
```## Contributing
Bug reports and pull requests are welcome at https://github.com/kanety/ii_callback.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).