Class: Bookland::Identifier

Inherits:
Object
  • Object
show all
Defined in:
lib/bookland/identifier.rb

Direct Known Subclasses

ASIN, EAN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Identifier

Returns a new instance of Identifier.



13
14
15
# File 'lib/bookland/identifier.rb', line 13

def initialize(raw)
  @digits = raw.split('')
end

Instance Attribute Details

#digitsObject (readonly)

Returns the value of attribute digits.



11
12
13
# File 'lib/bookland/identifier.rb', line 11

def digits
  @digits
end

Class Method Details

.calculate_checksum_digit(data_digits) ⇒ Object

Raises:

  • (NotImplementedError)


7
8
9
# File 'lib/bookland/identifier.rb', line 7

def self.calculate_checksum_digit(data_digits)
  raise NotImplementedError
end

.valid?(raw) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/bookland/identifier.rb', line 3

def self.valid?(raw)
  new(raw).valid?
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/bookland/identifier.rb', line 33

def ==(other)
  to_s == other.to_s
end

#checksum_digitObject



21
22
23
# File 'lib/bookland/identifier.rb', line 21

def checksum_digit
  digits[-1]
end

#data_digitsObject



17
18
19
# File 'lib/bookland/identifier.rb', line 17

def data_digits
  digits[0...-1]
end

#to_sObject



25
26
27
# File 'lib/bookland/identifier.rb', line 25

def to_s
  digits.join
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/bookland/identifier.rb', line 29

def valid?
  checksum_digit == recalculate_checksum_digit
end