百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

hooks

> 编程语言
开源

针对 Ruby 的通用钩子与回调函数。

281 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

针对 Ruby 的通用钩子与回调函数。

Hooks

Generic hooks with callbacks for Ruby.

Introduction

Hooks lets you define hooks declaratively in your ruby class. You can add callbacks to your hook, which will be run as soon as you run the hook!

It's almost like ActiveSupport::Callbacks but 76,6% less complex. Instead, it is not more than a few lines of code, one method compilation, no method_missing and no magic.

Also, you may pass additional arguments to your callbacks when invoking a hook.

Example

Let's take... a cat.

ruby
require 'hooks'

class Cat
  include Hooks

  define_hooks :before_dinner, :after_dinner

Now you can add callbacks to your hook declaratively in your class.

ruby
  before_dinner :wash_paws

  after_dinner do
    puts "Ice cream for #{self}!"
  end

  after_dinner :have_a_dessert   # => refers to Cat#have_a_dessert

  def have_a_dessert
    puts "Hell, yeah!"
  end

This will run the block and #have_a_dessert from above.

ruby
cat.run_hook :after_dinner
# => Ice cream for #!
     Hell, yeah!

Callback blocks and methods will be executed with instance context. Note how self in the block refers to the Cat instance.

Inheritance

Hooks are inherited, here's a complete example to put it all together.

ruby
class Garfield  Ice cream for #!
     Hell, yeah!
     Is that all?

Note how the callbacks are invoked in the order they were inherited.

Options for Callbacks

You're free to pass any number of arguments to #run_callback, those will be passed to the callbacks.

ruby
cat.run_hook :before_dinner, cat, Time.now

The callbacks should be ready for receiving parameters.

ruby
before_dinner :wash_pawns
before_dinner do |who, when|
  ...
end

def wash_pawns(who, when)

Not sure why a cat should have ice cream for dinner. Beside that, I was tempted naming this gem hooker.

Running And Halting Hooks

Using #run_hook doesn't only run all callbacks for this hook but also returns an array of the results from each callback method or block.

ruby
class Garfield
  include Hooks
  define_hook :after_dark

  after_dark { "Chase mice" }
  after_dark { "Enjoy supper" }
end

Garfield.new.run_hook :after_dark
# => ["Chase mice", "Enjoy supper"]

This is handy if you need to collect data from your callbacks without having to access a global (brrr) variable.

With the :halts_on_falsey option you can halt the callback chain when a callback returns nil or false.

ruby
class Garfield
  include Hooks
  define_hook :after_dark, halts_on_falsey: true

  after_dark { "Chase mice" }
  after_dark { nil }
  after_dark { "Enjoy supper" }
end

result = Garfield.new.run_hook :after_dark
# => ["Chase mice"]

This will only run the first two callbacks. Note that the result doesn't contain the nil value. You even can check if the chain was halted.

ruby
result.halted? #=> true

Execution Scope

Normally, callbacks are executed in object context. You're free to provide your own context object using :scope.

ruby
define_hook :before_lunch, scope: lambda { |callback, scope| Logger }

This will evaluate the lambda in Logger class context.

Return nil to execute the lambda in the original context.

ruby
define_hook :before_lunch, scope: lambda { |*| nil }
what = "hands"
before_lunch  invoke "Chase mice" hook and #sleep

What happens is that the garfield object inherits existing hooks with all their callbacks from the Cat class, namely, this is :after_dark. It can then add local callbacks (or even more hooks) to itself without affecting the Cat class.

Naturally, adding new hooks works like-wise.

ruby
garfield.define_hook :before_six
garfield.before_six { .. }

This feature was added in 0.3.2.

Installation

In your Gemfile, do

ruby
gem "hooks"

Anybody using it?

  • Hooks is already used in Apotomo, a hot widget framework for Rails.
  • The datamappify gem uses hooks and the author Fred Wu contributed to this gem!

Similar libraries

  • http://github.com/nakajima/aspectory
  • http://github.com/auser/backcall
  • http://github.com/mmcgrana/simple_callbacks

License

Copyright (c) 2013-2015, Nick Sutterer

Released under the MIT License.

Issues· 5 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言