{"id":30741911,"url":"https://github.com/ceritium/hooki","last_synced_at":"2026-01-20T17:36:21.307Z","repository":{"id":149511150,"uuid":"621836606","full_name":"ceritium/hooki","owner":"ceritium","description":"Add before and after callbacks to methods.","archived":false,"fork":false,"pushed_at":"2025-07-08T21:12:45.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-24T19:29:36.056Z","etag":null,"topics":["after-method","before-method","callbacks"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ceritium.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-03-31T13:46:40.000Z","updated_at":"2025-07-08T21:12:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e14f077-dc4b-4f38-aacc-b00e3656dd68","html_url":"https://github.com/ceritium/hooki","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ceritium/hooki","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceritium%2Fhooki","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceritium%2Fhooki/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceritium%2Fhooki/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceritium%2Fhooki/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ceritium","download_url":"https://codeload.github.com/ceritium/hooki/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceritium%2Fhooki/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273535003,"owners_count":25122764,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["after-method","before-method","callbacks"],"created_at":"2025-09-04T01:10:14.723Z","updated_at":"2026-01-20T17:36:21.294Z","avatar_url":"https://github.com/ceritium.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hooki\n\nAdd before and after callbacks to methods.\n\n## Installation\n\nInstall the gem and add it to the application's Gemfile by executing:\n\n    $ bundle add hooki\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n\n    $ gem install hooki\n\n## Usage\n\nYou can use Hooki in several ways to add before and after callbacks to methods.\n\nHooki provides callbacks for instance and singleton methods:\n\ninstance: `before_method`, `after_method`\nsingleton: `before_singleton_method`, `after_singleton_method`\n\nAll the callbacks accept optional parameters `only` and `except` for filtering\nin which methods trigger the callbacks. `only` and `except` accepts a single symbol or array.\n\nThis is a basic example:\n\n```ruby\nclass Foo\n  include Hooki\n\n  before_method :log_before, only: :bar # or [:bar]\n  after_method :log_after, expect: :bar # or [:bar]\n\n  before_singleton_method :log_singleton_before, only: [:bar] # or :bar\n  after_singleton_method :log_singleton_after, expect: [:bar] # or :bar\n\n  def self.bar\n    puts \"singleton bar\"\n  end\n\n  def self.baz\n    puts \"singleton baz\"\n  end\n\n  def self.log_singleton_before(method_name)\n    puts \"-- log singleton before #{method_name}\"\n  end\n\n  def self.log_singleton_after(method_name)\n    puts \"-- log singleton after #{method_name}\"\n  end\n\n  def bar\n    puts \"bar\"\n  end\n\n  def baz\n    puts \"baz\"\n  end\n\n  private\n\n  def log_before(method_name)\n    puts \"-- log before #{method_name}\"\n  end\n\n  def log_after(method_name)\n    puts \"-- log after #{method_name}\"\n  end\nend\n```\n\nThe previous example doesn't seem too useful, Hooki unchains its potential when\nused on class inheritance or modules, for example:\n\n```ruby\nmodule Logger\n  include Hooki\n\n  before_method :log\n\n  private\n\n  def log(method_name)\n    puts \"running #{method_name}\"\n  end\nend\n\nclass Foo\n  include Logger\n\n  def bar\n    puts \"bar\"\n  end\n\n  def baz\n    puts \"baz\"\n  end\nend\n```\n\nThere are more examples on [`test/examples.rb`](test/examples.rb)\n\n## Performance\n\nGood, but how slow is it? Well, this is a lot of metaprograming, so it will be\nslower than the traditional approach.\n\nThere are two benchmarks in [`benchmarks/`](benchmarks/) folder.\n\nThe results on my machine are the following:\n\n```\n$ bundle exec ruby benchmarks/no_job.rb\nWarming up --------------------------------------\n                with    58.373k i/100ms\n             without   377.367k i/100ms\nCalculating -------------------------------------\n                with    387.588k (±20.7%) i/s -      1.868M in   5.058400s\n             without      3.624M (±19.7%) i/s -     17.359M in   5.025660s\n\nComparison:\n             without:  3623753.6 i/s\n                with:   387587.8 i/s - 9.35x  slower\n\n\n$ bundle exec ruby benchmarks/with_job.rb\nWarming up --------------------------------------\n                with    11.220k i/100ms\n             without    14.801k i/100ms\nCalculating -------------------------------------\n                with    112.751k (± 8.7%) i/s -    561.000k in   5.025158s\n             without    143.033k (±11.0%) i/s -    710.448k in   5.052800s\n\nComparison:\n             without:   143033.0 i/s\n                with:   112751.4 i/s - 1.27x  slower\n```\n\nThe \"no job\" benchmark is ~9 times slower, but it is doing nothing, it doesn't seem\na realistic scenario. The \"with job\" benchmark is only a bit slower.\n\n## Development\n\nAfter 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.\n\nTo 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).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/ceritium/hooki. 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/ceritium/hooki/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Hooki project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ceritium/hooki/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceritium%2Fhooki","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceritium%2Fhooki","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceritium%2Fhooki/lists"}