Commit accc110

mo khan <mo.khan@gmail.com>
2020-05-15 18:58:49
Make #scan a private method
1 parent 95ba331
Changed files (3)
lib/spandx/core/index_file.rb
@@ -21,24 +21,15 @@ module Spandx
         end
       end
 
-      def search
-        min = 0
-        max = size
-
+      def search(min: 0, max: size)
         scan do |reader|
           until min >= max
-            mid = (max - min) == 1 ? min : (((max - min) / 2) + min)
+            mid = mid_for(min, max)
             row = reader.row(mid)
-            return if row.nil? || row.empty?
-
             comparison = yield row
             return row if comparison.zero?
 
-            if comparison.positive?
-              min = mid + 1
-            else
-              max = mid
-            end
+            comparison.positive? ? (min = mid + 1) : (max = mid)
           end
         end
       end
@@ -59,12 +50,6 @@ module Spandx
         entry
       end
 
-      def scan
-        data_file.open_file(mode: 'rb') do |io|
-          yield Relation.new(io, self)
-        end
-      end
-
       def update!
         return unless data_file.exist?
 
@@ -76,6 +61,12 @@ module Spandx
 
       attr_reader :entries
 
+      def scan
+        data_file.open_file(mode: 'rb') do |io|
+          yield Relation.new(io, self)
+        end
+      end
+
       def offset_for(row_number)
         row_number * UINT_32_SIZE
       end
@@ -101,6 +92,10 @@ module Spandx
         lines.pop if lines.size > 1
         lines
       end
+
+      def mid_for(min, max)
+        (max - min) == 1 ? min : (((max - min) / 2) + min)
+      end
     end
   end
 end
spec/unit/core/index_file_spec.rb
@@ -3,7 +3,7 @@
 RSpec.describe Spandx::Core::IndexFile do
   subject { described_class.new(data_file) }
 
-  describe '#scan' do
+  describe '#search' do
     let(:data_file) { Spandx::Core::DataFile.new(tmp_file.path) }
     let(:tmp_file) { Tempfile.new }
 
@@ -20,21 +20,18 @@ RSpec.describe Spandx::Core::IndexFile do
     end
 
     specify do
-      subject.scan do |x|
-        expect(x.row(0)).to eql(['activemodel', '6.0.2.2', 'Apache-2.0'])
-      end
+      result = subject.search { |row| 'activemodel-6.0.2.2' <=> "#{row[0]}-#{row[1]}" }
+      expect(result).to eql(['activemodel', '6.0.2.2', 'Apache-2.0'])
     end
 
     specify do
-      subject.scan do |x|
-        expect(x.row(1)).to eql(['spandx', '0.1.0', 'MIT'])
-      end
+      result = subject.search { |row| 'spandx-0.1.0' <=> "#{row[0]}-#{row[1]}" }
+      expect(result).to eql(['spandx', '0.1.0', 'MIT'])
     end
 
     specify do
-      subject.scan do |x|
-        expect(x.row(2)).to eql(['zlib', '1.1.0', '0BSD'])
-      end
+      result = subject.search { |row| 'zlib-1.1.0' <=> "#{row[0]}-#{row[1]}" }
+      expect(result).to eql(['zlib', '1.1.0', '0BSD'])
     end
   end
 
Gemfile.lock
@@ -93,7 +93,7 @@ GEM
       unicode-display_width (>= 1.4.0, < 2.0)
     rubocop-rspec (1.39.0)
       rubocop (>= 0.68.1)
-    ruby-prof (1.4.0)
+    ruby-prof (1.4.1)
     ruby-progressbar (1.10.1)
     ruby-xxHash (0.4.0.1)
     rugged (0.99.0)