class Robot::CommandProxy

This class implements the proxy design pattern. It sits between the client and the commands, and only invokes the command if it passes validation.

Attributes

command_string[R]
position[R]

Public Class Methods

new(command_string:, position: nil) click to toggle source
# File lib/robot/command_proxy.rb, line 9
def initialize(command_string:, position: nil)
  @command_string = command_string
  @position = position
end

Public Instance Methods

call() click to toggle source
# File lib/robot/command_proxy.rb, line 14
def call
  return if not_placed_yet? && !place_command?
  return position if would_fall?

  execute
end

Private Instance Methods

command_class() click to toggle source
# File lib/robot/command_proxy.rb, line 31
def command_class
  Robot::Commands::Factory.build(command_string)
end
execute() click to toggle source
# File lib/robot/command_proxy.rb, line 23
def execute
  if place_command?
    command_class.build_from_string_command(command_string)
  else
    command_class.(position)
  end
end
not_placed_yet?() click to toggle source
# File lib/robot/command_proxy.rb, line 46
def not_placed_yet?
  position.nil?
end
place_command?() click to toggle source
# File lib/robot/command_proxy.rb, line 50
def place_command?
  command_class == Robot::Commands::Place
end
table() click to toggle source
# File lib/robot/command_proxy.rb, line 42
def table
  @table ||= Robot::Table.new
end
would_fall?() click to toggle source
# File lib/robot/command_proxy.rb, line 35
def would_fall?
  return unless [Robot::Commands::Move, Robot::Commands::Place].include?(command_class)

  new_position = execute
  new_position.point > table.max_point || new_position.point < table.min_point
end