Project

garfio

0.01
No commit activity in last 3 years
No release in over 3 years
With few lines of code, one method compilation and no method missing Garfio with an easy way lets you to launch callbacks before and after of the methods of your objects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 1.1.3
 Project Readme

Garfio

Garfio helps you to build hooks in your ruby objects, is minimalist and framework-agnostic.

Introduction

With few lines of code, one method compilation, no monkey patching and no method missing Garfio lets you launch callbacks before and after of your methods as soon as you run them.

Installation

Installing Garfio is as simple as running:

$ gem install garfio

Include Garfio in your Gemfile with gem 'garfio' or require it with require 'garfio'.

Usage

Once you have extend Garfio, you can define your before and after callbacks in the set_hook method, your callback recognize the method sending his name as symbol

class User
  extend Garfio

  def send_greeting
    puts "preparing the welcome message"
  end

  def register_user
    puts "registering user"
  end

  def send_mailer
    puts "sending to your email"
  end

  set_hook :register_user do
    before :send_greeting
    after :send_mailer
  end
end

User.new.register_user
#=> preparing the welcome message
#=> registering user
#=> sending to your email

Is not neccesary indicate both callbacks, you can work with just before or after

  set_hook :register_user do
    before :send_greeting
  end

Also you can send blocks to your callbacks

set_hook :register_user do
  before { puts "hello user!" }
  after { puts "Ok, bye" }
end

User.new.register_user
#=> hello user!
#=> registering user
#=> Ok, bye

And you can take advantage of them to do more complex stuff

set_hook :register_user do
  after do
    update_status_with("activate")
    send_mailer
    notify_friends
  end
end