class PositionalGenerator::Builder::Int

Public Class Methods

new(length, ranges) click to toggle source
# File lib/helpers/positional_generator.rb, line 407
def initialize(length, ranges)
  # Internally we store only an Enumerable of Range values. So if we are
  # not given any Ranges but are given a length, we need to convert the
  # length to a Range.
  #
  # If the length is `5`, that means we should compute the Range `10000..99999`.
  # We can compute the lower end with a simple exponent: 10^4 = 10000.
  # The upper end is one less than an exponent: 10^5 - 1 = 99999.
  if ranges.nil?
    lower = 10**(length - 1)
    upper = (10**length) - 1
    ranges = [lower..upper]
  end

  @ranges = ranges
end

Public Instance Methods

generate(_) click to toggle source
# File lib/helpers/positional_generator.rb, line 424
def generate(_)
  Faker::Base.rand(@ranges.sample(random: Faker::Config.random))
end