Class: Identifier::Global::Issn

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

Overview

From www.issn.org/understanding-the-issn/what-is-an-issn/ on 1/31/2014

An ISSN takes the following form:
  - the acronym ISSN is shown in capitals,
  - followed by a space,
  - followed by two groups of four digits, separated by a hyphen.

The number must always be preceded by the ISSN acronym.

From Wikipedia en.wikipedia.org/wiki/International_Standard_Serial_Number on 1/31/2014

The format of the ISSN is an eight digit number, divided by a hyphen into two four-digit numbers.
The last digit, which may be 0–9 or an X, is a check digit.

validates :identifier, :format => => /Adddd-dddz/, :message => ‘Invalid ISSN.’

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Constants included from Shared::DualAnnotator

Shared::DualAnnotator::ALWAYS_COMMUNITY

Instance Attribute Summary

Attributes inherited from Identifier::Global

#relation

Attributes inherited from Identifier

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

Instance Method Summary collapse

Methods inherited from Identifier::Global

#build_cached, #is_global?, #permit_only_one_global_without_relation_supplied_per_type, #sv_resolves?

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods inherited from Identifier

#build_cached, #build_cached_numeric_identifier, #is_global?, #is_local?, prototype_identifier, #set_cached, #type_name

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 Shared::Labels

#labeled?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Method Details

#using_issn_classObject

TODO:

the validator for this identifier has been perverted so as to NOT require the preamble ‘ISSN ’, even though the ISSN spec is quite specific about its being there, because the Bibtex gem does not return it with the ISSN vslue as it should.

Examples from issn.org: ISSN 0317-8471, ISSN 1050-124X



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/identifier/global/issn.rb', line 20

def using_issn_class
  validate_preamble = false
  unless identifier.nil?
    issn = identifier.upcase

    # 'ISSN 1234-567X'
    /^(?<preamble>ISSN ){0,1}(?<part_1>\d{4})-(?<part_2>\d{3})(?<last>.)$/ =~ issn

    if part_1.nil? or part_2.nil? or last.nil? or (preamble.nil? and validate_preamble)
      errors.add(:identifier, "'#{identifier}' is an improperly formed ISSN.")
      return
    end

    data = part_1 + part_2
    if last == 'X'
      sum = 10
    else
      sum = last.to_i
    end
    index = 8

    data.each_char { |c|
      sum += c.to_i * index
      index -= 1
    }
    sum = (sum % 11)

    if sum != 0
      errors.add(:identifier, "'#{identifier}' has bad check digit.")
      return
    end
  end
end