Class: Synthra::Types::DateTime::FutureDate

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/date_time/dates.rb

Overview

FutureDate type

Instance Method Summary collapse

Instance Method Details

#generate_edge(rng, context, args) ⇒ Object



299
300
301
302
# File 'lib/synthra/types/date_time/dates.rb', line 299

def generate_edge(rng, context, args)
  anchor = TimeTravel.anchor_date || Date.today
  [anchor + 1, anchor + 365].sample(random: rng.instance_variable_get(:@random))
end

#generate_invalid(rng, context, args) ⇒ Object



304
305
306
# File 'lib/synthra/types/date_time/dates.rb', line 304

def generate_invalid(rng, context, args)
  [nil, "not a date", 123, [], {}].sample(random: rng.instance_variable_get(:@random))
end

#generate_random(rng, context, args) ⇒ Date

Generate a random future date

Generates a date in the future using the specified duration.

Parameters:

Options Hash (args):

  • :value (Integer, Hash)

    duration value

  • :suffix (String)

    duration unit (d, m, y)

Returns:

  • (Date)

    date in the future



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/synthra/types/date_time/dates.rb', line 281

def generate_random(rng, context, args)
  duration = parse_duration(args)
  anchor = TimeTravel.anchor_date
  return anchor + duration if anchor

  adapter = faker_adapter(context)
  if adapter
    adapter.date_forward(days: duration)

  else
    Faker::Date.forward(days: duration)
  end

rescue StandardError
  Date.today + duration
end

#parse_duration(args) ⇒ Object (private)



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/synthra/types/date_time/dates.rb', line 310

def parse_duration(args)
  # Handle nested value hash like {value: {value: 7, suffix: "d"}}
  if args[:value].is_a?(Hash)
    value = args[:value][:value] || 1
    suffix = args[:value][:suffix] || "d"
  else
    value = args[:value] || 365
    suffix = args[:suffix] || "d"
  end

  case suffix.to_s.downcase
  when "y", "year", "years"
    value * 365
  when "m", "month", "months"
    value * 30
  when "d", "day", "days"
    value
  else
    value
  end
end