Project

ripar

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Convert series of chained methods from . syntax to block syntax. Like instance_eval but with access to external scope.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Ripar Gem Version Build Status

Think riparian. Think old man river, he jus' keep on rollin'. Think rive. Also river, reaver, repair, reaper.

Tear chained method calls apart, put them in a block, and return the block value. eg

Before:

  result = values.select{|x| x.name =~ /Jo/}.map{|x| x.count}.inject(0){|s,x| s + x}

After:

  result = values.rive do
    select{|x| x.name =~ /Jo/}
    map{|x| x.count}
    inject(0){|s,x| s + x}
  end

This is also a little different to instance_eval, because the following will work:

  outside_block_regex = /Wahoody-hey/

  result = values.rive do
    select{|x| x.name =~ outside_block_regex}
    map{|x| x.count}
    inject(0){|s,x| s + x}
  end

Warning this can have some rare but weird side effects:

  • will probably break on classes that have defined method_missing, but not respond_to_missing
  • an outside variable with the same name as an inside method taking in-place hash argument will cause a syntax error.

But you can obviate all of that by just using the safe syntax:

  outside_block_regex = /Wahoody-hey/

  result = values.rive do |vs|
    vs.select{|x| x.name =~ outside_block_regex}
    vs.map{|x| x.count}
    vs.inject(0){|s,x| s + x}
  end

Or using the magic disambiguaters:

  select = /Wahoody-hey/

  result = values.rive do
    __inside__.select{|x| x.name =~ __outside__.select}
    map{|x| x.count}
    inject(0){|s,x| s + x}
  end

Installation

Add this line to your application's Gemfile:

gem 'ripar'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ripar

Usage

In your class

class YourChainableThing
  include Ripar
end

yct = YourChainableThing.new.rive do
  # operations
end

In a singleton

o = Object.new
class << o
  include Ripar
end

The mostest lightweightiest

o = Object.new.extend(Ripar)

Monkey-patch

class Object
  include Ripar
end

Contributing

The standard github pull request dance:

  1. Fork it ( http://github.com//ripar/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

PS

Thing = Struct.new :name, :count
values = [
  Thing.new('John', 20),
  Thing.new('Joe', 7),
  Thing.new('Paul', 3),
  Thing.new('James', 3),
  Thing.new('Wahoody-heydi-dude', 3.141527),
]