Sha256: 22550313dc2e8a81a7da3fbf4d2d675163f20daef83e82330e4e60c99dbd25b4

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module RegexpExamples
  # Abstract (base) class to assist limiting Regexp.examples max results
  class MaxResultsLimiter
    def initialize(initial_results_count)
      @results_count = initial_results_count
    end

    private

    # limiter_method and cumulator_method must be inverses
    # i.e. :- and :+, or :/ and :*
    def limit_results(partial_results, limiter_method, cumulator_method)
      return [] if partial_results.empty? # guard clause
      results_allowed = results_allowed_from(partial_results, limiter_method)
      cumulate_total(results_allowed.length, cumulator_method)
      results_allowed
    end

    def cumulate_total(new_results_count, cumulator_method)
      if @results_count.zero?
        @results_count = new_results_count
      else
        @results_count = @results_count.public_send(cumulator_method, new_results_count)
      end
    end

    def results_allowed_from(partial_results, limiter_method)
      partial_results.first(
        RegexpExamples::Config.max_results_limit
          .public_send(limiter_method, @results_count)
      )
    end
  end

  # For example:
  # Needed when generating examples for /[ab]{10}/
  # (here, results_count will reach 2**10 == 1024)
  class MaxResultsLimiterByProduct < MaxResultsLimiter
    def initialize
      super(1)
    end

    def limit_results(partial_results)
      super(partial_results, :/, :*)
    end
  end

  # For example:
  # Needed when generating examples for /[ab]{10}|{cd}{11}/
  # (here, results_count will reach 1024 + 2048 == 3072)
  class MaxResultsLimiterBySum < MaxResultsLimiter
    def initialize
      super(0)
    end

    def limit_results(partial_results)
      super(partial_results, :-, :+)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regexp-examples-1.4.0 lib/regexp-examples/max_results_limiter.rb