Class: Cognition::Matcher
- Inherits:
-
Object
- Object
- Cognition::Matcher
- Defined in:
- lib/cognition/matcher.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#help ⇒ Object
Returns the value of attribute help.
-
#response ⇒ Object
Returns the value of attribute response.
-
#trigger ⇒ Object
Returns the value of attribute trigger.
Instance Method Summary collapse
- #attempt(msg) ⇒ Object
-
#initialize(trigger, help = 'Undocumented', &action) ⇒ Matcher
constructor
A new instance of Matcher.
- #matches?(msg) ⇒ Boolean
- #run(msg) ⇒ Object
Constructor Details
#initialize(trigger, help = 'Undocumented', &action) ⇒ Matcher
Returns a new instance of Matcher.
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
#action ⇒ Object
Returns the value of attribute action.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def action @action end |
#help ⇒ Object
Returns the value of attribute help.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def help @help end |
#response ⇒ Object
Returns the value of attribute response.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def response @response end |
#trigger ⇒ Object
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
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 |