From: Chris Jaekl Date: Thu, 27 Jun 2024 23:50:47 +0000 (-0400) Subject: Refactor Book.heading to reduce single-method complexity. X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=c169cfb6d35ad28a0fcfc436e6ee797c7ac2bb5a;p=quanlib.git Refactor Book.heading to reduce single-method complexity. --- diff --git a/app/book.rb b/app/book.rb index 417f7a2..bc4afb7 100644 --- a/app/book.rb +++ b/app/book.rb @@ -51,29 +51,12 @@ class Book end def heading - result = [] - - if @title.nil? - result.push("(Unknown title)") - else - result.push("#{@title}") - end - result.push("by #{@author.reading_order}") 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("
") + [ + heading_title, + @author.nil? ? nil : "by #{@author.reading_order}", + heading_series_info, + heading_classification, + ].compact.join("
") 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? + "(Unknown title)" + else + "#{@title}" + end + end + def upper?(character) /[[:upper:]]/.match(character) end