Class: Slappy::Listener::Base

Inherits:
Object
  • Object
show all
Includes:
Targettable, Validatable
Defined in:
lib/slappy/listeners/base.rb

Direct Known Subclasses

SubtypeListener, TextListener, TypeListener

Instance Attribute Summary

Attributes included from Validatable

#pattern

Instance Method Summary collapse

Methods included from Targettable

#target, #target?

Methods included from Debuggable

included

Methods included from Validatable

#valid?

Constructor Details

#initialize(pattern, options = {}, &callback) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
# File 'lib/slappy/listeners/base.rb', line 10

def initialize(pattern, options = {}, &callback)
  self.pattern = pattern
  if options[:from]
    target.channel = options[:from][:channel]
    target.user = options[:from][:user]
  end
  @callback = callback
end

Instance Method Details

#call(event) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/slappy/listeners/base.rb', line 19

def call(event)
  channel = "channel: #{event.try(:channel).try(:name)}"
  element = "#{target_element}: #{event.try(target_element)}"
  Debug.log "Listen event call(#{channel} / #{element})"

  return unless valid?(event)
  return unless target?(event)

  Debug.log "Callback event call: #{pattern}"
  @callback.call(event)
end