Class: Savage::SubPath

Inherits:
Object
  • Object
show all
Includes:
DirectionProxy, Utils
Defined in:
lib/savage/sub_path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DirectionProxy

included

Methods included from Utils

#bool_to_int

Constructor Details

#initialize(*args) {|_self| ... } ⇒ SubPath

Returns a new instance of SubPath.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
25
# File 'lib/savage/sub_path.rb', line 21

def initialize(*args)
  @directions = []
  move_to(*args) if (2..3).include?(args.length)
  yield self if block_given?
end

Instance Attribute Details

#directionsObject

Returns the value of attribute directions.



14
15
16
# File 'lib/savage/sub_path.rb', line 14

def directions
  @directions
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/savage/sub_path.rb', line 45

def closed?
  @directions.last.kind_of? Directions::ClosePath
end

#commandsObject



41
42
43
# File 'lib/savage/sub_path.rb', line 41

def commands
  @directions
end

#move_to(*args) ⇒ Object



16
17
18
19
# File 'lib/savage/sub_path.rb', line 16

def move_to(*args)
  return nil unless @directions.empty?
  (@directions << Directions::MoveTo.new(*args)).last
end

#to_commandObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/savage/sub_path.rb', line 27

def to_command
  @directions.to_enum(:each_with_index).collect { |dir, i|
    command_string = dir.to_command
    if i > 0
      prev_command_code = @directions[i-1].command_code
      if dir.command_code == prev_command_code || (prev_command_code.match(/^[Mm]$/) && dir.command_code == 'L')
        command_string.gsub!(/^[A-Za-z]/,'')
        command_string.insert(0,' ') unless command_string.match(/^-/)
      end
    end
    command_string
  }.join
end