Sha256: b3bf102845a4fe3f930a6d00e1bb1c2f3b1e80627c4f649155a7e343499c6f8f

Contents?: true

Size: 1.99 KB

Versions: 15

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Utils
    module Array
      ##
      # @internal
      #   IMPORTANT: Since `contain_exactly` uses hashes under the hood, `first_array` and `second_array` items should be comparable by `hash` and `eql?`.
      #   It may sometimes introduce hash collisions, which in turn can degrade the performance.
      #   But since the probability is relatively low, because it is NOT too common to have hashes that contain keys with different classes,
      #   hashes comparison is still preferred over array comparison.
      #   Check specs for more details.
      #   - https://github.com/ruby/spec/blob/master/core/hash/shared/eql.rb
      #   - https://stackoverflow.com/questions/54961311/ruby-why-does-hash-need-to-overridden-whenever-eql-is-overridden
      #   - https://ruby-doc.org/core-3.1.2/Object.html#method-i-hash
      #
      #   NOTE: Inspired by `contain_exactly` from `RSpec`.
      #   - https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/contain-exactly-matcher
      #
      class ContainExactly < Support::Command
        ##
        # @!attribute [r] first_array
        #   @return [Array]
        #
        attr_reader :first_array

        ##
        # @!attribute [r] second_array
        #   @return [Array]
        #
        attr_reader :second_array

        ##
        # @param first_array [Array]
        # @param second_array [Array]
        # @return [Boolean]
        #
        def initialize(first_array, second_array)
          @first_array = first_array
          @second_array = second_array
        end

        ##
        # @return [Boolean]
        #
        def call
          first_array_counts = first_array.each_with_object(::Hash.new { |h, k| h[k] = 0 }) { |item, counts| counts[item] += 1 }

          second_array_counts = second_array.each_with_object(::Hash.new { |h, k| h[k] = 0 }) { |item, counts| counts[item] += 1 }

          first_array_counts == second_array_counts
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.11.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.10.1 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.10.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.9.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.8.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.7.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.6.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.5.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.4.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.3.1 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.3.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.2.1 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.2.0 lib/convenient_service/utils/array/contain_exactly.rb
convenient_service-0.1.0 lib/convenient_service/utils/array/contain_exactly.rb