Sha256: 772a28652f16485b1d9aef77f45c49ab0d79cac59779d3668689eb251c9312fb

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "attentive"
require "thread_safe"
require "delegate"
require "attentive/cursor"
require "attentive/listener"
require "attentive/matcher"

module Attentive
  class ListenerCollection < SimpleDelegator

    def initialize
      super ThreadSafe::Array.new
    end

    def listen_for(*args, &block)
      options = args.last.is_a?(::Hash) ? args.pop : {}

      Attentive::Listener.new(self, args, options, block).tap do |listener|
        push listener
      end
    end

    def hear(message)
      listeners = select { |listener| listener.matches_context?(message) }

      # Listen for any known phrase, starting with any token in the message.
      matches = []
      message.tokens.each_with_index do |token, i|
        listeners.each do |listener|
          listener.phrases.each do |phrase|
            match = Attentive::Matcher.new(phrase, Cursor.new(message.tokens, i), listener: listener, message: message).match!
            next unless match

            # Don't match more than one phrase per listener
            matches.push match
            break
          end
        end
      end
      matches
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attentive-0.2.0 lib/attentive/listener_collection.rb
attentive-0.1.1 lib/attentive/listener_collection.rb
attentive-0.1.0 lib/attentive/listener_collection.rb