Class: BatchLoad::Import::AssertedDistributions

Inherits:
BatchLoad::Import show all
Defined in:
lib/batch_load/import/asserted_distributions.rb

Instance Attribute Summary collapse

Attributes inherited from BatchLoad::Import

#create_attempted, #csv, #errors, #file, #file_errors, #import_level, #processed, #processed_rows, #project, #project_id, #successful_rows, #total_data_lines, #total_lines, #user, #user_header_map, #user_id

Instance Method Summary collapse

Methods inherited from BatchLoad::Import

#all_objects, #create, #create_attempted?, #import_level_ok?, #line_strict_level_ok?, #processed?, #ready_to_create?, #save_order, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

#initialize(data_origin: nil, **args) ⇒ AssertedDistributions

Returns a new instance of AssertedDistributions.

Parameters:

  • args (Hash)


10
11
12
13
14
# File 'lib/batch_load/import/asserted_distributions.rb', line 10

def initialize(data_origin: nil, **args)
  @asserted_distributions = {}
  @data_origin            = data_origin
  super(**args)
end

Instance Attribute Details

#asserted_distributionsObject

Returns the value of attribute asserted_distributions.



5
6
7
# File 'lib/batch_load/import/asserted_distributions.rb', line 5

def asserted_distributions
  @asserted_distributions
end

#data_originObject

Returns the value of attribute data_origin.



7
8
9
# File 'lib/batch_load/import/asserted_distributions.rb', line 7

def data_origin
  @data_origin
end

Instance Method Details

#buildBoolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/batch_load/import/asserted_distributions.rb', line 55

def build
  if valid?
    build_asserted_distributions
    @processed = true
  end
end

#build_asserted_distributionsInteger

when this routine is invoked, the following class variables must be instantiated:

@project_id
@user
@data_origin: one of:
                    ["gadm", "SFG", "ne_states", "country_names_and_code_elements",
                      "ne_countries", "tdwg_l3", "tdwg_l2", "tdwg_l1", "tdwg_l4"]

Returns:

  • (Integer)


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
# File 'lib/batch_load/import/asserted_distributions.rb', line 23

def build_asserted_distributions
  i = 1
  csv.each do |row|
    i += 1

    row.push('project_id' => @project_id)

    # TODO: FIX! THIS!
    # WAS: next if row.empty? || row.all? { |h, v| v.nil? || v.length == "" }
    next if row.empty?

    row.push('project_id' => @project_id)

    rp = BatchLoad::RowParse.new
    @processed_rows[i] = rp

    o = BatchLoad::ColumnResolver.otu(row)
    s = BatchLoad::ColumnResolver.source(row)
    g = BatchLoad::ColumnResolver.geographic_area(row, @data_origin)

    if o.resolvable? && s.resolvable? && g.resolvable?
      rp.objects[:asserted_distributions] = [AssertedDistribution.new(otu: o.item, source: s.item, geographic_area: g.item, project_id: @project_id, by: @user)]
    else
      rp.parse_errors += o.error_messages unless o.resolvable?
      rp.parse_errors += g.error_messages unless g.resolvable?
      rp.parse_errors += s.error_messages unless s.resolvable?
    end
  end
  @total_lines = i - 1
end