Class: Ego::Robot
- Inherits:
-
Object
- Object
- Ego::Robot
- Includes:
- Hooks, Hooks::InstanceHooks
- Defined in:
- lib/ego/robot.rb
Overview
The robot instance provides a DSL for plug-ins to use to implement new functionality and also share that functionality with other plug-ins.
Much built-in functionality of Ego is also implemented using the plug-in DSL.
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#can(desc) ⇒ Array
Adds a new capability, which documents functionality added by a plug-in.
-
#first_handler_for(query) ⇒ nil, Handler
Find the highest-priority handler for a given query and return it.
-
#handle(query) ⇒ false
Run the action for the highest-priority handler that can handle the given query and return the result.
-
#initialize(options) ⇒ Robot
constructor
A new instance of Robot.
-
#on(condition, priority = 5, &action) ⇒ void
Register a new query handler.
-
#ready ⇒ self
Run
on_readyhook. -
#run_action(action, params) ⇒ Object
Run action with given parameters in the context of the robot instance.
-
#shutdown ⇒ void
Run
on_shutdownhook.
Constructor Details
#initialize(options) ⇒ Robot
Returns a new instance of Robot.
31 32 33 34 35 36 |
# File 'lib/ego/robot.rb', line 31 def initialize() @name = .robot_name @options = @capabilities = [] @handlers = [] end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
20 21 22 |
# File 'lib/ego/robot.rb', line 20 def capabilities @capabilities end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/ego/robot.rb', line 20 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
20 21 22 |
# File 'lib/ego/robot.rb', line 20 def @options end |
Instance Method Details
#can(desc) ⇒ Array
Adds a new capability, which documents functionality added by a plug-in.
71 72 73 |
# File 'lib/ego/robot.rb', line 71 def can(desc) @capabilities << Capability.new(desc) end |
#first_handler_for(query) ⇒ nil, Handler
Find the highest-priority handler for a given query and return it. When a block is passed, the block is called if and only if a handler is found, passing the handler and parsed params as the block's arguments.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ego/robot.rb', line 174 def first_handler_for(query) @handlers.sort.reverse_each do |handler| if (params = handler.handle(query)) yield(handler, params) if block_given? return handler end end nil end |
#handle(query) ⇒ false
Run the action for the highest-priority handler that can handle the given query and return the result. Associated hooks are run before and after running the action.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ego/robot.rb', line 154 def handle(query) run_hook :before_handle_query, query first_handler_for(query) do |handler, params| return run_action(handler.action, params).tap do |_result| run_hook :after_handle_query, query, handler end end run_hook :on_unhandled_query, query false end |
#on(condition, priority = 5, &action) ⇒ void
This method returns an undefined value.
Register a new query handler.
The robot will execute the given block (the "action") when the given pattern (the "condition") matches. Conditions are assigned priorities, which determined in what order conditions are checked against the query. A higher number is treated as higher priority.
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ego/robot.rb', line 112 def on(condition, priority = 5, &action) unless action # rubocop:disable Style/IfUnlessModifier raise RobotError, "Hook requires an action: robot.on #{condition.inspect}" end if condition.respond_to?(:each_pair) # Condition is a hash of conditions and priorities condition.each_pair { |c, p| on(c, p, &action) } else # Register a new handler with condition @handlers << Handler.new(condition, action, priority) end end |
#ready ⇒ self
Run on_ready hook.
Should be called after plug-ins are registered, before handling queries.
45 46 47 48 |
# File 'lib/ego/robot.rb', line 45 def ready run_hook :on_ready self end |
#run_action(action, params) ⇒ Object
Run action with given parameters in the context of the robot instance.
134 135 136 137 138 139 |
# File 'lib/ego/robot.rb', line 134 def run_action(action, params) run_hook :before_action, action, params result = instance_exec(*params, &action) run_hook :after_action, action, params, result result end |
#shutdown ⇒ void
This method returns an undefined value.
Run on_shutdown hook.
Should be called after all queries are handled, before program termination.
58 59 60 |
# File 'lib/ego/robot.rb', line 58 def shutdown run_hook :on_shutdown end |