Class: Codinginfo

Inherits:
Object show all
Defined in:
lib/codinginfo.rb

Defined Under Namespace

Classes: Cvar, Variant

Constant Summary collapse

CVAR_PREFIX_REGEXP =
/^CVAR_[A-Za-z0-9_]+_\d+\./
PARAM_ASSIGNMENT_HEADERS =
{ name: /Parameter/, cvar: /Schalter/, package: /Komponente/ }

Instance Method Summary collapse

Constructor Details

#initialize(filepath, filter) ⇒ Codinginfo



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/codinginfo.rb', line 52

def initialize(filepath, filter)
  @doc         = SimpleXlsxReader.open(filepath)
  @filter      = filter
  @headers     = []
  @variants    = []
  @assignments = {}

  overview_sheet
    .rows
    .each do |row|
      next unless headers_parsed?(row)
      next unless filter.match?(@headers, row)

      @variants << Variant.new(@headers, row)
    end

  param_assignment_sheet
    .rows
    .each(headers: PARAM_ASSIGNMENT_HEADERS) do |row|
      next if row[:cvar].nil?

      @assignments[row[:name]] = Cvar.new(name: row[:cvar], package: row[:package])
    end
end

Instance Method Details

#add_cvar_prefix(name, variant) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/codinginfo.rb', line 139

def add_cvar_prefix(name, variant)
  variant
    .cvars
    .find { _1 == @assignments[name] }
    .then { _1.combine(@assignments[name]) }
    .then { _1.label_prefix + name }
end

#cvar_coded?(label) ⇒ Boolean



127
128
129
# File 'lib/codinginfo.rb', line 127

def cvar_coded?(label)
  label.name.match?(CVAR_PREFIX_REGEXP)
end

#flatten_list(list) ⇒ Object



120
121
122
123
124
125
# File 'lib/codinginfo.rb', line 120

def flatten_list(list)
  list
    .select { !cvar_coded?(_1) || has_matching_cvar?(_1) }
    .map_int { _1.with(name: remove_cvar_prefix(_1.name)) }
    .with(headers: variants_description)
end

#has_cvar_assignment?(label) ⇒ Boolean



131
132
133
# File 'lib/codinginfo.rb', line 131

def has_cvar_assignment?(label)
  @assignments.key?(label.name)
end

#has_matching_cvar?(label) ⇒ Boolean



135
136
137
# File 'lib/codinginfo.rb', line 135

def has_matching_cvar?(label)
  variant.cvars.any? { label.name.match?(_1.label_prefix) }
end

#headers_parsed?(row) ⇒ Boolean



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/codinginfo.rb', line 151

def headers_parsed?(row)
  return true if @headers.any?

  return false unless row[1]&.match?(/SWFK-ID/)
  return false unless row[1]&.match?(/CVAR_BeguData/)
  return false unless row[2]&.match?(/Kommentar/)

  @headers = row
    .map(&:chomp)
    .map(&:lstrip)
    .map(&:strip)
    .map { _1.sub(/\n.*/, "") }
    .tap { _1[0] = :typestr }
    .tap { _1[1] = :swfk_id }
    .tap { _1[4] = :typekey }
    .tap { _1[6] = :devkey }
end

#overview_sheetObject



77
78
79
80
81
82
# File 'lib/codinginfo.rb', line 77

def overview_sheet
  @doc
    .sheets
    .find { _1.name == "Gesamtübersicht" }
    .tap { fail "Cannot find sheet Gesamtübersicht" if _1.nil? }
end

#param_assignment_sheetObject



84
85
86
87
88
89
# File 'lib/codinginfo.rb', line 84

def param_assignment_sheet
  @doc
    .sheets
    .find { _1.name == "Parameterdefinition" }
    .tap { fail "Cannot find sheet Parameterdefinition" if _1.nil? }
end

#remove_cvar_prefix(name) ⇒ Object



147
148
149
# File 'lib/codinginfo.rb', line 147

def remove_cvar_prefix(name)
  name.sub(CVAR_PREFIX_REGEXP, "")
end

#unflatten_list(list) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/codinginfo.rb', line 106

def unflatten_list(list)
  fail "No variants found matching #{@filter}" if @variants.empty?

  Ecu::LabelList.new \
    list.flat_map { |label|
      if has_cvar_assignment?(label)
        @variants.map { label.with(name: add_cvar_prefix(label.name, _1)) }
      else
        label
      end
    }.uniq,
    variants_description
end

#variantObject



91
92
93
94
95
96
# File 'lib/codinginfo.rb', line 91

def variant
  fail "No variant matching #{@filter} found!" if @variants.empty?
  fail "More than one variant matching #{id} found!" if @variants.size > 1

  @variants.first
end

#variants_descriptionObject



98
99
100
101
102
103
104
# File 'lib/codinginfo.rb', line 98

def variants_description
  [
    "Created by dcm version #{Dcm::VERSION}",
    "The following swfk_ids were matched:",
    *@variants.map { "  " + _1.description }
  ]
end