Class: Synthra::Types::DateTime::PastDate

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

Overview

PastDate type

Instance Method Summary collapse

Instance Method Details

#generate_edge(rng, context, args) ⇒ Object



232
233
234
235
# File 'lib/synthra/types/date_time/dates.rb', line 232

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



237
238
239
# File 'lib/synthra/types/date_time/dates.rb', line 237

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 past date

Generates a date in the past using the specified duration.

Parameters:

Options Hash (args):

  • :value (Integer, Hash)

    duration value

  • :suffix (String)

    duration unit (d, m, y, h)

Returns:

  • (Date)

    date in the past



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/synthra/types/date_time/dates.rb', line 214

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_backward(days: duration)

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

rescue StandardError
  Date.today - duration
end

#parse_duration(args) ⇒ Object (private)



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/synthra/types/date_time/dates.rb', line 243

def parse_duration(args)
  # Handle nested value hash like {value: {value: 2, suffix: "y"}}
  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
  when "h", "hour", "hours"
    (value / 24.0).ceil
  else
    value
  end
end