Sha256: ee667e0e475b2882975be263626c54031117eaceb6a05b5b54a47882419b0d1f

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

class Ecu
  class DcmPropertyParser

    def self.call(line)
      key, _, value = line.strip.partition(/\s+/)
      eval_property(key, value)
    end

    private

    def self.eval_property(key, value)
      case key
      when "ST/X"        then { xvalue:      numeric_array(value) }
      when "ST/Y"        then { yvalue:      numeric_array(value) }
      when "WERT"        then { value:       numeric_array(value) }
      when "ST_TX/X"     then { xvalue:      string_array(value) }
      when "ST_TX/Y"     then { yvalue:      string_array(value) }
      when "TEXT"        then { value:       string_array(value) }
      when "FUNKTION"    then { function:    value }
      when "EINHEIT_X"   then { xunit:       string_value(value) }
      when "EINHEIT_Y"   then { yunit:       string_value(value) }
      when "EINHEIT_W"   then { unit:        string_value(value) }
      when "LANGNAME"    then { description: string_value(value) }
      when "DISPLAYNAME" then { }
      else fail ArgumentError, "Unknown key #{key}"
      end
    end

    def self.string_value(str)
      str.delete_surrounding('"')
    end

    def self.numeric_array(str)
      str.split.map { numeric_value(_1) }
    end

    def self.string_array(str)
      str.scan(/"([^"]*)"/).flatten
    end

    def self.numeric_value(str)
      return str.to_f if str.match?(/^\d+\.$/)

      Float(str)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
automotive-ecu-0.1.4 lib/ecu/interfaces/dcm/property_parser.rb
automotive-ecu-0.1.3 lib/ecu/interfaces/dcm/property_parser.rb