Class: Identifier::Global::Orcid

Inherits:
Identifier::Global show all
Defined in:
app/models/identifier/global/orcid.rb

Overview

Orcid definition…

Constant Summary

Constant Summary

Constants inherited from Identifier

SHORT_NAMES

Instance Attribute Summary

Attributes inherited from Identifier::Global

#relation

Attributes inherited from Identifier

#cached, #identifier, #identifier_object_id, #namespace_id, #project_id, #type

Instance Method Summary (collapse)

Methods inherited from Identifier::Global

#permit_only_one_global_without_relation_supplied_per_type, #set_cached

Methods inherited from Identifier

#annotated_object, class_name, find_for_autocomplete, generate_download, #klass_name, prototype_identifier, #set_cached

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from ActiverecordUtilities

#trim_attributes

Instance Method Details

- (Object) generate_check_digit(base_digits)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/identifier/global/orcid.rb', line 25

def generate_check_digit(base_digits)

  # int total = 0;
  total = 0

  # for (int i = 0; i < baseDigits.length(); i++) {
  base_digits.each_char { |digit|
    #   int digit = Character.getNumericValue(baseDigits.charAt(i));
    # total = (total + digit) * 2;
    # }
    total = (total + digit.to_i) * 2
  }
  # int remainder = total % 11;
  # int result = (12 - remainder) % 11;
  remainder = (total % 11)
  result = (12 - remainder) % 11
  # return result == 10 ? "X" : String.valueOf(result);
  return result == 10 ? "X" : result.to_s
end

- (Object) using_orcid_class



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/identifier/global/orcid.rb', line 7

def using_orcid_class
  unless identifier.nil?
    orcid = identifier.upcase

    /^(?<preamble>http:\/\/){0,1}(?<domain>orcid\.org\/){1}(?<part_1>\d{4})-(?<part_2>\d{4})-(?<part_3>\d{4})-(?<part_4>\d{3})(?<last>.)$/i =~ orcid

    if domain.nil? or part_1.nil? or part_2.nil? or part_3.nil? or part_4.nil? or last.nil?
      errors.add(:identifier, "'#{identifier}' is an improperly formed ORCID ID.")
      return
    end

    if last != generate_check_digit(part_1 + part_2 + part_3 + part_4)
      errors.add(:identifier, "'#{identifier}' has bad check digit.")
      return
    end
  end
end