Sha256: 3cb17ff9bdaa7be5cbbaf538a313a66b5796d0b17ccb393a9643400aa2fe7bbd

Contents?: true

Size: 792 Bytes

Versions: 1

Compression:

Stored size: 792 Bytes

Contents

# frozen_string_literal: true

module Hollaback
  # An object that contains a sequence of callbacks along with a main execution
  # function
  class Sequence
    attr_reader :befores, :afters, :main

    def initialize(args = {}, &main)
      @main    = main
      @befores = args.fetch(:befores, [])
      @afters  = args.fetch(:afters, [])
    end

    def before(&before)
      befores << before
      self
    end

    def after(&after)
      afters << after
      self
    end

    def around(&around)
      Sequence.new do |target|
        around.call(target) { call(target) }
      end
    end

    def call(target = nil)
      befores.each { |before| before.call(target) }

      main.call(target).tap do
        afters.each { |after| after.call(target) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hollaback-0.1.1 lib/hollaback/sequence.rb