Sha256: 60302761b41a7c6ccc66a0347064ea20ab0948938bd9d4d1c31041a376c2398a

Contents?: true

Size: 1.94 KB

Versions: 15

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Utils
    module Array
      class Merge < Support::Command
        ##
        # @!attribute [r] array
        #   @return [Array]
        #
        attr_reader :array

        ##
        # @!attribute [r] overrides
        #   @return [Hash]
        #
        attr_reader :overrides

        ##
        # @!attribute [r] raise_on_non_integer_index
        #   @return [Boolean]
        #
        attr_reader :raise_on_non_integer_index

        ##
        # @param array [Array]
        # @param overrides [Hash]
        # @param raise_on_non_integer_index [Boolean]
        # @return [void]
        #
        def initialize(array, overrides, raise_on_non_integer_index: true)
          @array = array
          @overrides = overrides
          @raise_on_non_integer_index = raise_on_non_integer_index
        end

        ##
        # @return [Array]
        # @raise [ConvenientService::Utils::Array::Errors::NonIntegerIndex]
        #
        def call
          ensure_valid_overrides!

          integer_key_overrides.each_with_object(array.dup) { |(index, value), array| array[index] = value }
        end

        private

        ##
        # @return [void]
        # @raise [ConvenientService::Utils::Array::Errors::NonIntegerIndex]
        #
        def ensure_valid_overrides!
          return integer_key_overrides unless raise_on_non_integer_index

          return if overrides.size == integer_key_overrides.size

          raise Errors::NonIntegerIndex.new(index: non_integer_index)
        end

        ##
        # @return [Hash]
        #
        def integer_key_overrides
          @integer_key_overrides ||= overrides.dup.keep_if { |index| index.is_a?(::Integer) }
        end

        ##
        # @return [Object] Can be any type.
        #
        def non_integer_index
          @non_integer_index ||= overrides.keys.find { |index| !index.is_a?(::Integer) }
        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/merge.rb
convenient_service-0.11.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.10.1 lib/convenient_service/utils/array/merge.rb
convenient_service-0.10.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.9.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.8.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.7.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.6.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.5.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.4.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.3.1 lib/convenient_service/utils/array/merge.rb
convenient_service-0.3.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.2.1 lib/convenient_service/utils/array/merge.rb
convenient_service-0.2.0 lib/convenient_service/utils/array/merge.rb
convenient_service-0.1.0 lib/convenient_service/utils/array/merge.rb