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

+ (Object) collection_object_by_identifier(columns)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/batch_load/column_resolver.rb', line 32

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

+ (Object) geographic_area(columns, data_origin = nil)



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
# File 'lib/batch_load/column_resolver.rb', line 76

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

+ (Object) otu(columns)



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

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

+ (Object) source(columns)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/batch_load/column_resolver.rb', line 55

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