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, #successful_rows, #total_data_lines, #total_lines, #user, #user_header_map

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?, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

- (AssertedDistributions) initialize(data_origin: nil, **args)

Returns a new instance of AssertedDistributions



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

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

Instance Attribute Details

- (Object) asserted_distributions

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

- (Object) data_origin

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

- (Object) build



52
53
54
55
56
57
# File 'lib/batch_load/import/asserted_distributions.rb', line 52

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

- (Object) build_asserted_distributions

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"]


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

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