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
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Matrix

Returns a new instance of Matrix.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/csa/ccm/matrix.rb', line 14

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

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

  @control_domains ||= {}

  self
end

Class Method Details

.from_xlsx(version, input_file) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/csa/ccm/matrix.rb', line 98

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

  all_rows = matrix.worksheet.sheet_data.rows
  start_row = 4 # FIXME add some basic logic to calculate it

  last_control_domain = nil
  last_control_id = nil
  last_control_specification = nil

  worksheet = matrix.worksheet

  row_number = start_row
  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,
          name: row.control_domain_name
        )

      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,
        name: row.control_name,
        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
    puts question.to_hash
    control.questions[row.question_id] = Question.new(id: row.question_id, content: row.question_content)
  end

  matrix
end

Instance Method Details

#metadataObject



41
42
43
44
45
46
47
# File 'lib/csa/ccm/matrix.rb', line 41

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

#row(i) ⇒ Object



94
95
96
# File 'lib/csa/ccm/matrix.rb', line 94

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

#source_fileObject



28
29
30
# File 'lib/csa/ccm/matrix.rb', line 28

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

#titleObject



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

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

#to_file(filename) ⇒ Object



180
181
182
183
184
# File 'lib/csa/ccm/matrix.rb', line 180

def to_file(filename)
  File.open(filename,"w") do |file|
    file.write(to_hash.to_yaml)
  end
end

#to_hashObject



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/csa/ccm/matrix.rb', line 168

def to_hash
  {
    "ccm" => {
      "metadata" => .to_hash,
      "control_domains" => control_domains.inject([]) do |acc, (k, v)|
          acc << v.to_hash
          acc
        end
    }
  }
end

#worksheetObject



32
33
34
# File 'lib/csa/ccm/matrix.rb', line 32

def worksheet
  workbook.worksheets.first
end