Class: DatasetRecord::DarwinCore

Inherits:
DatasetRecord show all
Defined in:
app/models/dataset_record/darwin_core.rb

Direct Known Subclasses

Extension, Occurrence, Taxon

Defined Under Namespace

Classes: Extension, InvalidData, Occurrence, Taxon

Instance Attribute Summary

Attributes inherited from DatasetRecord

#metadata, #status

Instance Method Summary collapse

Methods inherited from DatasetRecord

#create_fields, #data_fields, #dataset_record_fields, #destroy_fields, #field_db_attributes, #fields_db_attributes, #frozen_fields?, #get_data_field, #initialize_data_fields, #set_data_field, #update_fields

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Method Details

#data_field_changed(index, value) ⇒ Object (private)

Re-implemented method from DatasetRecord



71
72
73
# File 'app/models/dataset_record/darwin_core.rb', line 71

def data_field_changed(index, value)
  term_value_changed(get_fields_mapping[index], value)
end

#get_field_mapping(field_name) ⇒ Object (private)



83
84
85
# File 'app/models/dataset_record/darwin_core.rb', line 83

def get_field_mapping(field_name)
  get_fields_mapping[field_name.to_s.downcase]
end

#get_field_value(field_name) ⇒ Object



13
14
15
16
17
18
19
20
# File 'app/models/dataset_record/darwin_core.rb', line 13

def get_field_value(field_name)
  index = get_field_mapping(field_name)

  value = data_fields[index] if index
  normalize_value!(value)

  value.clone if value.present?
end

#get_fields_mappingObject (private)



78
79
80
81
# File 'app/models/dataset_record/darwin_core.rb', line 78

def get_fields_mapping
  @fields_mapping ||= import_dataset.['core_headers']
    .each.with_index.inject({}) { |m, (h, i)| h.blank? ? m : m.merge({ h&.downcase => i, i => h}) }
end

#get_tw_biocuration_groupsObject



52
53
54
55
56
57
58
59
60
# File 'app/models/dataset_record/darwin_core.rb', line 52

def get_tw_biocuration_groups
  get_fields_mapping.keys
    .select { |f| f.is_a?(String) }
    .map do |field|
      /(TW:BiocurationGroup:).+/i =~ field
      {field:, selector: field.sub($1, '')} if $1
    end
    .reject(&:nil?)
end

#get_tw_data_attribute_fields_for(subject_class) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/models/dataset_record/darwin_core.rb', line 22

def get_tw_data_attribute_fields_for(subject_class)
  get_fields_mapping.keys
    .select { |f| f.is_a?(String) }
    .map do |field|
      /(TW:DataAttribute:#{Regexp.escape(subject_class)}:).+/i =~ field
      {field:, selector: field.sub($1, '')} if $1
    end
    .reject(&:nil?)
end

#get_tw_fields_for(subject_class) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'app/models/dataset_record/darwin_core.rb', line 42

def get_tw_fields_for(subject_class)
  get_fields_mapping.keys
    .select { |f| f.is_a?(String) }
    .map do |field|
      /(TW:#{Regexp.escape(subject_class)}:).+/i =~ field
      {field:, name: field.sub($1, '').downcase.to_sym} if $1
    end
    .reject(&:nil?)
end

#get_tw_tag_fields_for(subject_class) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/models/dataset_record/darwin_core.rb', line 32

def get_tw_tag_fields_for(subject_class)
  get_fields_mapping.values
                    .select { |f| f.is_a?(String) }
                    .map do |field|
    /(TW:Tag:#{Regexp.escape(subject_class)}:).+/i =~ field
    {field:, selector: field.sub($1, '')} if $1
  end
                    .reject(&:nil?)
end

#import(dwc_data_attributes = {}) ⇒ Object



62
63
64
65
66
# File 'app/models/dataset_record/darwin_core.rb', line 62

def import(dwc_data_attributes = {})
  raise DatasetRecord::DarwinCore::InvalidData.new(
    {status: ["Import attempted with '#{status}' status"]}
  ) if %w{NotReady Imported Unsupported}.include?(status)
end

#normalize_value!(value) ⇒ Object (private)



87
88
89
90
# File 'app/models/dataset_record/darwin_core.rb', line 87

def normalize_value!(value)
  value&.gsub!(/^[[:space:]]+|[[:space:]]+$/, '') # strip method doesn't deal with https://en.wikipedia.org/wiki/Non-breaking_space
  value&.squeeze!(' ')
end

#term_value_changed(name, value) ⇒ Object (private)

Subclasses may re-implement to react to value changes



76
# File 'app/models/dataset_record/darwin_core.rb', line 76

def term_value_changed(name, value); end