Sha256: aefe72a07636de4aa3e77f893b6f085a6340886e0d088168231c67d5e5ae0c29

Contents?: true

Size: 828 Bytes

Versions: 1

Compression:

Stored size: 828 Bytes

Contents

module Hollaback
  class Chain
    attr_reader :callbacks

    def initialize
      @callbacks = []
    end

    def +(other)
      @callbacks += other.callbacks
      self
    end

    def before(execute = nil, &block)
      build(:before, execute, &block)
    end

    def after(execute = nil, &block)
      build(:after, execute, &block)
    end

    def around(execute = nil, &block)
      build(:around, execute, &block)
    end

    def empty?
      callbacks.empty?
    end

    def compile(&block)
      if empty?
        block
      else
        callbacks.inject(Sequence.new(&block)) do |sequence, callback|
          sequence.send(callback.type, &callback.build)
        end
      end
    end

    private

    def build(type, execute, &block)
      callbacks << Callback.new(type, execute, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hollaback-0.1.0 lib/hollaback/chain.rb