Class: RoboDog::Robot

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/robo_dog/robot.rb

Constant Summary collapse

DataError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Robot

Returns a new instance of Robot.



21
22
23
24
25
# File 'lib/robo_dog/robot.rb', line 21

def initialize(args = {})
  @pose = args[:pose]
  @commands = args[:commands] || []
  @coordinators = args[:coordinators] || []
end

Class Method Details

.build(attrs) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
# File 'lib/robo_dog/robot.rb', line 12

def self.build(attrs)
  raise DataError unless validate(attrs[:commands])

  pose = Pose.build(attrs[:pose])
  commands = lex(attrs[:commands])

  new(pose: pose, commands: commands)
end

Instance Method Details

#add_coordinator(coordinator) ⇒ Object



27
28
29
# File 'lib/robo_dog/robot.rb', line 27

def add_coordinator(coordinator)
  coordinators << coordinator
end

#execute(mode) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/robo_dog/robot.rb', line 31

def execute(mode)
  case mode
  when :all
    commands.map! do |command|
      self.send(command)
      nil
    end
    commands.compact!
  when :next
    command = commands.shift
    if command
      self.send(command)
      true
    else
      false
    end
  end
end

#leftObject



61
62
63
64
# File 'lib/robo_dog/robot.rb', line 61

def left
  pose.rotate!(:counter)
  nil
end

#moveObject



50
51
52
53
54
# File 'lib/robo_dog/robot.rb', line 50

def move
  adj_pose = pose.adjacent
  self.pose = adj_pose if coordinators.all? { |c| c.valid_coordinates?(adj_pose.coordinates) }
  nil
end

#rightObject



56
57
58
59
# File 'lib/robo_dog/robot.rb', line 56

def right
  pose.rotate!(:clockwise)
  nil
end