Class: Importer::Regulation

Inherits:
Object
  • Object
show all
Defined in:
lib/importer/regulation.rb

Instance Method Summary (collapse)

Constructor Details

- (Regulation) initialize

A new instance of Regulation



7
8
# File 'lib/importer/regulation.rb', line 7

def initialize
end

Instance Method Details

- (Object) run_csv(csv_string)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/importer/regulation.rb', line 16

def run_csv(csv_string)
  program = nil

  headers_seen = false

  CSV.parse(csv_string) do |row|
    # Iterate until we see the header row (denoted by 'slug' in first
    # column), then start row-by-row imports.

    if !headers_seen
      # Still waiting for headers

      # Check for the 'Slug' header
      if row[0] && row[0].match(/slug/i)
        headers_seen = true
        next

      # We're still in metadata mode
      elsif row[0] && row[0].match(/program/i)
        slug = row[1].strip
        title = row[2].present? ? row[2].strip : slug

        program = Program.where(:slug => slug).first
        program ||= Program.create(
          :slug => slug,
          :title => title)

        section = Section.create_in_tree(
          :slug => slug,
          :title => title,
          :program => program)
      end

    else
      # Headers passed, now doing row-by-row import

      title = row[1]
      if !title.present?
        title = row[2][0,80]
        title += "..." if row[2].size > 80
      end

      description = row[2].strip

      # For now, append notes to the description field
      description += "\n\nNotes:\n#{row[3].strip}" if row[3].present?

      section = Section.where(:slug => row[0].strip).first
      section ||= Section.create_in_tree(
        :slug => row[0].strip,
        :title => title,
        :description => description,
        :program => program)
    end
  end

end

- (Object) run_file(filename)



10
11
12
13
14
# File 'lib/importer/regulation.rb', line 10

def run_file(filename)
  open(filename, 'r:utf-8') do |file|
    run_csv(file.read)
  end
end