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

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?

Instance Method Details

- (Object) using_issn_class

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