Class: Attentive::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/attentive/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listeners, phrases, options, callback) ⇒ Listener

Returns a new instance of Listener.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/attentive/listener.rb', line 8

def initialize(listeners, phrases, options, callback)
  context_options = options.fetch(:context, {})
  @required_contexts = context_options.fetch(:in, Attentive.default_required_contexts)
  @required_contexts = [] if @required_contexts == :any
  @required_contexts = Set[*@required_contexts]
  @prohibited_contexts = context_options.fetch(:not_in, Attentive.default_prohibited_contexts)
  @prohibited_contexts = Set[*@prohibited_contexts]

  @listeners = listeners
  @phrases = tokenize_phrases!(phrases)
  @callback = callback
end

Instance Attribute Details

#phrasesObject (readonly)

Returns the value of attribute phrases.



6
7
8
# File 'lib/attentive/listener.rb', line 6

def phrases
  @phrases
end

Instance Method Details

#call(e) ⇒ Object



32
33
34
# File 'lib/attentive/listener.rb', line 32

def call(e)
  @callback.call(e)
end

#matches_context?(message) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/attentive/listener.rb', line 21

def matches_context?(message)
  return false unless message.contexts.superset? @required_contexts
  return false unless message.contexts.disjoint? @prohibited_contexts
  true
end

#stop_listening!Object



27
28
29
30
# File 'lib/attentive/listener.rb', line 27

def stop_listening!
  listeners.delete self
  self
end