Class: Saulabs::Reportable::Grouping

Inherits:
Object
  • Object
show all
Defined in:
lib/saulabs/reportable/grouping.rb

Overview

The grouping specifies which records are grouped into one ReportingPeriod.

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ Grouping

Initializes a new grouping.

Parameters:

  • identifier (Symbol)

    the identifier of the grouping (one of :hour, :day, :week or :month)

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/saulabs/reportable/grouping.rb', line 14

def initialize(identifier)
  raise ArgumentError.new("Invalid grouping #{identifier}") unless [:hour, :day, :week, :month].include?(identifier)
  @identifier = identifier
end

Instance Method Details

#date_parts_from_db_string(db_string) ⇒ Array<Fixnum>

Gets an array of date parts from a DB string.

Parameters:

  • db_string (String)

    the DB string to get the date parts from

Returns:

  • (Array<Fixnum>)

    array of numbers that represent the values of the date



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/saulabs/reportable/grouping.rb', line 36

def date_parts_from_db_string(db_string)
  case ActiveRecord::Base.connection.adapter_name
    when /mysql/i
      from_mysql_db_string(db_string)
    when /sqlite/i
      from_sqlite_db_string(db_string)
    when /postgres/i
      from_postgresql_db_string(db_string)
    when /mssql/i, /sqlserver/i
      from_sqlserver_db_string(db_string)
  end
end

#identifierSymbol

Gets the identifier of the grouping.

Returns:

  • (Symbol)

    the identifier of the grouping.



24
25
26
# File 'lib/saulabs/reportable/grouping.rb', line 24

def identifier
  @identifier
end

#to_sql(date_column) ⇒ Object

Converts the grouping into a DB specific string that can be used to group records.

Parameters:

  • date_column (String)

    the name of the DB column that holds the date



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/saulabs/reportable/grouping.rb', line 54

def to_sql(date_column)
  case ActiveRecord::Base.connection.adapter_name
    when /mysql/i
      mysql_format(date_column)
    when /sqlite/i
      sqlite_format(date_column)
    when /postgres/i
      postgresql_format(date_column)
    when /mssql/i, /sqlserver/i
      sqlserver_format(date_column)
  end
end