Sha256: e6c1f2b118ae1747ec1d1668138e490b05b0b4360ab45d91e48fccf36bb3e6ba

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module Savage
  class SubPath
    include Utils
    include DirectionProxy
    
    define_proxies do |sym,const|
      define_method(sym) do |*args|
        (@directions << constantize("Savage::Directions::" << const).new(*args)).last
      end 
    end
    
    attr_accessor :directions
    
    def move_to(*args)
      return nil unless @directions.empty?
      (@directions << Directions::MoveTo.new(*args)).last
    end
    
    def initialize(*args)
      @directions = []
      move_to(*args) if (2..3).include?(args.length)
      yield self if block_given?
    end
    
    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
    
    def commands
      @directions
    end
    
    def closed?
      @directions.last.kind_of? Directions::ClosePath
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
savage-1.0.0 lib/savage/sub_path.rb
savage-0.2.0 lib/savage/sub_path.rb