Add `arrived` attribute (file creation timestamp) to books table.
[quanlib.git] / book.rb
diff --git a/book.rb b/book.rb
index 2f7b7a6afff83e3c5c61225c15ba886f916d975b..2b93f4b574c9c45738a3a95a2c0b4f1697083d1c 100644 (file)
--- a/book.rb
+++ b/book.rb
@@ -10,17 +10,21 @@ 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 :arrived
+  attr_accessor :author
+  attr_accessor :classification_id
+  attr_accessor :cover
+  attr_accessor :description
+  attr_accessor :language
+  attr_accessor :path
+  attr_accessor :series_id
+  attr_accessor :title
+  attr_accessor :volume
 
   def initialize(store)
-    @author = nil
-    @classification_id = nil
-    @cover = nil
-    @description = nil
-    @path = nil
-    @series_id = nil
     @store = store
-    @title = nil
-    @volume = nil
   end
 
   def load_from_file!(fileName)
@@ -47,36 +51,16 @@ class Book
     return false
   end
 
-  def author
-    return @author
-  end
-
-  def author=(value)
-    @author = value
-  end
-
-  def classification_id
-    @classification_id
-  end
+  def self.grouping_for_title(title)
+    result = title
 
-  def classification_id=(value)
-    @classification_id = value
-  end
-
-  def cover
-    return @cover
-  end
-
-  def cover=(value)
-    @cover = value
-  end
-
-  def description
-    @description
-  end
+    '\'",!#'.split('').each do |c|
+      result = result.gsub(c, '-')
+    end
+    result = result.gsub(/: */, '--')
+    result = result.gsub(' ', '_')
 
-  def description=(value)
-    @description = value
+    result
   end
 
   def heading
@@ -142,34 +126,10 @@ class Book
     return '(Book:' + data.join(',') + ')'
   end
 
-  def path
-    @path
-  end
-
-  def path=(value)
-    @path = value
-  end
-
-  def series_id
-    @series_id
-  end
-
-  def series_id=(value)
-    @series_id = value
-  end
-
   def to_s
     return inspect()
   end
 
-  def title
-    @title
-  end
-
-  def title=(value)
-    @title = value
-  end
-
   def title_grouping
     if nil == @path
       return nil
@@ -178,14 +138,6 @@ class Book
     return File.basename(@path, '.*')
   end
 
-  def volume
-    @volume
-  end
-
-  def volume=(value)
-    @volume = value
-  end
-
   protected
   def isUpper?(c)
     return /[[:upper:]]/.match(c)
@@ -221,7 +173,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]
@@ -236,6 +188,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
 
@@ -264,6 +225,8 @@ class Book
       scan_pdf!(file_name)
     end
 
+    @arrived = File.ctime(file_name)
+
     @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
 
     # TODO:  Fix horrible hard-coded strings and paths
@@ -371,6 +334,17 @@ class Book
       end
     end
 
+    #---------------------------------------
+    # Language
+
+    langNodes = opfDoc.css('dc|language', 'dc' => @@DC_NS_URL)
+    if (langNodes.length > 0)
+      langNode = langNodes[0]
+      if langNode
+        @language = langNode.content
+      end
+    end
+
     #---------------------------------------
     # Other metadata:  series, volume, cover