Sha256: 6bf10fab174e36d9004513dfcc4f4a092d459fb5df9a08b5966e33e1f3b6ce4f

Contents?: true

Size: 969 Bytes

Versions: 4

Compression:

Stored size: 969 Bytes

Contents

require 'rexml/document'
require 'rexml/xpath'

module CanControlsGateway

  class CursorVector
  
    attr_reader :x, :y
  
    def initialize xml_node
      @xml_node = REXML::Document.new(xml_node.to_s)
      @x = nil
      @y = nil
    
      parse_xml
    end
  
    def is_valid?
      if (@y && @x)
        true
      else
        false
      end
    end
  
    def dimensions
      if is_valid?
        2
      else
        0
      end
    end
  
    def to_s
      if is_valid?
        "#{x},#{y}"
      else
        ""
      end
    end
  
    private
  
    def parse_xml
      my_vector_data = REXML::XPath.first(@xml_node, "/position").text
      my_vector_data.to_s.strip!
      my_vector_data = my_vector_data.split(",")
      if my_vector_data.size > 1
        @x = Float(my_vector_data[0].to_s.strip)
        @y = Float(my_vector_data[1].to_s.strip.to_f)
      else
        @x, @y = nil
      end
      rescue
        @x, @y = nil
    end
  
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tg-can-controls-gateway-0.1.3 lib/cursor_vector.rb
tg-can-controls-gateway-0.1.2 lib/cursor_vector.rb
tg-can-controls-gateway-0.1.1 lib/cursor_vector.rb
tg-can-controls-gateway-0.1.0 lib/cursor_vector.rb