Class: Identifier

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

Instance Method Summary collapse

Instance Method Details

#check_identifierObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/identifier.rb', line 17

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



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

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



52
53
54
55
56
57
# File 'app/models/identifier.rb', line 52

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

#normalizeObject



59
60
61
62
63
64
65
66
67
68
# File 'app/models/identifier.rb', line 59

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