Module: Stagger

Defined in:
lib/stagger.rb,
lib/stagger/version.rb

Constant Summary collapse

SECONDS_IN_DAY =
86_400
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.distribute(items, number_of_days, delay: 0) ⇒ Array

Evenly distributes items acros business days

Parameters:

  • items (Array, items)

    to stagger

  • number (Integer, number_of_days)

    of business days to distribute within

  • number (Integer, delay)

    of seconds to delay the staggering

Returns:

  • (Array)

    array of arrays, where first subarray element is a scheduled DateTime, second is the staggered item



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stagger.rb', line 11

def distribute(items, number_of_days, delay: 0)
  return [] if Array(items).empty? || number_of_days.to_i < 1
  time = get_starting_time(delay)
  period_in_seconds = get_period_in_seconds(items.size, number_of_days, time)
  items.reduce [] do |arr, item|
    if business_day?(time)
      arr << [item, time]
      time = time + period_in_seconds
      arr
    else
      time = time + SECONDS_IN_DAY
      redo
    end
  end
end