Class: SOF::Cycle::TimeSpan
- Inherits:
-
Object
- Object
- SOF::Cycle::TimeSpan
- Extended by:
- Forwardable
- Defined in:
- lib/sof/cycle/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.
-
.period_registry ⇒ Object
The registry of period classes, owned by DatePeriod.
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.
272 273 274 275 |
# File 'lib/sof/cycle/time_span.rb', line 272 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.
276 277 278 |
# File 'lib/sof/cycle/time_span.rb', line 276 def window @window end |
Class Method Details
.for(count, period) ⇒ Object
Return a time_span for the given count and period
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sof/cycle/time_span.rb', line 93 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
105 106 107 108 109 110 111 112 |
# File 'lib/sof/cycle/time_span.rb', line 105 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
120 121 122 123 |
# File 'lib/sof/cycle/time_span.rb', line 120 def notation_id_from_name(name) period_registry.for_period(name)&.code || raise(InvalidPeriod, "'#{name}' is not a valid period") end |
.period_registry ⇒ Object
The registry of period classes, owned by DatePeriod. Exposed here because DatePeriod is a private constant, so callers outside cannot name it — the parser needs the code alternation to build its pattern.
117 |
# File 'lib/sof/cycle/time_span.rb', line 117 def period_registry = PeriodRegistry.instance |
Instance Method Details
#begin_date_of_period(date) ⇒ Object
284 285 286 |
# File 'lib/sof/cycle/time_span.rb', line 284 def begin_date_of_period(date) window.beginning_of_period(date) end |
#end_date_of_period(date) ⇒ Object
280 281 282 |
# File 'lib/sof/cycle/time_span.rb', line 280 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
296 297 298 299 300 |
# File 'lib/sof/cycle/time_span.rb', line 296 def final_date(date) return unless period window.end_date(date.to_date) end |
#notation ⇒ Object
126 127 128 |
# File 'lib/sof/cycle/time_span.rb', line 126 def notation [period_count, code].join end |
#period_count ⇒ Object
Integer value for the period count or nil
289 290 291 |
# File 'lib/sof/cycle/time_span.rb', line 289 def period_count @count end |
#to_h ⇒ Object
302 303 304 305 306 307 |
# File 'lib/sof/cycle/time_span.rb', line 302 def to_h { period:, period_count: } end |