Class: Saulabs::Reportable::ReportingPeriod
- Inherits:
-
Object
- Object
- Saulabs::Reportable::ReportingPeriod
- 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
-
#date_time ⇒ Object
readonly
The actual +DateTime the reporting period represents.
-
#grouping ⇒ Object
readonly
The Grouping of the reporting period.
Class Method Summary collapse
-
.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).
-
.from_db_string(grouping, db_string) ⇒ Saulabs::Reportable::ReportingPeriod
Gets a reporting period from a DB date string.
Instance Method Summary collapse
-
#<(other) ⇒ Boolean
Gets whether the reporting period
other
is smaller to the current one. -
#==(other) ⇒ Boolean
Gets whether the reporting period
other
is equal to the current one. -
#initialize(grouping, date_time = nil) ⇒ ReportingPeriod
constructor
Initializes a new reporting period.
-
#last_date_time ⇒ DateTime
Gets the latest point in time that is included the reporting period.
-
#next ⇒ Saulabs::Reportable::ReportingPeriod
Gets the next reporting period.
-
#offset(offset) ⇒ Saulabs::Reportable::ReportingPeriod
Gets a reporting period relative to the current one.
-
#previous ⇒ Saulabs::Reportable::ReportingPeriod
Gets the previous reporting period.
Constructor Details
#initialize(grouping, date_time = nil) ⇒ ReportingPeriod
Initializes a new reporting period.
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_time ⇒ Object (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 |
#grouping ⇒ Object (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).
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.
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.
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.
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_time ⇒ DateTime
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.
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 |
#next ⇒ Saulabs::Reportable::ReportingPeriod
Gets the next reporting period.
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.
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 |
#previous ⇒ Saulabs::Reportable::ReportingPeriod
Gets the previous reporting period.
101 102 103 |
# File 'lib/saulabs/reportable/reporting_period.rb', line 101 def previous self.offset(-1) end |