Class: BatchLoad::Import::TaxonifiToTaxonworks

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

Overview

Batch loading of CSV formatted taxon names via Taxonifi

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, #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(nomenclature_code: nil, parent_taxon_name_id: nil, also_create_otu: false, **args) ⇒ TaxonifiToTaxonworks

Returns a new instance of TaxonifiToTaxonworks.

Parameters:

  • args (Hash)


25
26
27
28
29
30
31
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 25

def initialize(nomenclature_code: nil, parent_taxon_name_id: nil, also_create_otu: false, **args)
  @nomenclature_code = nomenclature_code
  @also_create_otu = also_create_otu
  @parent_taxon_name_id = parent_taxon_name_id

  super(**args)
end

Instance Attribute Details

#also_create_otuObject

Whether to create an OTU as well



19
20
21
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 19

def also_create_otu
  @also_create_otu
end

#name_collectionObject

The Taxonifi Name collection



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

def name_collection
  @name_collection
end

#nomenclature_codeSymbol

Returns:

  • (Symbol)


16
17
18
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 16

def nomenclature_code
  @nomenclature_code
end

#parent_taxon_nameTaxonName

Returns:



10
11
12
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 10

def parent_taxon_name
  @parent_taxon_name
end

#parent_taxon_name_idObject

The id of the parent taxon name, computed automatically as Root if not provided



13
14
15
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 13

def parent_taxon_name_id
  @parent_taxon_name_id
end

#project_idObject

Required to handle some defaults



22
23
24
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 22

def project_id
  @project_id
end

Instance Method Details

#buildBoolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 56

def build
  if valid?
    build_name_collection
    build_protonyms
    @processed = true
  end
end

#build_name_collectionObject (protected)



66
67
68
69
70
71
72
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 66

def build_name_collection
  begin
    @name_collection ||= ::Taxonifi::Lumper.create_name_collection(csv: csv)
  rescue Taxonifi::Assessor::RowAssessor::RowAssessorError => e
    @file_errors.push 'Error assessing a row of data in the inputfile.'
  end
end

#build_protonymsInteger (protected)

Returns:

  • (Integer)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 75

def build_protonyms
  if name_collection.nil?
    @file_errors.push 'No names were readable in the file.'
    return
  end

  parents = {}

  total_lines = 0

  name_collection.collection.each do |n|
    i  = n.row_number + 1
    rp = nil

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

    p = Protonym.new(
      name: n.name,
      year_of_publication: n.year.to_s,
      rank_class: Ranks.lookup(nomenclature_code, n.rank),
      by: @user,
      also_create_otu: also_create_otu,
      project: @project,
      verbatim_author: (n.parens ? n.author_with_parens : nil),
      taxon_name_authors_attributes: taxon_name_authors_hash(n)
    )

    p.parent = (n.parent.nil? ? parent_taxon_name : parents[n.parent.id])

    rp.objects[:protonyms] ||= []
    rp.objects[:protonyms].push(p)

    parents[n.id] = p

    total_lines = i if total_lines < i
  end

  @total_data_lines = total_lines

  true
end

#taxon_name_authors_hash(taxonifi_name) ⇒ Array (protected)

Parameters:

Returns:

  • (Array)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/batch_load/import/taxonifi_to_taxonworks.rb', line 124

def taxon_name_authors_hash(taxonifi_name)
  author_attributes = []
  taxonifi_name.authors.each_with_index do |a,i|
    suffix = a.suffix.join(' ') if !a.suffix.nil?
    author_attributes.push({
      last_name: a.last_name,
      first_name: a.first_name,
      prefix: a.initials_string,
      suffix: suffix,
    })
  end
  author_attributes
  # author_attributes.reverse
end