Sha256: 8568a863273a526544e07c7785cbcb2de3f2282c0f6697773426fbfa487f8ffc

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

module Hollaback
  # A set of callbacks
  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.1 lib/hollaback/chain.rb