Project

underware

0.0
No commit activity in last 3 years
No release in over 3 years
Middleware for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Gem Version Build Status

Underware

Middleware for Ruby

Installation

gem install underware

2-Minute Tour

If your object defines a #call method, then it can be used as middleware. For example:

class Add
  attr_reader :delta

  def initialize(delta)
    @delta = delta
  end

  def call(x)
    puts "BEFORE: #{x} + #{delta}"

    # Pass control to the next piece of middleware
    yield x + delta

    puts "AFTER:  #{x} + #{delta}"
  end
end

Now that we've defined some middleware, let's chain it together:

require 'underware'

f = Underware.fold([Add.new(1), Add.new(-2), Add.new(3)]) do |x|
  puts "THE VALUE IS: #{x}"
end

f.call(3)

This produces the following output:

BEFORE: 3 + 1
BEFORE: 4 + -2
BEFORE: 2 + 3
THE VALUE IS: 5
AFTER:  2 + 3
AFTER:  4 + -2
AFTER:  3 + 1

Note that the block to the Underware.fold method is just for convenience. You could have also written:

require 'underware'

g = Proc.new do |x|
  puts "THE VALUE IS: #{x}"
end

f = Underware.fold([Add.new(1), Add.new(-2), Add.new(3), g])

f.call(3)

Wouldn't it be nice if you could somehow fold the middleware and call it at the same time? Well, guess what -- you can!

require 'underware'

Underware.exec_underware([Add.new(1), Add.new(-2), Add.new(3)], 3) do |x|
  puts "THE VALUE IS: #{x}"
end

In reality, the Underware#exec_underware is really intended to be used as a mixin (ie: include Underware). Otherwise, it's a lot to type! Fortunately, there's another shortcut:

require 'underware'

# This is the same as Underware.exec_underware
Underware([Add.new(1), Add.new(-2), Add.new(3)], 3) do |x|
  puts "THE VALUE IS: #{x}"
end