Sha256: 1d7ea87ebfad7daa5c104c99c857bdab61714d5b316e1473689b2f8112dcc6e2

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 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)
      case str
      in /^\d+$/   then str.to_i
      in /^\d+\.$/ then str.to_f
      else              Float(str)
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
automotive-ecu-0.1.9 lib/ecu/interfaces/dcm/property_parser.rb
automotive-ecu-0.1.8 lib/ecu/interfaces/dcm/property_parser.rb
automotive-ecu-0.1.7 lib/ecu/interfaces/dcm/property_parser.rb
automotive-ecu-0.1.6 lib/ecu/interfaces/dcm/property_parser.rb
automotive-ecu-0.1.5 lib/ecu/interfaces/dcm/property_parser.rb