Class: Identifier

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/identifier.rb,
app/models2/identifier.rb

Instance Method Summary collapse

Instance Method Details

#check_identifierObject



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

def check_identifier
  case identifier_type.try(:name)
  when 'isbn'
    unless StdNum::ISBN.valid?(body)
      errors.add(:body)
    end

  when 'issn'
    unless StdNum::ISSN.valid?(body)
      errors.add(:body)
    end

  when 'lccn'
    unless StdNum::LCCN.valid?(body)
      errors.add(:body)
    end

  when 'doi'
    if URI.parse(body).scheme
      errors.add(:body)
    end
  end
end

#convert_isbnObject



45
46
47
48
49
50
51
52
53
54
# File 'app/models/identifier.rb', line 45

def convert_isbn
  if identifier_type.name == 'isbn'
    lisbn = Lisbn.new(body)
    if lisbn.isbn
      if lisbn.isbn.length == 10
        self.body = lisbn.isbn13
      end
    end
  end
end

#hyphenated_isbnObject



56
57
58
59
60
61
# File 'app/models/identifier.rb', line 56

def hyphenated_isbn
  if identifier_type.name == 'isbn'
    lisbn = Lisbn.new(body)
    lisbn.parts.join('-')
  end
end

#normalizeObject



63
64
65
66
67
68
69
70
71
72
# File 'app/models/identifier.rb', line 63

def normalize
  case identifier_type.try(:name)
  when 'isbn'
    self.body = StdNum::ISBN.normalize(body)
  when 'issn'
    self.body = StdNum::ISSN.normalize(body)
  when 'lccn'
    self.body = StdNum::LCCN.normalize(body)
  end
end