Class: BerkeleyLibrary::AV::RecordId

Inherits:
Object
  • Object
show all
Extended by:
Constants
Includes:
Util, Comparable
Defined in:
lib/berkeley_library/av/record_id.rb

Defined Under Namespace

Classes: Type

Constant Summary

Constants included from Constants

Constants::ALMA_RECORD_RE, Constants::MILLENNIUM_RECORD_RE, Constants::OCLC_RECORD_RE, Constants::SUBFIELD_CODES_TRACKS, Constants::SUBFIELD_CODE_ALMA_BIB_NUMBER, Constants::SUBFIELD_CODE_DURATION, Constants::SUBFIELD_CODE_LINKTEXT, Constants::SUBFIELD_CODE_PATH, Constants::SUBFIELD_CODE_TIND_BIB_NUMBER, Constants::SUBFIELD_CODE_TITLE, Constants::SUBFIELD_CODE_URI, Constants::TAG_ALMA_MIGRATION_INFO, Constants::TAG_LINK_FIELD, Constants::TAG_TIND_CATALOG_ID, Constants::TAG_TIND_ID, Constants::TAG_TITLE_FIELD, Constants::TAG_TRACK_FIELD, Constants::TAG_TRANSCRIPT_FIELD, Constants::UNKNOWN_TITLE

Constants included from Util

Util::DEFAULT_USER_AGENT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#class_name, #compare_by_attributes, #compare_values, #do_get, #tidy_value

Constructor Details

#initialize(id) ⇒ RecordId


Initializer



19
20
21
22
# File 'lib/berkeley_library/av/record_id.rb', line 19

def initialize(id)
  @id = id.to_s
  @type = Type.for_id(@id)
end

Instance Attribute Details

#idObject (readonly)


Fields



14
15
16
# File 'lib/berkeley_library/av/record_id.rb', line 14

def id
  @id
end

#typeObject (readonly)


Fields



14
15
16
# File 'lib/berkeley_library/av/record_id.rb', line 14

def type
  @type
end

Class Method Details

.ensure_check_digit(bib_number) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/berkeley_library/av/record_id.rb', line 36

def ensure_check_digit(bib_number)
  digit_str, check_str = split_bib(bib_number)

  digits = digit_str.chars.map(&:to_i)
  check_digit = calculate_check_digit(digits)

  return "b#{digit_str}#{check_digit}" if check_str.nil? || check_str == 'a'
  return bib_number if check_str == check_digit

  raise ArgumentError, "#{bib_number} check digit invalid: expected #{check_digit}, got #{check_str}"
end

.ensure_record_id(record_id) ⇒ Object



30
31
32
33
34
# File 'lib/berkeley_library/av/record_id.rb', line 30

def ensure_record_id(record_id)
  return record_id if record_id.is_a?(RecordId)

  RecordId.new(record_id)
end

.strip_check_digit(bib_number) ⇒ Object



48
49
50
51
# File 'lib/berkeley_library/av/record_id.rb', line 48

def strip_check_digit(bib_number)
  digit_str, = split_bib(bib_number)
  "b#{digit_str}"
end

Instance Method Details

#<=>(other) ⇒ Object


Comparable



73
74
75
# File 'lib/berkeley_library/av/record_id.rb', line 73

def <=>(other)
  compare_by_attributes(self, other, :id, :type)
end

#hashObject


Object overrides



80
81
82
# File 'lib/berkeley_library/av/record_id.rb', line 80

def hash
  [id, type].hash
end

#inspectObject



88
89
90
# File 'lib/berkeley_library/av/record_id.rb', line 88

def inspect
  "#{self.class}(#{id} [#{type}])@#{object_id}"
end

#to_sObject



84
85
86
# File 'lib/berkeley_library/av/record_id.rb', line 84

def to_s
  id
end