Sha256: c70a081679818bd687aca7af6ab618d8a7fc721d77719a6b678680ea5dfce61a

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Utils
    module Array
      class LimitedPush < Support::Command
        module Constants
          ##
          # @return [Integer]
          #
          DEFAULT_LIMIT = 1_000
        end

        ##
        # @!attribute [r] array
        #   @return [Array<Object>]
        #
        attr_reader :array

        ##
        # @!attribute [r] object
        #   @return [Object] Can be any type.
        #
        attr_reader :object

        ##
        # @!attribute [r] limit
        #   @return [Integer]
        #
        attr_reader :limit

        ##
        # @param array [Array<Object>]
        # @param object [Object] Can be any type.
        # @return [void]
        #
        def initialize(array, object, limit: Constants::DEFAULT_LIMIT)
          @array = array
          @object = object
          @limit = limit
        end

        ##
        # @return [Array<Object>]
        #
        def call
          return array if array.size >= limit

          array.push(object)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.19.0 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.18.0 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.17.0 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.16.0 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.15.0 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.14.0 lib/convenient_service/utils/array/limited_push.rb
convenient_service-0.13.0 lib/convenient_service/utils/array/limited_push.rb