Class: Cognition::Matcher
- Inherits:
-
Object
- Object
- Cognition::Matcher
- Defined in:
- lib/cognition/matcher.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#help ⇒ Object
readonly
Returns the value of attribute help.
-
#match_data ⇒ Object
readonly
Returns the value of attribute match_data.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#trigger ⇒ Object
readonly
Returns the value of attribute trigger.
Instance Method Summary collapse
- #attempt(msg) ⇒ Object
-
#initialize(trigger, options = {}, &action) ⇒ Matcher
constructor
A new instance of Matcher.
- #matches?(msg) ⇒ Boolean
- #run(msg) ⇒ Object
Constructor Details
#initialize(trigger, options = {}, &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, = {}, &action) fail ArgumentError, "matcher must have a trigger" unless trigger fail ArgumentError, "matcher must have an action" unless action @trigger = trigger @help = [:help] ||= {} @action = action end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def action @action end |
#help ⇒ Object (readonly)
Returns the value of attribute help.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def help @help end |
#match_data ⇒ Object (readonly)
Returns the value of attribute match_data.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def match_data @match_data end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/cognition/matcher.rb', line 3 def response @response end |
#trigger ⇒ Object (readonly)
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
19 20 21 22 |
# File 'lib/cognition/matcher.rb', line 19 def attempt(msg) return false unless matches?(msg) run(msg) end |
#matches?(msg) ⇒ Boolean
30 31 32 33 34 35 36 37 |
# File 'lib/cognition/matcher.rb', line 30 def matches?(msg) case trigger when String trigger == msg.command when Regexp @match_data = trigger.match msg.command end end |
#run(msg) ⇒ Object
24 25 26 27 28 |
# File 'lib/cognition/matcher.rb', line 24 def run(msg) @response = action.call(msg, match_data).to_s rescue => e @response = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}" end |