Sha256: 040dd17207e1665661a9af4ab39077334f8671fe22762467ecc98366887190c8

Contents?: true

Size: 1004 Bytes

Versions: 1

Compression:

Stored size: 1004 Bytes

Contents

# frozen_string_literal: true

module RShade
  class EventObserver
    attr_reader :event_processor, :config

    # @param [RShade::Config::EventStore] config
    # @param [RShade::EventProcessor] event_store
    def initialize(config, event_processor)
      @event_processor = event_processor
      @config = config
      @level = 0
      @hook = Hash.new(0)
      @hook[:enter] = 1
      @hook[:leave] = -1
    end

    # @param [:enter, :leave, :other] type
    # @param [RShade::Event] event
    def call(event, type)
      @level += @hook[type]
      return unless pass?(event)

      enter(event) if type == :enter
      leave(event) if type == :leave
      other(event) if type == :other
    end

    private

    def enter(event)
      event_processor.enter event, @level
    end

    def leave(event)
      event_processor.leave event, @level
    end

    def other(event)
      event_processor.other event, @level
    end

    def pass?(event)
      config.filter.call(event)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rshade-0.2.2 lib/rshade/event_observer.rb