Sha256: 3107c1ebbbaebb2f0cb0d34a4ca169a85c087c43dc4cfa02516711bc9c7b4ff9

Contents?: true

Size: 850 Bytes

Versions: 1

Compression:

Stored size: 850 Bytes

Contents

require "callchain/version"

module CallChain
  # CallChain implementations
  #
  # Individual filters do work with ::call(object)
  #
  # Compose a CallChain with the ::use method
  #
  # Compose a CallChain of call chains because it exports ::call(object)
  #
  # Example:
  #
  #   Pricer = Class.new(CallChain)
  #
  #   class AppTierPricer < Pricer
  #     use PlanScoper, Quantizer, AppGrouper
  #     use UserGrouper
  #   end

  def use(*args)
    @chain ||= []
    @chain.push(*args)
  end

  def chain
    @chain
  end

  def call(thing)
    return thing unless chain
    chain.each do |filter|
      thing = filter.call(thing)
      yield thing, filter if block_given?
    end
    thing
  end

  def self.[](key)
    self.bind(key)
  end

  def self.bind(method_name, *args)
    lambda { |thing| thing.send(method_name, *args) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
callchain-0.0.2 lib/callchain.rb