Code Monkey home page Code Monkey logo

easy_callbacks's Introduction

Easy Callbacks

Provides callback support for Ruby classes.

Example

For a given class:

class SomeClass

  def some_method(some_arg)
    puts "some_method: #{some_arg}"
  end

  def before_some_method(some_arg)
    puts "before_some_method: #{some_arg}"
  end

  def around_some_method(some_arg)
    puts "around_some_method: #{some_arg}"
  end

end

EasyCallbacks.configure(SomeClass) do

  # named callback
  before :some_method, :before_some_method

  # named callback
  around :some_method, :around_some_method

  # anonymous callback
  after  :some_method do
    puts "after_some_method: #{call_args[0]}"
  end

end

This call:

SomeClass.new.some_method :some_arg

Produces this output:

before_some_method: hue
some_method: hue
around_some_method: hue
after_some_method: hue

Helper Methods

Inside a callback block/method, you can call three helper methods:

call_args        # returns original arguments given to the call
call_block       # returns original block given to the call
callback_details # returns details of callback execution

Callback Details

The ‘callback_details` helper method will return:

{
    target_class: SomeClass,
    callback_type: :around,
    target_method_name: :some_method,
    callback_method_name: :around_some_method,
    callback_block: nil,
    return_type: :success, # exclusive for `around` callbacks
    return_object: nil     # exclusive for `around` callbacks
}

Module inclusion

You can call ‘before`, `around` and `after` methods directly instead of calling them inside of `EasyCallbacks.configure` block. Just include `EasyCallbacks` module:

class SomeClass

  def some_method(some_arg)
    puts "some_method: #{some_arg}"
  end

  def before_some_method(some_arg)
    puts "before_some_method: #{some_arg}"
  end

  def around_some_method(some_arg)
    puts "around_some_method: #{some_arg}"
  end

  include EasyCallbacks

  before :some_method, :before_some_method

  around :some_method, :around_some_method

  after  :some_method do
    puts "after_some_method: #{call_args[0]}"
  end

end

Singleton Classes Support

It works too:

class SomeClass

  class << self

    def some_method(some_arg)
      puts "some_method: #{some_arg}"
    end

    def before_some_method(some_arg)
      puts "before_some_method: #{some_arg}"
    end

    def around_some_method(some_arg)
      puts "around_some_method: #{some_arg}"
    end

    include EasyCallbacks

    before :some_method, :before_some_method

    around :some_method, :around_some_method

    after  :some_method do
      puts "after_some_method: #{call_args[0]}"
    end

  end

end

SomeClass.some_method :hue

More

To see more of ‘EasyCallbacks` usages, please take a look at DummyClass.

<img src=“https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif” alt=“Donate” />

easy_callbacks's People

Contributors

r4z3c avatar

Stargazers

João Pedro Pinheiro avatar Danillo Pereira avatar Gabriel Seixas avatar Pedro Henrique Pereira de Jesus avatar

Watchers

Pedro Henrique Pereira de Jesus avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.