Module: Noventius::Extension::DateQuery::InstanceMethods

Defined in:
lib/noventius/extensions/date_query.rb

Constant Summary collapse

SQL_FUNCTIONS =
{
  'month' => "DATE_TRUNC('month', <%column%>::timestamptz AT TIME ZONE {time_zone})",
  'day' => "DATE_TRUNC('day', <%column%>::timestamptz AT TIME ZONE {time_zone})",
  'dow' => 'EXTRACT(DOW from <%column%>::timestamptz AT TIME ZONE {time_zone})::integer',
  'hour' => 'EXTRACT(HOUR from <%column%>::timestamptz AT TIME ZONE {time_zone})::integer',
  'moy' => 'EXTRACT(MONTH from <%column%>::timestamptz AT TIME ZONE {time_zone})::integer'
}

Instance Method Summary collapse

Instance Method Details

#date_extract(component:, column:, time_zone: 'America/Montevideo') ⇒ String

SQL function for the extraction of the desired timestamp component

Parameters:

  • component (String)

    The Date component to extract

  • column (String)

    The column that has the timestamp

  • time_zone (String) (defaults to: 'America/Montevideo')

    The time_zone of the timestamp. Default: ‘America/Montevideo’

Returns:

  • (String)

    The SQL function



48
49
50
51
52
53
54
# File 'lib/noventius/extensions/date_query.rb', line 48

def date_extract(component:, column:, time_zone: 'America/Montevideo')
  sql_function = SQL_FUNCTIONS[component].dup

  Class.new(OpenStruct) {
    include Noventius::Report::Interpolator
  }.new(column: column, time_zone: time_zone).interpolate(sql_function)
end

#date_extract_optionsHash

The different component that can be extracted from a timestamp

Returns:

  • (Hash)

    The components



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/noventius/extensions/date_query.rb', line 29

def date_extract_options
  [
    {
      'Day' => 'day',
      'Month' => 'month',
      'Day of week' => 'dow',
      'Hour of day' => 'hour',
      'Month of year' => 'moy'
    }
  ]
end