class Timely::TrackableDateSet

Attributes

dates[R]
dates_to_do[R]
end_date[R]
start_date[R]

Public Class Methods

from_params(date_string, duration_string = nil) click to toggle source

TODO: remove Initialize from a date + duration

# File lib/timely/trackable_date_set.rb, line 52
def self.from_params(date_string, duration_string = nil)
  duration = duration_string.to_i
  duration = 1 if duration.zero?
  new_for_date(date_string.to_date, duration: duration)
end
new(dates) click to toggle source

Pass in dates as array, range or any kind of enumerable

# File lib/timely/trackable_date_set.rb, line 31
def initialize(dates)
  # Sort so that start/end date are correct
  sorted_dates = dates.sort

  # Have to do this, because Set doesn't respond to :last
  # ...but .sort returns an array which does
  @start_date = sorted_dates.first
  @end_date = sorted_dates.last

  @dates = Set.new(sorted_dates)
  # track dates_to_do instead of dates_done... better fits common access patterns
  @dates_to_do = @dates.dup
end
new_for_date(date, opts = {}) click to toggle source
# File lib/timely/trackable_date_set.rb, line 45
def self.new_for_date(date, opts = {})
  duration = opts[:duration] || 1
  TrackableDateSet.new(date..(date + duration - 1))
end

Public Instance Methods

action_applied?(action) click to toggle source

Check if an action has been applied

# File lib/timely/trackable_date_set.rb, line 128
def action_applied?(action)
  actions_applied.include? action
end
all_done?() click to toggle source
# File lib/timely/trackable_date_set.rb, line 103
def all_done?
  @dates_to_do.empty?
end
apply_action(action) click to toggle source

Remember an action has been applied across the whole date set

# File lib/timely/trackable_date_set.rb, line 123
def apply_action(action)
  actions_applied << action
end
dates_done() click to toggle source

Find which dates ARE done

# File lib/timely/trackable_date_set.rb, line 66
def dates_done
  @dates - @dates_to_do
end
Also aliased as: find_done
do_once(action_name, opts = {}) { || ... } click to toggle source

Do something once within this tracked period

Will only consider job done when opts is true

action_name => Name to track
{:job_done_when} => Block to call, passed result of yield
# File lib/timely/trackable_date_set.rb, line 113
def do_once(action_name, opts = {})
  return if action_applied?(action_name)

  result = yield

  job_done = opts[:job_done_when].blank? || opts[:job_done_when].call(result)
  apply_action(action_name) if job_done
end
done_dates?(date_or_date_range) click to toggle source
# File lib/timely/trackable_date_set.rb, line 95
def done_dates?(date_or_date_range)
  if date_or_date_range.is_a?(Enumerable)
    @dates_to_do.none? { |date_to_do| date_or_date_range.include?(date_to_do) }
  else
    !@dates_to_do.include? date_or_date_range
  end
end
duration() click to toggle source
# File lib/timely/trackable_date_set.rb, line 132
def duration
  @dates.size
end
Also aliased as: number_of_nights
each_date() { |date| ... } click to toggle source

Yield each date in the whole set

# File lib/timely/trackable_date_set.rb, line 77
def each_date
  @dates.each { |date| yield date }
end
each_date_to_do() { |date| ... } click to toggle source

Yield each date to do

# File lib/timely/trackable_date_set.rb, line 72
def each_date_to_do
  @dates_to_do.each { |date| yield date }
end
find_done()
Alias for: dates_done
find_to_do() click to toggle source

Find the set of dates which are YET to do

# File lib/timely/trackable_date_set.rb, line 61
def find_to_do
  @dates_to_do
end
number_of_nights()
Alias for: duration
set_all_done!() click to toggle source

Set all done!

# File lib/timely/trackable_date_set.rb, line 91
def set_all_done!
  @dates_to_do.clear
end
set_date_done!(date) click to toggle source
# File lib/timely/trackable_date_set.rb, line 86
def set_date_done!(date)
  @dates_to_do.delete(date)
end
set_dates_done!(date_enum) click to toggle source

Set dates as done

# File lib/timely/trackable_date_set.rb, line 82
def set_dates_done!(date_enum)
  @dates_to_do.subtract(date_enum)
end

Private Instance Methods

actions_applied() click to toggle source

Can't say whole_period anymore… it's not necessarily sequential dates

def whole_period
  self.dates
end
# File lib/timely/trackable_date_set.rb, line 144
def actions_applied
  @actions_applied ||= Set.new
end