Sha256: 05506ac316b4d8a08e3c4fcf1739e7cd4281de0039b4ccea2071ff1939a0a33d

Contents?: true

Size: 719 Bytes

Versions: 2

Compression:

Stored size: 719 Bytes

Contents

module Slappy
  module Listener
    module Listenable
      include ActiveSupport::Concern

      attr_reader :pattern

      def initialize(target, callback)
        @pattern = target
        @callback = callback
      end

      def call(event)
        return unless time_valid?(event)

        target = event.send(target_element)
        return unless target

        event.matches = target.match pattern
        return unless event.matches

        @callback.call(event)
      end

      def time_valid?(event)
        event.ts > Slappy.client.start_time
      end

      private

      def target_element
        self.class.name.split('::').last.gsub(/Listener$/, '').underscore.to_sym
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slappy-0.5.1 lib/slappy/listeners/concerns/listenable.rb
slappy-0.5.0 lib/slappy/listeners/concerns/listenable.rb