Class: Saulabs::Reportable::ReportingPeriod

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

Overview

A reporting period is a specific hour or a specific day etc. depending on the used Grouping.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grouping, date_time = nil) ⇒ ReportingPeriod

Initializes a new reporting period.

Parameters:

  • grouping (Saulabs::Reportable::Grouping)

    the grouping the generate the reporting period for

  • date_time (DateTime) (defaults to: nil)

    the DateTime to generate the reporting period for



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

def initialize(grouping, date_time = nil)
  @grouping  = grouping
  @date_time = parse_date_time(date_time || DateTime.now)
end

Instance Attribute Details

#date_timeObject (readonly)

The actual +DateTime the reporting period represents



11
12
13
# File 'lib/saulabs/reportable/reporting_period.rb', line 11

def date_time
  @date_time
end

#groupingObject (readonly)

The Grouping of the reporting period



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

def grouping
  @grouping
end

Class Method Details

.first(grouping, limit, end_date = nil) ⇒ Saulabs::Reportable::ReportingPeriod

Gets the first reporting period for a grouping and a limit (optionally relative to and end date).

Parameters:

  • grouping (Saulabs::ReportingPeriod::Grouping)

    the grouping to get the first reporting period for

  • limit (Fixnum)

    the limit to get the first reporting period for

  • end_date (DateTime) (defaults to: nil)

    the end date to get the first reporting period for (the first reporting period is then end_date - limit * grouping)

Returns:



58
59
60
# File 'lib/saulabs/reportable/reporting_period.rb', line 58

def self.first(grouping, limit, end_date = nil)
  self.new(grouping, end_date).offset(-limit)
end

.from_db_string(grouping, db_string) ⇒ Saulabs::Reportable::ReportingPeriod

Gets a reporting period from a DB date string.

Parameters:

  • grouping (Saulabs::Reportable::Grouping)

    the grouping to get the reporting period for

  • db_string (String)

    the DB string to parse and get the reporting period for

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/saulabs/reportable/reporting_period.rb', line 72

def self.from_db_string(grouping, db_string)
  return self.new(grouping, db_string) if db_string.is_a?(Date) || db_string.is_a?(Time)
  parts = grouping.date_parts_from_db_string(db_string.to_s)
  case grouping.identifier
    when :hour
      self.new(grouping, DateTime.new(parts[0], parts[1], parts[2], parts[3], 0, 0))
    when :day
      self.new(grouping, Date.new(parts[0], parts[1], parts[2]))
    when :week
      self.new(grouping, Date.commercial(parts[0], parts[1], 1))
    when :month
      self.new(grouping, Date.new(parts[0], parts[1], 1))
  end
end

Instance Method Details

#<(other) ⇒ Boolean

Gets whether the reporting period other is smaller to the current one.

Parameters:

Returns:

  • (Boolean)

    true if other is smaller to the current reporting period, false otherwise



131
132
133
134
135
136
137
138
139
# File 'lib/saulabs/reportable/reporting_period.rb', line 131

def <(other)
  if other.is_a?(Saulabs::Reportable::ReportingPeriod)
    return @date_time < other.date_time
  elsif other.is_a?(Time) || other.is_a?(DateTime)
    @date_time < parse_date_time(other)
  else
    raise ArgumentError.new("Can only compare instances of #{self.class.name}")
  end
end

#==(other) ⇒ Boolean

Gets whether the reporting period other is equal to the current one.

Parameters:

Returns:

  • (Boolean)

    true if other is equal to the current reporting period, false otherwise



113
114
115
116
117
118
119
120
121
# File 'lib/saulabs/reportable/reporting_period.rb', line 113

def ==(other)
  if other.is_a?(Saulabs::Reportable::ReportingPeriod)
    @date_time == other.date_time && @grouping.identifier == other.grouping.identifier
  elsif other.is_a?(Time) || other.is_a?(DateTime)
    @date_time == parse_date_time(other)
  else
    raise ArgumentError.new("Can only compare instances of #{self.class.name}")
  end
end

#last_date_timeDateTime

Gets the latest point in time that is included the reporting period. The latest point in time included in a reporting period for grouping hour would be that hour and 59 minutes and 59 seconds.

Returns:

  • (DateTime)

    the latest point in time that is included in the reporting period



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/saulabs/reportable/reporting_period.rb', line 147

def last_date_time
  case @grouping.identifier
    when :hour
      DateTime.new(@date_time.year, @date_time.month, @date_time.day, @date_time.hour, 59, 59)
    when :day
      DateTime.new(@date_time.year, @date_time.month, @date_time.day, 23, 59, 59)
    when :week
      date_time = (@date_time - @date_time.wday.days) + 7.days
      Date.new(date_time.year, date_time.month, date_time.day)
    when :month
      Date.new(@date_time.year, @date_time.month, (Date.new(@date_time.year, 12, 31) << (12 - @date_time.month)).day)
  end
end

#nextSaulabs::Reportable::ReportingPeriod

Gets the next reporting period.

Returns:



92
93
94
# File 'lib/saulabs/reportable/reporting_period.rb', line 92

def next
  self.offset(1)
end

#offset(offset) ⇒ Saulabs::Reportable::ReportingPeriod

Gets a reporting period relative to the current one.

Examples:

Getting the reporting period one week later


reporting_period = Saulabs::Reportable::ReportingPeriod.new(:week, DateTime.now)
next_week = reporting_period.offset(1)

Parameters:

  • offset (Fixnum)

    the offset to get the reporting period for

Returns:



42
43
44
# File 'lib/saulabs/reportable/reporting_period.rb', line 42

def offset(offset)
  self.class.new(@grouping, @date_time + offset.send(@grouping.identifier))
end

#previousSaulabs::Reportable::ReportingPeriod

Gets the previous reporting period.

Returns:



101
102
103
# File 'lib/saulabs/reportable/reporting_period.rb', line 101

def previous
  self.offset(-1)
end