Class: Faker::Date

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/date.rb

Direct Known Subclasses

Time

Constant Summary

Constants inherited from Base

Base::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.backward(days = 365) ⇒ Object


31
32
33
34
35
36
# File 'lib/faker/default/date.rb', line 31

def backward(days = 365)
  from = ::Date.today - days
  to   = ::Date.today - 1

  between(from, to).to_date
end

.between(from, to) ⇒ Object


6
7
8
9
10
11
# File 'lib/faker/default/date.rb', line 6

def between(from, to)
  from = get_date_object(from)
  to   = get_date_object(to)

  Faker::Base.rand_in_range(from, to)
end

.between_except(from, to, excepted) ⇒ Object

Raises:

  • (ArgumentError)

13
14
15
16
17
18
19
20
21
22
# File 'lib/faker/default/date.rb', line 13

def between_except(from, to, excepted)
  raise ArgumentError, 'From date, to date and excepted date must not be the same' if from == to && to == excepted

  excepted = get_date_object(excepted)

  loop do
    date = between(from, to)
    break date.to_date if date != excepted
  end
end

.birthday(min_age = 18, max_age = 65) ⇒ Object


38
39
40
41
42
43
44
45
# File 'lib/faker/default/date.rb', line 38

def birthday(min_age = 18, max_age = 65)
  t = ::Date.today

  from = birthday_date(t, max_age)
  to   = birthday_date(t, min_age)

  between(from, to).to_date
end

.forward(days = 365) ⇒ Object


24
25
26
27
28
29
# File 'lib/faker/default/date.rb', line 24

def forward(days = 365)
  from = ::Date.today + 1
  to   = ::Date.today + days

  between(from, to).to_date
end