Module: BatchLoad::ColumnResolver

Defined in:
lib/batch_load/column_resolver.rb,
lib/batch_load/column_resolver/result.rb

Overview

Stores the attributes of a Column Resolver results

Defined Under Namespace

Classes: Result

Class Method Summary (collapse)

Class Method Details

+ (BatchLoad::ColumnResolver::Result) collection_object_by_identifier(columns)

Parameters:

  • columns (Hash)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/batch_load/column_resolver.rb', line 36

def collection_object_by_identifier(columns)
  r = BatchLoad::ColumnResolver::Result.new

  # find a namespace (ns1) with a short_name of row[headers[0]] (id1)
  # find a collection_object which has an identifier which has a namespace (ns1), and a cached of
  # (ns1.short_name + ' ' + identifier.identifier)
  # ns1    = Namespace.where(short_name: id1).first

  ident_text = columns['catalog_number']
  ident_text = columns['collection_object_identifier'] if ident_text.blank?

  ident_text = "#{columns['collection_object_identifier_namespace_short_name']}" + ' ' +
    " #{columns['collection_object_identifier_identifier']}".strip if ident_text.blank?

  if ident_text.blank?
    r.error_messages << 'No column combination suitable for collection object resolution was provided.'
  else
    r.assign CollectionObject.joins(:identifiers).where(identifiers: {cached: ident_text}).to_a
    r.error_messages << "No collection object with cached identifier '#{ident_text}' exists." if r.no_matches?
  end
  r
end

+ (BatchLoad::ColumnResolver::Result) geographic_area(columns, data_origin = nil)

rubocop:disable Metrics/MethodLength

Parameters:

  • columns (Hash)
  • data_origin (String) (defaults to: nil)

Returns:



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
121
122
123
124
125
# File 'lib/batch_load/column_resolver.rb', line 86

def geographic_area(columns, data_origin = nil)
  r = BatchLoad::ColumnResolver::Result.new

  if columns['geographic_area_id']
    begin
      r.assign(GeographicArea.find(columns['geographic_area_id']))
    rescue ActiveRecord::RecordNotFound
      r.error_messages << "No Geographic area with id #{columns['source_id']} exists."
    end
  elsif columns['geographic_area_name']
    search_list = columns['geographic_area_name']
    # @tuckerjd - fix the data_origin logic
    if data_origin.blank?
      data_origin = 'blank'
      r.assign(GeographicArea.where(name: search_list).limit(10).to_a)
    else
      r.assign(GeographicArea.where(name: search_list, data_origin: data_origin).limit(10).to_a)
    end


    # @tuckerjd - tweak as necessary
    r.error_messages <<  "Multiple matches to '#{search_list}' (data_origin: #{data_origin}) were found." if r.multiple_matches?

  elsif columns['country'] || columns['state'] || columns['county']
    search_list = [columns['country'], columns['state'], columns['county']].compact.join(', ')

    # @tuckerjd - tweak as necessary to include data_origin
    if data_origin.blank?
      data_origin = 'blank'
      r.assign(GeographicArea.with_name_and_parent_names([columns['county'], columns['state'], columns['country']].compact).to_a)
    else
      r.assign(GeographicArea.where(data_origin: data_origin).with_name_and_parent_names([columns['county'], columns['state'], columns['country']].compact).to_a)
    end
    # @tuckerjd - tweak as necessary
    r.error_messages << "'#{search_list}' (data_origin: #{data_origin}): Geographic area was not determinable." if r.no_matches?
    r.error_messages << "Multiple matches to '#{search_list}' (data_origin: #{data_origin}) were found." if r.multiple_matches?
  end

  r
end

+ (BatchLoad::ColumnResolver::Result) otu(columns)

Parameters:

  • columns (Hash)

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/batch_load/column_resolver.rb', line 11

def otu(columns)
  r = BatchLoad::ColumnResolver::Result.new

  if columns['otu_id']
    begin
      r.assign Otu.find(columns['otu_id'])
    rescue => e  # ActiveRecord::RecordNotFound
      r.error_messages << "No OTU with id #{columns['otu_id']} exists."
    end
  elsif columns['otu_name']
    r.assign Otu.where(name: columns['otu_name'], project_id: columns['project_id']).limit(10).to_a
    r.error_messages << "Multiple OTUs matched the name '#{columns['otu_name']}'." if r.multiple_matches?
    r.error_messages << "No OTU with name '#{columns['otu_name']}' exists." if r.no_matches?
  elsif columns['taxon_name']
    r.assign Otu.joins(:taxon_names).where(taxon_names: {cached: columns['taxon_name']}, project_id: columns['project_id']).limit(10).to_a
    r.error_messages << "Multiple OTUs matched the taxon name '#{columns['taxon_name']}'." if r.multiple_matches?
  else
    r.error_messages << 'No column suitable for OTU resolution was provided.'
  end

  r
end

+ (BatchLoad::ColumnResolver::Result) source(columns)

Parameters:

  • columns (Hash)

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/batch_load/column_resolver.rb', line 61

def source(columns)
  r = BatchLoad::ColumnResolver::Result.new

  if columns['source_id']
    begin
      r.assign Source.find(columns['source_id'])
    rescue ActiveRecord::RecordNotFound
      r.error_messages << "No source with id #{columns['source_id']} exists."
    end
  elsif columns['doi']
    r.assign Source.where(doi: columns['doi']).limit(10).to_a # identifier is cached here, so we don't have to join Identifers
    r.error_messages << "Multiple matches to the DOI '#{column['doi']}' were found." if r.multiple_matches?
  elsif columns['citation']
    r.assign Source.where(cached: columns['citation']).limit(10).to_a
    r.error_messages << "Multiple matches to the citation '#{column['citation']}' were found." if r.multiple_matches?
    r.error_messages << "No source with citation '#{columns['citation']}' exists." if r.no_matches?
  end

  r
end