require "nasa_rovers/coord" module NasaRovers class Position class CardinalError < StandardError; end def self.factory(data) new(*data.split(" ")) end attr_reader :coord attr_accessor :cardinal def initialize(x, y, cardinal, coord_class=Coord) @coord = coord_class.new(x, y) @cardinal = check(cardinal) end def to_s "#{coord} #{cardinal}" end def ==(other) coord == other.coord && cardinal == other.cardinal end private def check(cardinal) return cardinal if CARDINALS.include?(cardinal) fail CardinalError.new("#{cardinal} is not a valid cardinal point") end end end