Class: RubyJard::ControlFlow
- Inherits:
-
Object
- Object
- RubyJard::ControlFlow
- Defined in:
- lib/ruby_jard/control_flow.rb
Overview
A helper to standardize control instruction passed around via throw and catch mechanism.
Constant Summary collapse
- THROW_KEYWORD =
:jard_control_flow
- ALLOW_LIST =
{ continue: [], # lib/ruby_jard/commands/continue_command.rb exit: [], # lib/ruby_jard/commands/exit_command.rb frame: [:frame], # lib/ruby_jard/commands/frame_command.rb up: [:times], # lib/ruby_jard/commands/up_command.rb down: [:times], # lib/ruby_jard/commands/down_command.rb next: [:times], # lib/ruby_jard/commands/next_command.rb step: [:times], # lib/ruby_jard/commands/step_command.rb step_out: [:times], # lib/ruby_jard/commands/step_out_command.rb key_binding: [:action], # lib/ruby_jard/commands/step_command.rb list: [] # lib/ruby_jard/commands/list_command.rb }.freeze
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, arguments = {}) ⇒ ControlFlow
constructor
A new instance of ControlFlow.
- #validate! ⇒ Object
Constructor Details
#initialize(command, arguments = {}) ⇒ ControlFlow
Returns a new instance of ControlFlow.
24 25 26 27 28 29 |
# File 'lib/ruby_jard/control_flow.rb', line 24 def initialize(command, arguments = {}) @command = command @arguments = arguments validate! end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
22 23 24 |
# File 'lib/ruby_jard/control_flow.rb', line 22 def arguments @arguments end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
22 23 24 |
# File 'lib/ruby_jard/control_flow.rb', line 22 def command @command end |
Class Method Details
.dispatch(command, arguments = {}) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/ruby_jard/control_flow.rb', line 49 def dispatch(command, arguments = {}) if command.is_a?(RubyJard::ControlFlow) throw THROW_KEYWORD, command else throw THROW_KEYWORD, new(command, arguments) end end |
.listen ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby_jard/control_flow.rb', line 57 def listen raise RubyJard::Error, 'This method requires a block' unless block_given? flow = catch(THROW_KEYWORD) do yield nil end if flow.nil? || flow.is_a?(RubyJard::ControlFlow) flow else raise RubyJard::Error, 'Control flow misused!' end end |
Instance Method Details
#validate! ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby_jard/control_flow.rb', line 31 def validate! if command.to_s.empty? raise RubyJard::Error, 'Control command is empty' end unless ALLOW_LIST.key?(command) raise RubyJard::Error, "Control command `#{command}` is not registered in the allow list." end invalid_keys = arguments.keys - ALLOW_LIST[command] unless invalid_keys.empty? raise RubyJard::Error, "Control command `#{command}` is attached with unregister arguments: #{invalid_keys}" end end |