Class: SOF::TimeSpan
- Inherits:
-
Object
- Object
- SOF::TimeSpan
- Extended by:
- Forwardable
- Defined in:
- lib/sof/time_span.rb
Overview
This class is not intended to be referenced directly. This is an internal implementation of Cycle behavior.
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidPeriod, TimeSpanNothing, TimeSpanOne
Instance Attribute Summary collapse
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Class Method Summary collapse
-
.for(count, period) ⇒ Object
Return a time_span for the given count and period.
-
.notation(hash) ⇒ Object
Return a notation string from a hash.
-
.notation_id_from_name(name) ⇒ Object
Return the notation character for the given period name.
Instance Method Summary collapse
- #begin_date_of_period(date) ⇒ Object
- #end_date_of_period(date) ⇒ Object
-
#final_date(date) ⇒ Object
Return a date according to the rules of the time_span.
-
#initialize(count, period_id) ⇒ TimeSpan
constructor
A new instance of TimeSpan.
- #notation ⇒ Object
-
#period_count ⇒ Object
Integer value for the period count or nil.
- #to_h ⇒ Object
Constructor Details
#initialize(count, period_id) ⇒ TimeSpan
Returns a new instance of TimeSpan.
190 191 192 193 |
# File 'lib/sof/time_span.rb', line 190 def initialize(count, period_id) @count = Integer(count, exception: false) @window = DatePeriod.for(period_count, period_id) end |
Instance Attribute Details
#window ⇒ Object (readonly)
Returns the value of attribute window.
194 195 196 |
# File 'lib/sof/time_span.rb', line 194 def window @window end |
Class Method Details
.for(count, period) ⇒ Object
Return a time_span for the given count and period
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sof/time_span.rb', line 19 def for(count, period) case count.to_i when 0 TimeSpanNothing when 1 TimeSpanOne else self end.new(count, period) end |
.notation(hash) ⇒ Object
Return a notation string from a hash
31 32 33 34 35 36 37 38 |
# File 'lib/sof/time_span.rb', line 31 def notation(hash) return unless hash.key?(:period) && hash[:period].present? [ hash.fetch(:period_count) { 1 }, notation_id_from_name(hash[:period]) ].compact.join end |
.notation_id_from_name(name) ⇒ Object
Return the notation character for the given period name
41 42 43 44 45 46 47 48 49 |
# File 'lib/sof/time_span.rb', line 41 def notation_id_from_name(name) type = DatePeriod.types.find do |klass| klass.period.to_s == name.to_s end raise InvalidPeriod, "'#{name}' is not a valid period" unless type type.code end |
Instance Method Details
#begin_date_of_period(date) ⇒ Object
202 203 204 |
# File 'lib/sof/time_span.rb', line 202 def begin_date_of_period(date) window.beginning_of_period(date) end |
#end_date_of_period(date) ⇒ Object
198 199 200 |
# File 'lib/sof/time_span.rb', line 198 def end_date_of_period(date) window.end_of_period(date) end |
#final_date(date) ⇒ Object
Return a date according to the rules of the time_span
214 215 216 217 218 |
# File 'lib/sof/time_span.rb', line 214 def final_date(date) return unless period window.end_date(date.to_date) end |
#notation ⇒ Object
52 53 54 |
# File 'lib/sof/time_span.rb', line 52 def notation [period_count, code].join end |
#period_count ⇒ Object
Integer value for the period count or nil
207 208 209 |
# File 'lib/sof/time_span.rb', line 207 def period_count @count end |
#to_h ⇒ Object
220 221 222 223 224 225 |
# File 'lib/sof/time_span.rb', line 220 def to_h { period:, period_count: } end |