Class: Ant::Bot::Command
- Inherits:
-
Object
- Object
- Ant::Bot::Command
- Defined in:
- lib/ant/bot/command_definition.rb
Overview
Object that wraps a command, it is analogus to a route definition. it currently only gets a param list, but it will be extended to a more complex DSL.
Instance Method Summary collapse
-
#execute(params) ⇒ Object
Calls the block with the params list.
-
#initialize(params, block) ⇒ Command
constructor
Receives a list of params as symbols and the lambda with the block.
-
#next_missing_param(current_params) ⇒ Object
Finds the first empty param from the given parameter.
-
#ready?(current_params) ⇒ Boolean
Checks if the params object given contains all the needed values.
Constructor Details
#initialize(params, block) ⇒ Command
Receives a list of params as symbols and the lambda with the block.
10 11 12 13 |
# File 'lib/ant/bot/command_definition.rb', line 10 def initialize(params, block) @params = params @block = block end |
Instance Method Details
#execute(params) ⇒ Object
Calls the block with the params list. Fails if there is a missing param
16 17 18 19 20 |
# File 'lib/ant/bot/command_definition.rb', line 16 def execute(params) raise 'NotReady' unless ready?(params) @block.call(params) end |
#next_missing_param(current_params) ⇒ Object
Finds the first empty param from the given parameter
28 29 30 |
# File 'lib/ant/bot/command_definition.rb', line 28 def next_missing_param(current_params) @params.find { |key| !current_params.key?(key) } end |
#ready?(current_params) ⇒ Boolean
Checks if the params object given contains all the needed values
23 24 25 |
# File 'lib/ant/bot/command_definition.rb', line 23 def ready?(current_params) @params.all? { |key| current_params.key?(key) } end |