Project

wendy

0.0
No commit activity in last 3 years
No release in over 3 years
Provides a nice way of chaining methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

Wendy

###An elegant way to chain methods in Ruby ##

Simply include Wendy in the class you want to chain methods in. You now have access to the wend method.

The wend method accepts as its argument the initial parameter for the chain.

You then 'wend' this argument through multiple methods by chaining them with the through and and methods.

When you're done, call result or and_return to get the final result.

Here's an example:

class MathsFunctions
  include Wendy
  
  def double(x)
    x + x
  end
  
  def square(x)
    x * x
  end
  
  def two_x_squared(x)
    wend(x).through(:double).and(:square).and_return
  end
  
end

MathsFunctions.new.two_x_squared(5) # => 100

You can also pass extra arguments to methods in the chain, like this:

def add(x, y)
  x + y
end

def wend_with_arg(x)
  wend(x).through(:square).and(:add, 15).result
end

NB. Methods called in the chain must be defined on the class on which wend is called.