Class: Csa::Ccm::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/csa/ccm/matrix.rb

Defined Under Namespace

Classes: Row

Constant Summary collapse

ATTRIBS =
%i[
  version title source_file workbook source_path control_domains answers
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Matrix

Returns a new instance of Matrix.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/csa/ccm/matrix.rb', line 21

def initialize(options = {})
  options.each_pair do |k, v|
    send("#{k}=", v)
  end

  if source_path
    @workbook = RubyXL::Parser.parse(source_path)

    parse_version if version.nil?
  end

  @control_domains ||= {}
  @answers ||= []

  self
end

Instance Attribute Details

#workbookObject (readonly)

Returns the value of attribute workbook.



59
60
61
# File 'lib/csa/ccm/matrix.rb', line 59

def workbook
  @workbook
end

Class Method Details

.fill_answers(answers_yaml_path, template_xslt_path, output_xslt_path) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/csa/ccm/matrix.rb', line 228

def self.fill_answers(answers_yaml_path, template_xslt_path, output_xslt_path)
  ccm = YAML.safe_load(File.read(answers_yaml_path, encoding: 'UTF-8'))['ccm']
  answers = ccm['answers']
  answers_hash = Hash[*answers.collect { |a| [a['question-id'], a] }.flatten]
  answers_version = ccm['metadata']['version']
  template_version = version_from_filepath(template_xslt_path)

  unless template_version == answers_version
    raise "Template XLSX & answers YAML version missmatch #{template_version} vs. #{answers_version}"
  end

  matrix = Matrix.new(
    version: template_version,
    source_path: template_xslt_path
  )

  worksheet = matrix.worksheet
  all_rows = worksheet.sheet_data.rows

  start_row = get_start_row(matrix.version)
  max_row_number = all_rows.length - 1

  (start_row..max_row_number).each do |row_number|
    question_id = worksheet[row_number][2].value

    next unless answers_hash.key?(question_id)

    answer = answers_hash[question_id]
    answer_value = answer['answer']

    answer_col = case answer_value
                 when 'yes', true
                   5
                 when 'no', false
                   6
                 when 'NA'
                   7
                 end

    worksheet[row_number][answer_col].change_contents(answer['notes'])
  end

  matrix.workbook.write(output_xslt_path)
  worksheet
end

.from_xlsx(input_file) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/csa/ccm/matrix.rb', line 152

def self.from_xlsx(input_file)
  matrix = Matrix.new(
    source_path: input_file
  )

  all_rows = matrix.worksheet.sheet_data.rows

  start_row = get_start_row(matrix.version)
  max_row_number = all_rows.length - 1

  # We loop over all Questions
  (start_row..max_row_number).each do |row_number|
    # puts "looping row #{row_number}"
    row = matrix.row(row_number)
    # Skip row if there is no question-id
    # puts"row #{row.question_id}"
    # require 'pry'
    # binding.pry
    next if row.question_id.nil?

    # puts"domain_id #{row.control_domain_id}"

    domain_id = row.control_domain_id
    unless domain_id.nil?

      control_domain = matrix.control_domains[domain_id] ||
                       ControlDomain.new(
                         id: row.control_domain_id,
                         title: row.control_domain_title
                       )

      # puts"control_domain #{control_domain.to_hash}"

      # Store the Control Domain
      matrix.control_domains[domain_id] = control_domain
    end

    control_id = row.control_id
    unless control_id.nil?

      control_domain = matrix.control_domains[domain_id]
      control = control_domain.controls[control_id] || Control.new(
        id: row.control_id,
        title: row.control_title,
        specification: row.control_spec
      )

      # puts"control #{control.to_hash}"
      # Store the Control
      control_domain.controls[control_id] = control
    end

    question = matrix.control_domains[domain_id].controls[control_id]
    # Store the Question
    # putsquestion.to_hash
    control.questions[row.question_id] = Question.new(id: row.question_id, content: row.question_content)

    answer = if row.answer_na
               'NA'
             elsif row.answer_no
               'no'
             elsif row.answer_yes
               'yes'
    end

    matrix.answers << Answer.new(
      question_id: row.question_id,
      control_id: control_id,
      answer: answer,
      comment: row.comment
    )
  end

  matrix
end

.get_start_row(version) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/csa/ccm/matrix.rb', line 136

def self.get_start_row(version)
  case version
  when '1.0'
    2
  when '1.1'
    3
  else
    # '3.0.1' and assume beyond
    4
  end
end

.version_from_filepath(input_file) ⇒ Object



148
149
150
# File 'lib/csa/ccm/matrix.rb', line 148

def self.version_from_filepath(input_file)
  input_file[/(?<=v)[0-9\.]*(?=-)/] || 'unknown'
end

Instance Method Details

#metadataObject



66
67
68
69
70
71
72
# File 'lib/csa/ccm/matrix.rb', line 66

def 
  {
    'version' => version,
    'title' => title,
    'source_file' => source_file
  }
end

#parse_versionObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/csa/ccm/matrix.rb', line 42

def parse_version
  title_prefix = 'consensus assessments initiative questionnaire v'
  first_row = workbook[0][0]

  if first_row[2].value.downcase.start_with? title_prefix # version v3.0.1
    self.version = first_row[2].value.downcase[title_prefix.length..-1]
  elsif first_row[0].value.downcase.start_with? title_prefix # version v1.1
    self.version = first_row[0].value.downcase[title_prefix.length..-1]
  else # version 1.0
    self.version = workbook[1][0][0].value[/(?<=Version )(\d+\.?)+(?= \()/]
  end
end

#row(i) ⇒ Object



132
133
134
# File 'lib/csa/ccm/matrix.rb', line 132

def row(i)
  Row.new(worksheet[i])
end

#source_fileObject



38
39
40
# File 'lib/csa/ccm/matrix.rb', line 38

def source_file
  @source_file || File.basename(source_path)
end

#titleObject



61
62
63
64
# File 'lib/csa/ccm/matrix.rb', line 61

def title
  worksheet = workbook.worksheets.first
  worksheet[0][2].value
end

#to_answers_file(filename) ⇒ Object



302
303
304
305
306
# File 'lib/csa/ccm/matrix.rb', line 302

def to_answers_file(filename)
  File.open(filename, 'w') do |file|
    file.write(to_answers_hash.to_yaml)
  end
end

#to_answers_hashObject



285
286
287
288
289
290
291
292
293
294
# File 'lib/csa/ccm/matrix.rb', line 285

def to_answers_hash
  {
    'ccm' => {
      'metadata' => .to_hash,
      'answers' => answers.each_with_object([]) do |v, acc|
                     acc << v.to_hash
                   end
    }
  }
end

#to_control_file(filename) ⇒ Object



296
297
298
299
300
# File 'lib/csa/ccm/matrix.rb', line 296

def to_control_file(filename)
  File.open(filename, 'w') do |file|
    file.write(to_control_hash.to_yaml)
  end
end

#to_control_hashObject



274
275
276
277
278
279
280
281
282
283
# File 'lib/csa/ccm/matrix.rb', line 274

def to_control_hash
  {
    'ccm' => {
      'metadata' => .to_hash,
      'control_domains' => control_domains.each_with_object([]) do |(_k, v), acc|
                             acc << v.to_hash
                           end
    }
  }
end

#worksheetObject



55
56
57
# File 'lib/csa/ccm/matrix.rb', line 55

def worksheet
  workbook.worksheets.first
end