Sha256: aad0cbb5c0d3686d37f393b4c2f4e805343d6c0b1b6a6da202ebfb3c4c5d4da6

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module ToyRoboSimulator
  module Validator
    def validate_placement(x, y, orientation)
      @errors << 'X must be a number' unless int?(x)
      @errors << 'Y must be a number' unless int?(y)
      @errors << "X must be between 0 and #{@table.x}" unless in_range?(x, @table.x)
      @errors << "Y must be between 0 and #{@table.y}" unless in_range?(y, @table.y)
      @errors << 'Orientation should be either NORTH, SOUTH, EAST, or WEST' unless orientation_valid?(orientation)
    end

    def validate_movement
      @errors << 'The Robo is at edge. No further move is allowed.' if at_table_edge?
    end

    def validate_if_placed
      @errors << 'The Robo is not placed yet. Use PLACE command first.' unless @x && @y && @orientation
    end

    def at_table_edge?
      (@orientation == :north && @y == @table.y) ||
      (@orientation == :east  && @x == @table.x) ||
      (@orientation == :south && @y == 0)        ||
      (@orientation == :west  && @x == 0)
    end

    private

    def int?(value)
      value.to_s == value.to_i.to_s
    end

    def in_range?(value, max)
      value.to_i >= 0 && value.to_i <= max
    end

    def orientation_valid?(orientation)
      ::ORIENTATIONS.include? orientation.downcase.to_sym
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toy_robo_simulator-0.1.0 lib/toy_robo_simulator/validator.rb