Class: Savage::SubPath

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transformable

#rotate, #scale, #skew_x, #skew_y, #translate

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:



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

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.



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

def directions
  @directions
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


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

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

#commandsObject



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

def commands
  @directions
end

#fully_transformable?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/savage/sub_path.rb', line 74

def fully_transformable?
  directions.all? &:fully_transformable?
end

#move_to(*args) ⇒ Object



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

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

#to_commandObject



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

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

#to_transformable_commands!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/savage/sub_path.rb', line 54

def to_transformable_commands!
  if !fully_transformable?
    pen_x, pen_y = 0, 0
    directions.each_with_index do |dir, index|
      unless dir.fully_transformable?
        directions[index] = dir.to_fully_transformable_dir( pen_x, pen_y )
      end

      dx, dy = dir.movement
      if dir.absolute?
        pen_x = dx if dx
        pen_y = dy if dy
      else
        pen_x += dx if dx
        pen_y += dy if dy
      end
    end
  end
end

#transform(*args) ⇒ Object



50
51
52
# File 'lib/savage/sub_path.rb', line 50

def transform(*args)
  directions.each { |dir| dir.transform *args }
end