From 54e513e2047649f043fe37c3e90551919b419682 Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Thu, 27 Jun 2024 21:05:18 -0400 Subject: [PATCH] Add Util::Inspector to reduce code complexity. --- app/author.rb | 10 +++------- app/book.rb | 11 +---------- app/classification.rb | 13 +++---------- app/series.rb | 12 +++--------- test/classification_test.rb | 4 ++-- 5 files changed, 12 insertions(+), 38 deletions(-) diff --git a/app/author.rb b/app/author.rb index 716f5aa..964f48e 100644 --- a/app/author.rb +++ b/app/author.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "util/inspector" + # Encapsulates information about an author class Author attr_accessor :grouping, :reading_order, :sort_order @@ -13,13 +15,7 @@ class Author end def inspect - field_info = [ - grouping.nil? ? nil : "grouping=\"#{grouping}\"", - reading_order.nil? ? nil : "reading_order=\"#{reading_order}\"", - sort_order.nil? ? nil : "sort_order=\"#{sort_order}\"", - ].compact.join(" ") - - "(Author: #{field_info})" + Util::Inspector.inspect(self, %w[grouping reading_order sort_order]) end def to_s diff --git a/app/book.rb b/app/book.rb index bc4afb7..7c9012b 100644 --- a/app/book.rb +++ b/app/book.rb @@ -60,16 +60,7 @@ class Book end def inspect - field_info = [ - author.nil? ? nil : "author=\"#{author.inspect}", - series_id.nil? ? nil : "series_id=\"#{series_id.inspect}", - volume.nil? ? nil : "volume=\"#{volume.inspect}", - title.nil? ? nil : "title=\"#{title.inspect}", - cover.nil? ? nil : "cover=\"#{cover.inspect}", - path.nil? ? nil : "path=\"#{path.inspect}", - ].compact.join(" ") - - "(Book: #{field_info})" + Inspector.inspect(self, %w[author series_id volume title cover path]) end def to_s diff --git a/app/classification.rb b/app/classification.rb index c9055a1..92c7e25 100644 --- a/app/classification.rb +++ b/app/classification.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "util/inspector" + # Encapsulates information about a book's classification, under the # Dewey Decimal and/or Library of Congress systems. class Classification @@ -16,16 +18,7 @@ class Classification end def inspect - field_info = [ - ddc.nil? ? nil : "Dewey=#{ddc.inspect}", - lcc.nil? ? nil : "LCC=#{lcc.inspect}", - author_grouping.nil? ? nil : "author_grouping=#{author_grouping.inspect}", - author.nil? ? nil : "author=#{author.inspect}", - title_grouping.nil? ? nil : "title_grouping=#{title_grouping.inspect}", - title.nil? ? nil : "title=#{title.inspect}", - ].compact.join(", ") - - "(Classification: #{field_info})" + Util::Inspector.inspect(self, %w[ddc lcc author_grouping author title_grouping title]) end def to_s diff --git a/app/series.rb b/app/series.rb index 418c70e..70a4246 100644 --- a/app/series.rb +++ b/app/series.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "util/inspector" + # Encapsulates data about a series of books class Series attr_reader :id @@ -16,15 +18,7 @@ class Series end def inspect - field_info = [ - age.nil? ? nil : "age=#{age.inspect}", - code.nil? ? nil : "code=#{code.inspect}", - descr.nil? ? nil : "descr=#{descr.inspect}", - genre.nil? ? nil : "genre=#{genre.inspect}", - grouping.nil? ? nil : "grouping=#{grouping.inspect}", - ].compact.join(" ") - - "(Series: #{field_info})" + Util::Inspector.inspect(self, %w[age code descr genre grouping]) end def key diff --git a/test/classification_test.rb b/test/classification_test.rb index 570393c..3049106 100644 --- a/test/classification_test.rb +++ b/test/classification_test.rb @@ -14,8 +14,8 @@ class ClassificationTest < Minitest::Test classification = Classification.new(ddc, lcc, author_grouping, author_sort, title_grouping, title) - expected = '(Classification: Dewey="973.3/092 B", LCC="E302.6.F7", author_grouping="BenjaminFranklin", ' \ - 'author="Franklin, Benjamin", title_grouping="Autobiography_of_Benjamin_Franklin", title="Autobiography ' \ + expected = '(Classification: ddc="973.3/092 B" lcc="E302.6.F7" author_grouping="BenjaminFranklin" ' \ + 'author="Franklin, Benjamin" title_grouping="Autobiography_of_Benjamin_Franklin" title="Autobiography ' \ 'of Benjamin Franklin")' actual = classification.inspect -- 2.39.2