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, is_original_default: nil, **args) ⇒ AssertedDistributions

Returns a new instance of AssertedDistributions.

Parameters:

  • args (Hash)


14
15
16
17
18
19
# File 'lib/batch_load/import/asserted_distributions.rb', line 14

def initialize(data_origin: nil, is_original_default: nil, **args)
  @data_origin = data_origin
  @is_original_default =
    ActiveModel::Type::Boolean.new.cast(is_original_default) ? true : false
  super(**args)
end

Instance Attribute Details

#data_origin ⇒ Object

Returns the value of attribute data_origin.



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

def data_origin
  @data_origin
end

#is_original_default ⇒ Boolean

Returns whether a line's citation is original when its is_original is blank, unless its asserted distribution already has an original citation (in the database or from an earlier line).

Returns:

  • (Boolean) —

    whether a line's citation is original when its is_original is blank, unless its asserted distribution already has an original citation (in the database or from an earlier line)



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

def is_original_default
  @is_original_default
end

Instance Method Details

#add_citation(rp, cited, object, shape, source, pages:, is_original:) ⇒ Object (private)

The line's objects: a new asserted distribution created with the line's citation, or, when the pair already has one, just the citation.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/batch_load/import/asserted_distributions.rb', line 157

def add_citation(rp, cited, object, shape, source, pages:, is_original:)
  if cited.object
    rp.objects[:citations] = [
      detached_citation(cited.object, source, pages:, is_original:)
    ]
  else
    cited.object = AssertedDistribution.new(
      asserted_distribution_object: object,
      asserted_distribution_shape: shape,
      project_id: @project_id,
      by: @user
    )
    cited.object.citations.build(
      source:,
      pages:,
      is_original: is_original || nil,
      project_id: @project_id,
      by: @user
    )
    rp.objects[:asserted_distributions] = [cited.object]
  end
end

#already_cited_message(by) ⇒ Object (private)

Parameters:



136
137
138
139
140
141
142
143
# File 'lib/batch_load/import/asserted_distributions.rb', line 136

def already_cited_message(by)
  if by == BatchLoad::CitedObject::DATABASE
    'Source is already cited on the existing ' \
      'asserted distribution for this OTU and area.'
  else
    "Duplicates the OTU, area and source of line #{by}."
  end
end

#asserted_distributions_created ⇒ Integer

Returns:

  • (Integer)


82
83
84
# File 'lib/batch_load/import/asserted_distributions.rb', line 82

def asserted_distributions_created
  persisted_count(:asserted_distributions)
end

#build ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/batch_load/import/asserted_distributions.rb', line 74

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

#build_asserted_distributions ⇒ Integer

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)


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

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

    row.push('user_id' => @user_id)
    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.shape(row, @data_origin)
    original = BatchLoad::ColumnResolver.boolean(row, 'is_original')
    pages = row['pages']&.strip.presence

    rp.parse_errors += unresolved_messages(o, 'OTU')
    rp.parse_errors += unresolved_messages(g, 'geographic area')
    rp.parse_errors += unresolved_messages(s, 'source')
    rp.parse_errors += original.error_messages
    next if rp.has_parse_errors?

    cited = cited_object(o.item, g.item)
    source = s.item

    # The checkbox fills a blank is_original only where it can't make a
    # second original citation.
    is_original = original.item
    if is_original.nil?
      is_original = @is_original_default && cited.original_by.nil?
    end

    if (by = cited.cited_by(source))
      rp.parse_errors << already_cited_message(by)
    elsif is_original && (by = cited.original_by)
      rp.parse_errors << original_already_given_message(by)
    else
      add_citation(rp, cited, o.item, g.item, source, pages:, is_original:)
      cited.cite(i, source, is_original:)
    end
  end
  @total_lines = i
end

#citations_created ⇒ Integer

Returns each new asserted distribution is saved with its origin citation.

Returns:

  • (Integer) —

    each new asserted distribution is saved with its origin citation



88
89
90
# File 'lib/batch_load/import/asserted_distributions.rb', line 88

def citations_created
  asserted_distributions_created + persisted_count(:citations)
end

#cited_object(object, shape) ⇒ BatchLoad::CitedObject (private)

Returns for the object/shape pair; its object is the present asserted distribution in the database, or the one built by an earlier line.

Returns:

  • (BatchLoad::CitedObject) —

    for the object/shape pair; its object is the present asserted distribution in the database, or the one built by an earlier line



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/batch_load/import/asserted_distributions.rb', line 120

def cited_object(object, shape)
  key = [
    object.class.base_class.name, object.id,
    shape.class.base_class.name, shape.id
  ]
  cited_objects[key] ||= BatchLoad::CitedObject.new(
    AssertedDistribution.where(
      project_id: @project_id,
      asserted_distribution_object: object,
      asserted_distribution_shape: shape,
      is_absent: [false, nil]
    ).first
  )
end

#cited_objects ⇒ Hash (private)

Returns [object type, object id, shape type, shape id] => BatchLoad::CitedObject.

Returns:

  • (Hash) —

    [object type, object id, shape type, shape id] => BatchLoad::CitedObject



113
114
115
# File 'lib/batch_load/import/asserted_distributions.rb', line 113

def cited_objects
  @cited_objects ||= {}
end

#detached_citation(asserted_distribution, source, pages:, is_original:) ⇒ Citation (private)

The citation for the file line being built, on an existing asserted distribution (in the database, or built by an earlier line, and so saved first). The asserted distribution isn't told about the citation, so the citation can't affect that asserted distribution's validity or save; it's validated, saved, and reported on its own line. (The citation still validates the asserted distribution it cites.)

Returns:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/batch_load/import/asserted_distributions.rb', line 187

def detached_citation(asserted_distribution, source, pages:, is_original:)
  citation = Citation.new(
    citation_object: asserted_distribution,
    source:,
    pages:,
    is_original: is_original || nil,
    project_id: @project_id,
    by: @user
  )
  # Assigning citation_object adds the citation to the asserted
  # distribution's in-memory citations (has_many_inversing); undo that.
  # The citation is unsaved, so this only removes it from memory.
  asserted_distribution.citations.delete(citation)
  citation
end

#original_already_given_message(by) ⇒ Object (private)

Parameters:



146
147
148
149
150
151
152
153
# File 'lib/batch_load/import/asserted_distributions.rb', line 146

def original_already_given_message(by)
  if by == BatchLoad::CitedObject::DATABASE
    'The existing asserted distribution for this ' \
      'OTU and area already has an original citation.'
  else
    "Line #{by} already gives the original citation for this OTU and area."
  end
end

#persisted_count(type) ⇒ Integer (private)

Returns:

  • (Integer)


95
96
97
98
99
# File 'lib/batch_load/import/asserted_distributions.rb', line 95

def persisted_count(type)
  processed_rows.values.sum do |rp|
    Array(rp.objects[type]).count(&:persisted?)
  end
end

#unresolved_messages(result, name) ⇒ Array<String> (private)

Some resolvers leave nothing resolved without saying why (e.g. no source column filled in, or a DOI matching no source).

Parameters:

Returns:

  • (Array<String>)


105
106
107
108
109
# File 'lib/batch_load/import/asserted_distributions.rb', line 105

def unresolved_messages(result, name)
  return [] if result.resolvable?

  result.error_messages.presence || ["No #{name} could be resolved."]
end