Class: MudratProjector::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/mudrat_projector/schedule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Schedule



5
6
7
8
9
# File 'lib/mudrat_projector/schedule.rb', line 5

def initialize params = {}
  @count  = params.fetch :count, nil
  @scalar = params.fetch :scalar
  @unit   = params.fetch :unit
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/mudrat_projector/schedule.rb', line 3

def count
  @count
end

#scalarObject (readonly)

Returns the value of attribute scalar.



3
4
5
# File 'lib/mudrat_projector/schedule.rb', line 3

def scalar
  @scalar
end

#unitObject (readonly)

Returns the value of attribute unit.



3
4
5
# File 'lib/mudrat_projector/schedule.rb', line 3

def unit
  @unit
end

Instance Method Details

#advance_over(range) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/mudrat_projector/schedule.rb', line 11

def advance_over range
  split_count_over(range).reduce range.begin do |date, factor|
    @count -= factor if @count
    yield [date, factor]
    DateDiff.advance intervals: factor, unit: unit, from: date
  end
end

#finished?Boolean



19
20
21
# File 'lib/mudrat_projector/schedule.rb', line 19

def finished?
  (count.nil? || count > 0) ? false : true
end

#serializeObject



31
32
33
34
35
36
# File 'lib/mudrat_projector/schedule.rb', line 31

def serialize
  {
    scalar: scalar,
    unit:   unit,
  }.tap { |h| h[:count] = count if count }
end

#slice(range, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/mudrat_projector/schedule.rb', line 38

def slice range, &block
  bits = []
  advance_over range do |date, factor| bits.push [date, factor]; end
  leftover = finished? ? nil : serialize
  [bits, leftover]
end

#split_count_over(range) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/mudrat_projector/schedule.rb', line 23

def split_count_over range
  diff = DateDiff.date_diff unit: unit, from: range.begin, to: range.end
  full_units, final_prorate = [@count, diff].compact.min.divmod 1
  ([1] * full_units).tap do |list|
    list.push final_prorate unless final_prorate.zero?
  end
end