Sha256: 511474226dd944eccd19dc869fbf909823427a7e5dae347bb8855d7c038f1c40

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

module Savage
  class SubPath
    include Utils
    include DirectionProxy

    define_proxies do |sym,const|
      define_method(sym) do |*args|
        raise TypeError if const == "QuadraticCurveTo" && @directions.last.class != Directions::QuadraticCurveTo && [2,3].include?(args.length)
        raise TypeError if const == "CubicCurveTo" && @directions.last.class != Directions::CubicCurveTo && [4,5].include?(args.length)
        (@directions << ("Savage::Directions::" << const).constantize.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

7 entries across 7 versions & 1 rubygems

Version Path
savage-1.1.4 lib/savage/sub_path.rb
savage-1.1.3 lib/savage/sub_path.rb
savage-1.1.2 lib/savage/sub_path.rb
savage-1.1.1 lib/savage/sub_path.rb
savage-1.1.0 lib/savage/sub_path.rb
savage-1.0.3 lib/savage/sub_path.rb
savage-1.0.2 lib/savage/sub_path.rb