Project

meddleware

0.1
The project is in a healthy, maintained state
A middleware framework to make meddling easy.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 11
>= 3.13
>= 0.22
 Project Readme

Meddleware

Gem codecov

A middleware framework to make meddling easy. Middleware is a popular pattern from Rack and Rails, which provides callers a way to execute code before and after yours. This gives callers the ability to modify parameters or results, conditionally skip execution, and log activity without the need to monkey patch or subclass.

require 'meddleware'

# lib/mywidget.rb
class MyWidget
  extend Meddleware

  def do_the_thing
    # invoke middleware chain
    middleware.call do
      # do your thing
      ...
    end
  end
end

# config/initializers/mywidget.rb
MyWidget.middleware do
  # add a logger
  use { puts "before the thing" }

  # add another middleware
  use MyMiddleware
end


# use it from whereever
MyWidget.new.do_the_thing

Usage

Extend your class with Meddleware to add a middleware method. Or use include to give each instance its own, individual middleware.

class MyWidget
  extend Meddleware
end

MyWidget.middleware

Then wrap your class's functionality so it will get executed along with the all the registered middleware.

class MyWidget
  extend Meddleware

  def do_the_thing
    # invoke middleware chain
    middleware.call(*args) do
      # do your thing
      ...
    end
  end
end

See documentation for full DSL.


Full Example

# lib/mylist.rb
module MyList
  extend Meddleware

  # generate an array from 1 to n
  def self.generate(n)
    # invoke middleware chain
    middleware.call(n) do |n|
      # do the actual work of generating your results 
      (1..n).to_a
    end
  end
end

# app/initializers/mylist.rb
class OneExtra
  def call(n)
    # adds one to the argument being passed in
    yield(n + 1)
  end
end

class Doubler
  def call(*)
    # modifies the results by doubles each value
    yield.map {|x| x * 2 }
  end
end

MyList.middleware do
  use OneExtra
  use Doubler

  # loggers
  prepend {|x| puts "n starts as #{x}" }
  append  {|x| puts "n ends as #{x}" }
end


# use it
> MyList.generate(2)

# would normally output [ 1, 2 ]
# but with middleware:

n starts as 2
n ends as 3
=> [2, 4, 6]

Contributing

Yes please :)

  1. Fork it
  2. Create your feature branch (git checkout -b my-feature)
  3. Ensure the tests pass (bundle exec rspec)
  4. Commit your changes (git commit -am 'awesome new feature')
  5. Push your branch (git push origin my-feature)
  6. Create a Pull Request

Inspired by / Thanks to

@mitchellh + middleware

@mperham + Sidekiq

Rails