Sha256: 71a52885a1df891ba1e6bdd5a7b6f261dc376dcb3e84f0d49e68299485b8c86c

Contents?: true

Size: 916 Bytes

Versions: 10

Compression:

Stored size: 916 Bytes

Contents

class Ecu
  class MalformedDcmError < StandardError
    attr_reader :lineno, :filecontent
    def initialize(msg, lineno, filecontent)
      super(msg)
      @lineno      = lineno
      @filecontent = filecontent
    end

    def to_s = "Malformed DCM: #{super} near line ##{lineno}"
    def to_str = to_s

    def context
      filecontent
        .lines
        .each
        .with_index(1)
        .map { |line, n| present(line, n, n == lineno) }
        .then { _1[ctx_startline..ctx_endline] }
    end

    def present(line, n, highlight)
      case highlight
        when true  then fmt(n) + " => | " + line
        when false then fmt(n) + "    | " + line
      end
    end

    def ctx_startline = [0, lineno - 7].max
    def ctx_endline   = @endline ||= [filecontent.lines.count - 1, lineno + 3].min
    def fmt_str       = "%#{(ctx_endline + 1).to_s.length}d"
    def fmt(n)        = fmt_str % n
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
automotive-ecu-0.1.9 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.8 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.7 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.6 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.5 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.4 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.3 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.2 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.1 lib/ecu/interfaces/dcm/malformed_dcm_error.rb
automotive-ecu-0.1.0 lib/ecu/interfaces/dcm/malformed_dcm_error.rb