Class: Classification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/classification.rb

Class Method Summary collapse

Class Method Details

.import_from_tsv(filename) ⇒ Object



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
# File 'app/models/classification.rb', line 23

def self.import_from_tsv(filename)
  logger.info "import_from_tsv start"

  cnt = 0
  ndc9 = ClassificationType.where(name: "ndc9").first
  unless ndc9
    logger.fatal "ndc9 not found"
    raise ActiveRecord::RecordNotFound("ndc9 not found")
  end

  Classification.destroy_all(classification_type_id: ndc9.id)

  open(filename) do |file|
    file.each do |line|
      if cnt == 0
      else
        rows = line.split("\t")
        if rows[9].present?
          ndcs = rows[9].split(';')
          ndcs.each do |ndc|
            logger.debug "ndc=#{ndc} ndlsh=#{rows[0]}"
            parent_id = nil
            category = rows[0].strip
            identifier = ndc.strip
            c = Classification.where(classification_type_id: ndc9.id, classifiation_identifier: identifier)
            if c.blank?
              c = Classification.new
              c.parent_id = parent_id
              c.category = category
              c.classifiation_identifier = identifier
              c.classification_type = ndc9
              c.save!
            end
          end
        end
      end
      cnt = cnt + 1
    end
  end

  logger.info "import_from_tsv end"
end