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

Returns a new instance of 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
18
19
# 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]
    scalar.times.reduce date do |date, _|
      DateDiff.advance intervals: factor, unit: unit, from: date
    end
  end
end

#finished?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/mudrat_projector/schedule.rb', line 21

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

#serializeObject



34
35
36
37
38
39
# File 'lib/mudrat_projector/schedule.rb', line 34

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

#slice(range, &block) ⇒ Object



41
42
43
44
45
46
# File 'lib/mudrat_projector/schedule.rb', line 41

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



25
26
27
28
29
30
31
32
# File 'lib/mudrat_projector/schedule.rb', line 25

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 scalar
  final_prorate /= scalar
  ([1] * full_units).tap do |list|
    list.push final_prorate unless final_prorate.zero?
  end
end