Module: EnvironmentHelpers::DatetimeHelpers

Included in:
EnvironmentHelpers
Defined in:
lib/environment_helpers/datetime_helpers.rb

Instance Method Summary collapse

Instance Method Details

#date(name, format: "%Y-%m-%d", default: nil, required: false) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/environment_helpers/datetime_helpers.rb', line 5

def date(name, format: "%Y-%m-%d", default: nil, required: false)
  check_default_type(:date, default, Date)
  text = fetch_value(name, required: required)
  date = parse_date_from(text, format: format)

  return date if date
  return default unless required
  fail(InvalidDateText, "Required date environment variable #{name} had inappropriate content '#{text}'")
end

#date_time(name, format: :iso8601, default: nil, required: false) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/environment_helpers/datetime_helpers.rb', line 15

def date_time(name, format: :iso8601, default: nil, required: false)
  check_default_type(:date_time, default, DateTime)
  text = fetch_value(name, required: required)
  dt = parse_date_time_from(text, format: format)

  return dt if dt
  return default unless required
  fail(InvalidDateTimeText, "Require date_time environment variable #{name} had inappropriate content '#{text}'")
end