Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/adrianmarino/ruby-method-wrapper

Allow intercept method invocations. Useful for log, cache, etc...
https://github.com/adrianmarino/ruby-method-wrapper

Last synced: 1 day ago
JSON representation

Allow intercept method invocations. Useful for log, cache, etc...

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/adrianmarino/ruml.svg?branch=master)](https://travis-ci.org/adrianmarino/ruby_method_wrapper)
[![License](http://img.shields.io/:license-mit-blue.svg)](http://badges.mit-license.org)

# ruby-method-wrapper

Allow intercept method invocations. Useful for log, cache, etc...

## Usage

Step 1: Include in your Gemfile.
```ruby
gem 'ruby_method_wrapper'
```

Step 2: Write an example.

```ruby
require 'bundler/setup'
require 'method_wrapper'

class Bob
def say_hello
puts "Hello!"
end
end

Bob.wrap_instance_method(pattern: /^say_hello$/, & ->(method, *args, &block) do
puts "Before call #{method.name}"
method.call(*args, &block)
puts "After call #{method.name}"
end)

Bob.new.say_hello
```

Also is possible:

* Invoke private and protected methods.
* Invoke class methods.
* Replace method call with another implementation.