Sha256: 620125545e3be8f10605b482007838e61a4864e337ba7f66ba9b37b1c4eecca7

Contents?: true

Size: 1008 Bytes

Versions: 3

Compression:

Stored size: 1008 Bytes

Contents

require 'celluloid'

class Eye::Utils::CelluloidChain
  include Celluloid

  def initialize(target)
    @target = target
    @calls = []
    @running = false
  end

  def add(method_name, *args, &block)
    @calls << {:method_name => method_name, :args => args, :block => block}
    process! unless @running
  end

  def add_wo_dups(method_name, *args, &block)
    h = {:method_name => method_name, :args => args, :block => block}
    @calls << h if @calls[-1] != h
    process! unless @running
  end

  def list
    @calls
  end

  def names_list
    list.map{|el| el[:method_name].to_sym }
  end

  def clear
    @calls = []
  end

  # need, because of https://github.com/celluloid/celluloid/issues/22
  def inspect
    "Celluloid::Chain(#{@target.class}: #{@calls.inspect})"
  end

  attr_reader :running

private

  def process
    while call = @calls.shift
      @running = true
      @target.send(call[:method_name], *call[:args], &call[:block]) if @target.alive?
    end
    @running = false
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eye-0.2.1 lib/eye/utils/celluloid_chain.rb
eye-0.2 lib/eye/utils/celluloid_chain.rb
eye-0.1.11 lib/eye/utils/celluloid_chain.rb