Class: Nppes::UpdatePack::Data

Inherits:
Base
  • Object
show all
Defined in:
lib/nppes/update_pack/data.rb

Instance Method Summary collapse

Methods inherited from Base

#parse, #split_row

Constructor Details

#initialize(data_file) ⇒ Data

Returns a new instance of Data.



4
5
6
# File 'lib/nppes/update_pack/data.rb', line 4

def initialize(data_file)
  @file = data_file
end

Instance Method Details

#proceedObject



8
9
10
11
12
# File 'lib/nppes/update_pack/data.rb', line 8

def proceed
  parse(@file) do |row|
    proceed_row(row)
  end
end

#proceed_row(row, required_fields = RequiredFields) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nppes/update_pack/data.rb', line 14

def proceed_row(row, required_fields = RequiredFields)
  @fields = split_row(row)

  npi = Nppes::NpIdentifier.where(npi: @fields[0]).first_or_initialize

  required_fields.fields.each_pair { |k, v| npi.send("#{k}=", prepare_value(@fields, v)) }

  # for submodels
  required_fields.relations.each_pair do |k, v|
    v.each do |entity|
      relation = npi.send(k).new
      entity.each_pair {|name, num| relation.send("#{name}=", prepare_value(@fields, num))}
      unless relation.valid?
        npi.send(k).delete(relation)
        break
      end
    end
  end

  npi.save if npi.valid?
end