Class: Pubid::Isbn::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/isbn/builder.rb

Overview

Builds a Pubid::Isbn::Identifier from a parse tree.

Validates the check digit; raises if invalid. Stores both the hyphenated form (for round-trip rendering) and the bare digits (for queries).

Constant Summary collapse

INVALID_LENGTH =
/^(?:\d{9}[\dX]|\d{13})$/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(parsed_data) ⇒ Object



13
14
15
# File 'lib/pubid/isbn/builder.rb', line 13

def self.build(parsed_data)
  new.build(parsed_data)
end

Instance Method Details

#build(data) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/pubid/isbn/builder.rb', line 17

def build(data)
  hyphenated = data[:body].to_s
  raw = hyphenated.delete("-")

  validate_length!(raw)
  validate_check_digit!(raw)

  Identifiers::Book.new(raw: raw, hyphenated: hyphenated.include?("-") ? hyphenated : nil)
end