Class: PositionalGenerator::Builder::Int

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/positional_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(length, ranges) ⇒ Int

Returns a new instance of Int.



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# 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

Instance Method Details

#generate(_) ⇒ Object



424
425
426
# File 'lib/helpers/positional_generator.rb', line 424

def generate(_)
  Faker::Base.rand(@ranges.sample(random: Faker::Config.random))
end