Giter VIP home page Giter VIP logo

interceptor's Introduction

Interceptor

Mad Max had one, and he was freaking awesome.

Overview:

Interceptor allows you to wrap other objects, and intercept method calls, without changing the target objects code, or performing any hairy metaprogramming. You may use this library to perform these kinds of tasks:

  • Modifying method arguments as they come in.
  • Modify return values as they go out.
  • Perform other tasks (such as logging) without sprinkling the code throughout the objects (typically unrelated) implementation.
  • Call completely different methods depending on anything you want, I suppose.

You can think of this as a more explicit form of AOP, but unlike AOP, instead of being picked off by a sniper from 3000m away (and being very surprised about the whole thing), it's like the sniper has arrived at your house with your birthday cake and birthday present in order to convey his birthday wishes.

Example:

Let's say as a Ruby programmer you picked up some Lua and decided that Lua's idea of starting an Array at 1 is way better than starting an Array at 0, in a fit of blinding genius you decide to make a Ruby Array behave like a Lua one.

lucky_numbers = [42, 666, 2012]

lucky_numbers[0] # => 42
lucky_numbers[1] # => 666
lucky_numbers[2] # => 2012

even_luckier_numbers = Interceptor.new(lucky_numbers)

# intercept the [] method, and take 1 off the index passed in
# so that our arrays become like lua arrays, kinda
even_luckier_numbers.intercept(:[]) do |receiver, target, index|
  receiver[index-1]
end

even_luckier_numbers[1] # => 42
even_luckier_numbers[2] # => 666
even_luckier_numbers[3] # => 2012

The first two arguments to the block is a reference to the object passed to the Interceptor (in this case, a reference to lucky_numbers), the second is a symbol of the method call (in this case, it is :[]), and the remaining arguments are the arguments passed to the method call (in this case, 1 and 2 and 3).

Patches:

What the hell is a patch?

RubyGem?

Nope, how do you make those? I asked this guy called John and he told me to 'google it' or something.

Mad Max:

Is freaking awesome, here's proof.

Other Questions?

If you have any questions, shoot me a message on the github-o-sphere. I likely have no idea what you're talking about as I have no idea what I'm talking about most of the time.

interceptor's People

Contributors

ryan-allen 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.