Add support to cross-reference books against list of award-winners.
[quanlib.git] / book.rb
diff --git a/book.rb b/book.rb
index d7135690d4d60514f4c900d59dc76698d3eb164d..ea650249a9ac6c8c120e72824dce646276bc4c9f 100644 (file)
--- a/book.rb
+++ b/book.rb
@@ -10,6 +10,7 @@ require_relative 'store'
 
 class Book
   @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
+  @@SERIES_AND_VOLUME_REGEX = /^([A-Z]+)([0-9]+(\.[0-9]+)?)$/
 
   attr_accessor :author
   attr_accessor :classification_id
@@ -49,6 +50,18 @@ class Book
     return false
   end
 
+  def self.grouping_for_title(title)
+    result = title
+
+    '\'",!#'.split('').each do |c|
+      result = result.gsub(c, '-')
+    end
+    result = result.gsub(/: */, '--')
+    result = result.gsub(' ', '_')
+
+    result
+  end
+
   def heading
     result = []
 
@@ -159,7 +172,7 @@ class Book
     vol = nil
 
     first = arr[0]
-    matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
+    matchData = (arr[0]).match(@@SERIES_AND_VOLUME_REGEX)
     if nil != matchData
       capt = matchData.captures
       series = capt[0]
@@ -174,6 +187,15 @@ class Book
 
     title = arr.join(' ')
 
+    bare_title_grouping = title_grouping
+      .split('_')
+      .reject { |part| part.match(@@SERIES_AND_VOLUME_REGEX) }
+      .join('_')
+
+    unless bare_title_grouping == Book.grouping_for_title(title)
+      puts "WARNING:  title_grouping mismatch:  #{bare_title_grouping.inspect} vs. #{Book.grouping_for_title(title).inspect}"
+    end
+
     return series, vol, title
   end