class Attentive::ListenerCollection

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/attentive/listener_collection.rb, line 11
def initialize
  super Concurrent::Array.new
end

Public Instance Methods

hear(message) click to toggle source
# File lib/attentive/listener_collection.rb, line 23
def hear(message)
  message = Attentive::Message.new(message) unless message.is_a?(Attentive::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, i), listener: listener).match!
        next unless match

        # Don't match more than one phrase per listener
        matches.push match
        break
      end
    end
  end
  matches
end
listen_for(*args, &block) click to toggle source
# File lib/attentive/listener_collection.rb, line 15
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