Class: Biz::Timeline::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/biz/timeline/abstract.rb

Direct Known Subclasses

Backward, Forward

Instance Method Summary collapse

Constructor Details

#initialize(periods) ⇒ Abstract

Returns a new instance of Abstract.



7
8
9
# File 'lib/biz/timeline/abstract.rb', line 7

def initialize(periods)
  @periods = periods.lazy
end

Instance Method Details

#for(duration) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/biz/timeline/abstract.rb', line 23

def for(duration)
  return enum_for(:for, duration) unless block_given?

  remaining = duration

  periods
    .each do |period|
      if overflow?(period, remaining)
        remaining = Duration.new(0)

        yield period
      else
        scoped_period = period & duration_period(period, remaining)

        remaining -= scoped_period.duration

        yield scoped_period unless scoped_period.disjoint?

        break unless remaining.positive?
      end
    end
end

#until(terminus) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/biz/timeline/abstract.rb', line 11

def until(terminus)
  return enum_for(:until, terminus) unless block_given?

  periods
    .map { |period| period & comparison_period(period, terminus) }
    .each do |period|
      break if occurred?(period, terminus)

      yield period unless period.disjoint?
    end
end