Sha256: d0cb1a40441a4984363ff4a2fa32e6625ec24cefaa61a58fcef6b304ccf7af14

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

require_relative "signal"

require_relative "../interfaces/lab/signal_list"
require_relative "../interfaces/a2l/signal_list"
require_relative "../interfaces/dbc/signal_list"

class Ecu
  class SignalList
    include Enumerable

    def self.from_file(file_path)
      case File.extname(file_path)
      when ".lab" then self.from_lab(File.read_encoded(file_path))
      when ".a2l" then self.from_a2l(File.read_encoded(file_path))
      when ".dbc" then self.from_dbc(File.read_encoded(file_path))
      else fail "Unknown file extension: #{file_path}!"
      end
    end

    attr_accessor :signals, :headers, :subheaders
    def initialize(signals=[], headers=[], subheaders=[])
      @signals    = signals
      @headers    = headers
      @subheaders = subheaders
    end

    def each(&blk)
      @signals.each(&blk)
    end

    def map_int(&blk)
      self.class.new(@signals.map(&blk), @headers, @subheaders)
    end

    def empty?
      @signals.empty?
    end

    def task(task_identifier)
      task_identifier = tasks[task_identifier] if task_identifier.is_a? Integer
      select { |signal| signal.task == task_identifier }
    end

    def select(&blk); self.class.new(@signals.select(&blk), @headers, @subheaders) end
    def reject(&blk); self.class.new(@signals.reject(&blk), @headers, @subheaders) end
    def sort_by(&blk); self.class.new(@signals.sort_by(&blk), @headers, @subheaders) end

    def tasks
      @tasks ||= map(&:task).compact.uniq
    end

    def to_s
      "[#{map(&:to_s).join(", ")}]"
    end

    def uniq
      self.class.new(@signals.uniq)
    end

    %i(+ - &).each do |method|
      define_method(method) do |other|
        fail TypeError unless other.is_a?(self.class)
        self.class.new(@signals.public_send(method, other.signals))
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
automotive-ecu-0.1.9 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.8 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.7 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.6 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.5 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.4 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.3 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.2 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.1 lib/ecu/signals/signal_list.rb
automotive-ecu-0.1.0 lib/ecu/signals/signal_list.rb