Class: Timerator
- Inherits:
-
Object
- Object
- Timerator
- Defined in:
- lib/timerator/timerator.rb
Constant Summary collapse
- TIME_SPANS =
{ :second => 1, :minute => 60, :hour => 3600, :day => 86_400, :week => 604_800 }
Instance Method Summary collapse
- #each(period = :second) ⇒ Object
-
#initialize(start_date = Time.now, end_date = Time.now, inclusive = false) ⇒ Timerator
constructor
A new instance of Timerator.
- #slice(period = :second) ⇒ Object
Constructor Details
#initialize(start_date = Time.now, end_date = Time.now, inclusive = false) ⇒ Timerator
Returns a new instance of Timerator.
11 12 13 14 15 |
# File 'lib/timerator/timerator.rb', line 11 def initialize start_date = Time.now, end_date = Time.now, inclusive = false @start_date = start_date @end_date = end_date @inclusive = inclusive end |
Instance Method Details
#each(period = :second) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/timerator/timerator.rb', line 29 def each period = :second diff = (@end_date - @start_date) - inclusive_modifier diff /= TIME_SPANS[period] (0..diff).each do |tick| span_start = @start_date + TIME_SPANS[period] * tick span_end = @start_date + TIME_SPANS[period] * (tick + 1) yield span_start, [span_end,@end_date].min end end |
#slice(period = :second) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/timerator/timerator.rb', line 17 def slice period = :second diff = (@end_date - @start_date) - inclusive_modifier diff /= TIME_SPANS[period] (0..diff).map do |tick| span_start = @start_date + TIME_SPANS[period] * tick span_end = @start_date + TIME_SPANS[period] * (tick + 1) (span_start..[span_end,@end_date].min) end end |