]> jaekl.net Git - quanlib.git/commitdiff
Refactor Book.heading to reduce single-method complexity.
authorChris Jaekl <chris@jaekl.net>
Thu, 27 Jun 2024 23:50:47 +0000 (19:50 -0400)
committerChris Jaekl <chris@jaekl.net>
Thu, 27 Jun 2024 23:50:47 +0000 (19:50 -0400)
app/book.rb

index 417f7a2077cccdfba57823bd15f318716ef550e5..bc4afb7ce5a8c8485d3a9b6e37331fbc6e8558a2 100644 (file)
@@ -51,29 +51,12 @@ class Book
   end
 
   def heading
-    result = []
-
-    if @title.nil?
-      result.push("<i>(Unknown title)</i>")
-    else
-      result.push("<b>#{@title}</b>")
-    end
-    result.push("<i>by #{@author.reading_order}</i>") unless @author.nil?
-
-    series_info = []
-    series = @store.load_series(@series_id)
-    series_info.push(series.descr.to_s) if series&.descr
-    series_info.push(@volume.to_s) unless @volume.nil?
-    result.push(series_info.join(" ")) unless series_info.empty?
-
-    classification = nil
-    classification = @store.load_classification(@classification_id) unless @classification_id.nil?
-    unless classification.nil?
-      result.push("Dewey: #{classification.ddc}") unless classification.ddc.nil?
-      result.push("LCC: #{classification.lcc}") unless classification.lcc.nil?
-    end
-
-    result.join("<br/>")
+    [
+      heading_title,
+      @author.nil? ? nil : "<i>by #{@author.reading_order}</i>",
+      heading_series_info,
+      heading_classification,
+    ].compact.join("<br/>")
   end
 
   def inspect
@@ -101,6 +84,35 @@ class Book
 
   private
 
+  def heading_classification
+    return if @classification_id.nil?
+
+    classification = @store.load_classification(@classification_id) unless @classification_id.nil?
+    return if classification.nil?
+
+    [
+      classification.ddc.nil? ? nil : "Dewey: #{classification.ddc}",
+      classification.lcc.nil? ? nil : "LCC: #{classification.lcc}",
+    ].compact.join(" ")
+  end
+
+  def heading_series_info
+    series = @store.load_series(@series_id)
+
+    [
+      series&.descr&.to_s,
+      @volume&.to_s,
+    ].compact.join(" ")
+  end
+
+  def heading_title
+    if @title.nil?
+      "<i>(Unknown title)</i>"
+    else
+      "<b>#{@title}</b>"
+    end
+  end
+
   def upper?(character)
     /[[:upper:]]/.match(character)
   end