Class: Cognition::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/cognition/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trigger, help = 'Undocumented', &action) ⇒ Matcher

Returns a new instance of Matcher.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
# File 'lib/cognition/matcher.rb', line 5

def initialize(trigger, help = 'Undocumented', &action)
  raise ArgumentError, 'matcher must have a trigger' unless trigger
  raise ArgumentError, 'matcher must have a action' unless action
  @trigger = trigger
  @help = help
  @action = action
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



3
4
5
# File 'lib/cognition/matcher.rb', line 3

def action
  @action
end

#helpObject

Returns the value of attribute help.



3
4
5
# File 'lib/cognition/matcher.rb', line 3

def help
  @help
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/cognition/matcher.rb', line 3

def response
  @response
end

#triggerObject

Returns the value of attribute trigger.



3
4
5
# File 'lib/cognition/matcher.rb', line 3

def trigger
  @trigger
end

Instance Method Details

#attempt(msg) ⇒ Object



13
14
15
16
17
# File 'lib/cognition/matcher.rb', line 13

def attempt(msg)
  return false unless matches?(msg)

  run(msg)
end

#matches?(msg) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/cognition/matcher.rb', line 23

def matches?(msg)
  case trigger
  when String
    trigger == msg.command
  when Regexp
    msg.matches = trigger.match msg.command
  end
end

#run(msg) ⇒ Object



19
20
21
# File 'lib/cognition/matcher.rb', line 19

def run(msg)
  @response = action.call(msg)
end