Class: MudratProjector::Schedule
- Inherits:
-
Object
- Object
- MudratProjector::Schedule
- Defined in:
- lib/mudrat_projector/schedule.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#scalar ⇒ Object
readonly
Returns the value of attribute scalar.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
- #advance_over(range) ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(params = {}) ⇒ Schedule
constructor
A new instance of Schedule.
- #serialize ⇒ Object
- #slice(range, &block) ⇒ Object
- #split_count_over(range) ⇒ Object
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
3 4 5 |
# File 'lib/mudrat_projector/schedule.rb', line 3 def count @count end |
#scalar ⇒ Object (readonly)
Returns the value of attribute scalar.
3 4 5 |
# File 'lib/mudrat_projector/schedule.rb', line 3 def scalar @scalar end |
#unit ⇒ Object (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 |
#serialize ⇒ Object
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 |