Sha256: e0376d573cfe641ff29b75b0d5d77c5e22b56c55bcc25573d2c657485dde1a07

Contents?: true

Size: 880 Bytes

Versions: 1

Compression:

Stored size: 880 Bytes

Contents

class DistributionEvaluator
  attr_reader :count, :value

  def initialize(value = nil, &block)
    @count = 0
    @value = value || block
    @evaluator_name = evaluator_name(@value)
  end

  def next_value
    current_val = send @evaluator_name
    @count += 1
    current_val
  end

  private

  def evaluator_name(value)
    case
      when value.is_a?(Array)
        :array_dist_value
      when value.is_a?(Hash)
        :hash_dist_value
      when value.respond_to?(:call)
        :block_dist_value
      else
        :value
    end
  end

  def array_dist_value
    value[@count % value.size]
  end

  def block_dist_value
    value.call(@count)
  end

  def hash_dist_value
    idx = value[:rest] ? @count : @count % value.keys.last
    matching_value = value.find{|k, _| k.is_a?(Integer) && k > idx }
    (matching_value && matching_value.last) || value[:rest]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aggrobot-0.1.0 spec/support/factory_robot/distribution_evaluator.rb