Class: ActiveWarehouse::Builder::DateDimensionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_warehouse/builder/date_dimension_builder.rb

Overview

A builder which will build a data structure which can be used to populate a date dimension using commonly used date dimension columns.

Constant Summary collapse

@@weekday_indicators =
['Weekend','Weekday','Weekday','Weekday','Weekday','Weekday','Weekend']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date = Time.now.years_ago(5), end_date = Time.now) ⇒ DateDimensionBuilder

Initialize the builder.

  • start_date: The start date. Defaults to 5 years ago from today.

  • end_date: The end date. Defaults to now.



23
24
25
26
27
# File 'lib/active_warehouse/builder/date_dimension_builder.rb', line 23

def initialize(start_date=Time.now.years_ago(5), end_date=Time.now)
  @start_date = start_date
  @end_date = end_date
  @holiday_indicators = []
end

Instance Attribute Details

#end_dateObject

Specify the end date for the last record



10
11
12
# File 'lib/active_warehouse/builder/date_dimension_builder.rb', line 10

def end_date
  @end_date
end

#holiday_indicatorsObject

Define any holiday indicators



13
14
15
# File 'lib/active_warehouse/builder/date_dimension_builder.rb', line 13

def holiday_indicators
  @holiday_indicators
end

#start_dateObject

Specify the start date for the first record



7
8
9
# File 'lib/active_warehouse/builder/date_dimension_builder.rb', line 7

def start_date
  @start_date
end

Instance Method Details

#build(options = {}) ⇒ Object

Returns an array of hashes representing records in the dimension. The values for each record are accessed by name.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_warehouse/builder/date_dimension_builder.rb', line 31

def build(options={})
  records = []
  date = start_date.to_time
  while date <= end_date.to_time
    record = {}
    record[:date] = date.strftime("%m/%d/%Y")
    record[:full_date_description] = date.strftime("%B %d,%Y")
    record[:day_of_week] = date.strftime("%A")
    #record[:day_number_in_epoch] = date.to_i / 24
    #record[:week_number_in_epoch] = date.to_i / (24 * 7)
    #record[:month_number_in_epoch] = date.to_i / (24 * 7 * 30)
    record[:day_number_in_calendar_month] = date.day
    record[:day_number_in_calendar_year] = date.yday
    record[:day_number_in_fiscal_month] = date.day # should this be different from CY?
    record[:day_number_in_fiscal_year] = date.fiscal_year_yday
    #record[:last_day_in_week_indicator] = 
    #record[:last_day_in_month_indicator] =
    #record[:calendar_week_ending_date] = 
    record[:calendar_week] = "Week #{date.week}"
    record[:calendar_week_number_in_year] = date.week
    record[:calendar_month_name] = date.strftime("%B")
    record[:calendar_month_number_in_year] = date.month
    record[:calendar_year_month] = date.strftime("%Y-%m")
    record[:calendar_quarter] = "Q#{date.quarter}"
    record[:calendar_quarter_number_in_year] = date.quarter
    record[:calendar_year_quarter] = "#{date.strftime('%Y')}-#{record[:calendar_quarter]}"
    #record[:calendar_half_year] = 
    record[:calendar_year] = "#{date.year}"
    record[:fiscal_week] = "FY Week #{date.fiscal_year_week}"
    record[:fiscal_week_number_in_year] = date.fiscal_year_week
    record[:fiscal_month] = date.fiscal_year_month
    record[:fiscal_month_number_in_year] = date.fiscal_year_month
    record[:fiscal_year_month] = "FY#{date.fiscal_year}-" + date.fiscal_year_month.to_s.rjust(2, '0')
    record[:fiscal_quarter] = "FY Q#{date.fiscal_year_quarter}"
    record[:fiscal_year_quarter] = "FY#{date.fiscal_year}-Q#{date.fiscal_year_quarter}"
    record[:fiscal_year_quarter_number] = date.fiscal_year_quarter
    #record[:fiscal_half_year] = 
    record[:fiscal_year] = "FY#{date.fiscal_year}"
    record[:fiscal_year_number] = date.fiscal_year
    record[:holiday_indicator] = holiday_indicators.include?(date) ? 'Holiday' : 'Nonholiday'
    record[:weekday_indicator] = weekday_indicators[date.wday]
    record[:selling_season] = 'None'
    record[:major_event] = 'None'
    record[:sql_date_stamp] = date
    
    records << record
    date = date.tomorrow
  end
  records
end