Class: Importer::Consolidated
- Inherits:
-
Object
- Object
- Importer::Consolidated
- Defined in:
- lib/importer/consolidated.rb
Overview
Importer for specialized CSV document format
Expected columns are: 0-4: Company control index, unused, title, description-part-1, description-part-2 5-6: Program 1: description, slug(s) 7-8: Program 2: description, slug(s) 9-10: Program 3: description, slug(s)
Header format (by column) (some unused): 0: "#" 2,3: Company title, slug 5,6: Program 1 description, slug 7,8: Program 1 description, slug 9,10: Program 1 description, slug
Instance Attribute Summary (collapse)
-
- (Object) company_controls
Returns the value of attribute company_controls.
Instance Method Summary (collapse)
-
- (Object) add_control(program, slugs, description)
For each slug, create the Control object and mappings.
- - (Object) add_program(slug, title, is_company = false)
-
- (Consolidated) initialize
constructor
A new instance of Consolidated.
-
- (Object) run_csv(csv_string)
Create the default Program objects and iterate rows, creating Controls, Sections, and associated mappings.
- - (Object) run_file(filename)
Constructor Details
- (Consolidated) initialize
A new instance of Consolidated
22 23 24 25 |
# File 'lib/importer/consolidated.rb', line 22 def initialize @programs = [] @company_controls = [] end |
Instance Attribute Details
- (Object) company_controls
Returns the value of attribute company_controls
20 21 22 |
# File 'lib/importer/consolidated.rb', line 20 def company_controls @company_controls end |
Instance Method Details
- (Object) add_control(program, slugs, description)
For each slug, create the Control object and mappings
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 |
# File 'lib/importer/consolidated.rb', line 100 def add_control(program, slugs, description) slugs.split(',').map(&:strip).each do |slug| slug = "#{program.slug}.#{slug}" control = find_or_create( Control, { :slug => slug }, { :description => description, :title => slug, :program => program }) section = Section.where(:slug => slug).first section ||= Section.create_in_tree( :slug => slug, :title => slug, :program => program) control_section = find_or_create( ControlSection, :control_id => control.id, :section_id => section.id) control_control = find_or_create( ControlControl, { :control_id => @company_controls.last.id, :implemented_control_id => control.id }) end end |
- (Object) add_program(slug, title, is_company = false)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/importer/consolidated.rb', line 81 def add_program(slug, title, is_company=false) raise ImportError, "Invalid header row" if !slug.present? program = find_or_create( Program, { :slug => slug.strip }, { :title => title.present? ? title.strip : slug, :company => is_company }) section = find_or_create( Section, { :slug => slug.strip }, { :title => title.present? ? title.strip : slug, :program => program }) @programs.push(program) end |
- (Object) run_csv(csv_string)
Create the default Program objects and iterate rows, creating Controls, Sections, and associated mappings
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/importer/consolidated.rb', line 35 def run_csv(csv_string) headers_seen = false CSV.parse(csv_string) do |row| if !headers_seen # Still waiting for headers # Check for the '#' column if row[0] && row[0].match(/#/) headers_seen = true # Retrieve Program names and slugs from headers of columns add_program(row[3], row[2], true) # Required add_program(row[6], row[5]) if row[5].present? add_program(row[8], row[7]) if row[7].present? add_program(row[10], row[9]) if row[9].present? end else # Headers passed, handle row-by-row import # Company controls if row[0] program = @programs[0] slug = "#{program.slug}.#{row[0]}" desc = "Objective:\n#{row[3]}\n\nControl:\n#{row[4]}" control = find_or_create( Control, { :slug => slug }, { :title => row[2], :description => desc, :program => program }) @company_controls.push(control) end # Regulatory Controls add_control(@programs[1], row[6], row[5]) if row[5] && row[6] != "N/A" add_control(@programs[2], row[8], row[7]) if row[7] && row[8] != "N/A" add_control(@programs[3], row[10], row[9]) if row[9] && row[10] != "N/A" end end end |
- (Object) run_file(filename)
27 28 29 30 31 |
# File 'lib/importer/consolidated.rb', line 27 def run_file(filename) open(filename, 'r:utf-8') do |file| run_csv(file.read()) end end |