Class: Ego::Handler
Overview
Note:
Handlers should be registered by plug-ins using the Robot#on
method.
Handlers map user queries to actions.
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare
prioritywithother.priority. -
#handle(query) ⇒ false, Array
Match the given query against the condition.
-
#initialize(condition, action, priority = 5) ⇒ Handler
constructor
A new instance of Handler.
Constructor Details
#initialize(condition, action, priority = 5) ⇒ Handler
Returns a new instance of Handler.
44 45 46 47 48 |
# File 'lib/ego/handler.rb', line 44 def initialize(condition, action, priority = 5) @condition = normalize(condition) @action = action @priority = priority end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
39 40 41 |
# File 'lib/ego/handler.rb', line 39 def action @action end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
39 40 41 |
# File 'lib/ego/handler.rb', line 39 def condition @condition end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
39 40 41 |
# File 'lib/ego/handler.rb', line 39 def priority @priority end |
Instance Method Details
#<=>(other) ⇒ Object
Compare priority with other.priority.
53 54 55 |
# File 'lib/ego/handler.rb', line 53 def <=>(other) @priority <=> other.priority end |
#handle(query) ⇒ false, Array
Match the given query against the condition.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ego/handler.rb', line 62 def handle(query) return false unless (result = @condition.call(query)) @action.parameters.each_with_object([]) do |param, arr| begin arr << result[param.pop] rescue IndexError arr << nil # Match group isn't defined. end end end |