Module: ToyRoboSimulator::Validator

Included in:
Robo
Defined in:
lib/toy_robo_simulator/validator.rb

Overview

Validator serves to validate operations performed by a Robo. It is a standalone module but takes arguments and attributes from a Robo.

Instance Method Summary collapse

Instance Method Details

#moveable?Boolean

Prevents Robo#move from moving out of the table. Returns false if the Robo is at the edge of the table, otherwise returns true.

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/toy_robo_simulator/validator.rb', line 21

def moveable?
  if edge?
    puts 'The Robo is at edge. No further move is allowed.'
  else
    true
  end
end

#placed?Boolean

Ensures the Robo is placed before any other actions are performed. Returns false if no attributes are set for the Robo, otherwise returns true.

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/toy_robo_simulator/validator.rb', line 31

def placed?
  if @x && @y && @orientation
    true
  else
    puts 'The Robo is not placed yet. Use PLACE command first.'
    false
  end
end

#valid_placement?Boolean

Ensures input values of Robo#place action is valid in type and range. Returns false if it is not valid, otherwise returns true.

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/toy_robo_simulator/validator.rb', line 7

def valid_placement?
  validate_placement
  if @errors.any?
    @errors.each { |message| puts message }
    @x, @y, @orientation = nil, nil, nil
    false
  else
    @x, @y = @x.to_i, @y.to_i
    true
  end
end