Module: DataMagic::DateTranslation
- Included in:
- DataMagic, Translation
- Defined in:
- lib/data_magic/date_translation.rb
Instance Method Summary collapse
-
#date_between(from = nil, to = nil, format = '%D') ⇒ Object
(also: #dm_date_between)
return random date within the range.
-
#day_of_week ⇒ Object
(also: #dm_day_of_week)
return a day of the week.
- #day_of_week_abbr ⇒ Object (also: #dm_day_of_week_abbr)
-
#month ⇒ Object
(also: #dm_month)
return a month.
-
#month_abbr ⇒ Object
(also: #dm_month_abbr)
return a month abbreviation.
-
#today(format = '%D') ⇒ Object
(also: #dm_today)
return today’s date.
-
#tomorrow(format = '%D') ⇒ Object
(also: #dm_tomorrow)
return tomorrow’s date.
-
#yesterday(format = '%D') ⇒ Object
(also: #dm_yesterday)
return yesterday’s date.
Instance Method Details
#date_between(from = nil, to = nil, format = '%D') ⇒ Object Also known as: dm_date_between
return random date within the range
84 85 86 87 88 89 90 91 |
# File 'lib/data_magic/date_translation.rb', line 84 def date_between(from = nil, to = nil, format = '%D') raise ArgumentError, 'Invalid date format' if from.to_s.empty? || to.to_s.empty? start_date = from.nil? ? Date.today.strftime(format) : Date.strptime(from, format) end_date = to.nil? ? Date.today.strftime(format) : Date.strptime(to, format) Faker::Date.between(from: start_date, to: end_date).strftime(format) end |
#day_of_week ⇒ Object Also known as: dm_day_of_week
return a day of the week
65 66 67 |
# File 'lib/data_magic/date_translation.rb', line 65 def day_of_week randomize(Date::DAYNAMES) end |
#day_of_week_abbr ⇒ Object Also known as: dm_day_of_week_abbr
70 71 72 |
# File 'lib/data_magic/date_translation.rb', line 70 def day_of_week_abbr randomize(Date::ABBR_DAYNAMES) end |
#month ⇒ Object Also known as: dm_month
return a month
49 50 51 |
# File 'lib/data_magic/date_translation.rb', line 49 def month randomize(Date::MONTHNAMES[1..-1]) end |
#month_abbr ⇒ Object Also known as: dm_month_abbr
return a month abbreviation
57 58 59 |
# File 'lib/data_magic/date_translation.rb', line 57 def month_abbr randomize(Date::ABBR_MONTHNAMES[1..-1]) end |
#today(format = '%D') ⇒ Object Also known as: dm_today
return today’s date
See ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime for details of the formats
13 14 15 |
# File 'lib/data_magic/date_translation.rb', line 13 def today(format = '%D') Date.today.strftime(format) end |
#tomorrow(format = '%D') ⇒ Object Also known as: dm_tomorrow
return tomorrow’s date
See ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime for details of the formats
26 27 28 29 |
# File 'lib/data_magic/date_translation.rb', line 26 def tomorrow(format = '%D') tomorrow = Date.today + 1 tomorrow.strftime(format) end |
#yesterday(format = '%D') ⇒ Object Also known as: dm_yesterday
return yesterday’s date
See ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime for details of the formats
40 41 42 43 |
# File 'lib/data_magic/date_translation.rb', line 40 def yesterday(format = '%D') yesterday = Date.today - 1 yesterday.strftime(format) end |