Sha256: 803c5bf05cd4afdfcfba9c5a57a8b9b7d857ad90163291fe4db07caf9f2de395

Contents?: true

Size: 763 Bytes

Versions: 1

Compression:

Stored size: 763 Bytes

Contents

module OwskiLog
  class Watcher
    TRIGGERS = [:all, :after_register, :after_finish]

    attr_accessor :lambda, :trigger

    def initialize(lambda, trigger: nil)
      @lambda = lambda
      @trigger = TRIGGERS.include?(trigger) ? trigger : :all
    end
  end

  module WatcherManager
    def self.register_watcher(watcher_collection, watcher)
      raise ArgumentError.new('You can only register OwskiLog::Watcher.') unless watcher.is_a? Watcher

      watcher_collection << watcher
    end

    def self.trigger_watchers(watcher_collection, triggers: nil)
      if triggers
        watcher_collection.select {|w| triggers.include?(w.trigger)}.each {|w| w.lambda.call}
      else
        watcher_collection.each {|w| w.lambda.call}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
owskilog-0.2.2 lib/owskilog/watcher.rb