]> jaekl.net Git - quanlib.git/commitdiff
Reduce complexity of book_loader_test and walk_dir
authorChris Jaekl <chris@jaekl.net>
Thu, 11 Jul 2024 01:22:41 +0000 (21:22 -0400)
committerChris Jaekl <chris@jaekl.net>
Thu, 11 Jul 2024 01:22:41 +0000 (21:22 -0400)
app/walk_dir.rb
test/book_loader_test.rb

index d23de80f87ae7832b0d4b0dccea284c639c61719..604946888771a9e110f4d715941e6b275ca658e5 100644 (file)
@@ -75,19 +75,12 @@ class WalkDir
       next unless Book.can_handle?(file)
 
       key = "#{File.dirname(file)}/#{File.basename(file, ".*")}"
-      if unique.key?(key)
-        new_ext = File.extname(file)
-        old_ext = File.extname(unique[key])
-        if old_ext == ".pdf" && new_ext == ".epub"
-          # Prefer EPUB over PDF
-          puts "REPLACED #{unique[key]} with #{file}"
-          unique[key] = file
+      unique[key] =
+        if unique.key?(key)
+          preferred_format(old_file: unique[key], new_file: file)
         else
-          puts "DROPPED #{file} because it's superceded by #{unique[key]}"
+          file
         end
-      else
-        unique[key] = file
-      end
     end
 
     unique.values
@@ -115,6 +108,19 @@ class WalkDir
     12
   end
 
+  def preferred_format(old_file:, new_file:)
+    new_ext = File.extname(new_file)
+    old_ext = File.extname(old_file)
+    if old_ext == ".pdf" && new_ext == ".epub"
+      # Prefer EPUB over PDF
+      puts "REPLACED #{old_file} with #{new_file}"
+      new_file
+    else
+      puts "DROPPED #{new_file} because it's superseded by #{old_file}"
+      old_file
+    end
+  end
+
   def should_descend_into_directory?(path)
     child = File.basename(path)
     File.directory?(path) && child != "." && child != ".." && !File.symlink?(path)
index fd0adb5924bec76b22510461f86e726f2eb01e3b..1bf165fe8dc720dd6042e653d650f85e2d8e50c1 100644 (file)
@@ -24,6 +24,7 @@ class BookLoaderTest < Minitest::Test
     end
   end
 
+  # rubocop:disable Metrics/AbcSize
   def test_load_from_file
     @store.expects(:get_series).returns(mock_series_lw)
     @store.connect
@@ -36,15 +37,18 @@ class BookLoaderTest < Minitest::Test
     assert_equal "Louisa May Alcott", author.reading_order
     assert_equal "Alcott, Louisa May", author.sort_order
 
-    expected_descr = "This story follows the lives of the four March sisters&emdash;Meg, Jo, Beth, and Amy&emdash;" \
-      "and details their coming of age."
+    assert_equal(
+      "This story follows the lives of the four March sisters&emdash;Meg, Jo, Beth, and Amy&emdash;" \
+      "and details their coming of age.",
+      book.description
+    )
 
-    assert_equal expected_descr, book.description
     assert_equal "en", book.language
     assert_equal "Little Women: Or, Meg, Jo, Beth and Amy", book.title
     assert_equal mock_series_lw.to_s, book.series_id.to_s
     assert_equal 1, book.volume.to_i
   end
+  # rubocop:enable Metrics/AbcSize
 
   def test_heading
     @store.expects(:get_series).returns(mock_series_lw)