Module: PgDice::DateHelper

Included in:
TableFinder
Defined in:
lib/pgdice/date_helper.rb

Overview

Helper used to manipulate date objects

Instance Method Summary collapse

Instance Method Details

#pad_date(numbers) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pgdice/date_helper.rb', line 6

def pad_date(numbers)
  return numbers if numbers.size == 8

  case numbers.size
  when 6
    "#{numbers}01"
  when 4
    "#{numbers}0101"
  else
    raise ArgumentError, "Invalid date. Cannot parse date from #{numbers}"
  end
end

#safe_date_builder(table_name) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/pgdice/date_helper.rb', line 32

def safe_date_builder(table_name)
  matches = table_name.match(/\d+/)
  raise ArgumentError, "Invalid date. Cannot parse date from #{table_name}" unless matches

  Date.parse(pad_date(matches[0]))
end

#truncate_date(date, period) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pgdice/date_helper.rb', line 19

def truncate_date(date, period)
  case period
  when 'year'
    Date.parse("#{date.year}0101")
  when 'month'
    Date.parse("#{date.strftime('%Y%m')}01")
  when 'day'
    date
  else
    raise ArgumentError, "Invalid date. Cannot parse date from #{date}"
  end
end