class TimeDistribution::WorkDayCollection

Constants

MONTHS

Attributes

official_work_days[R]

@returns [Hash<symbol,Integer>] Hash from calendar month to the

> number of official work days in that month. Initialized to zero.

Public Class Methods

from_map(map_data) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 16
def self.from_map(map_data)
  self.new(*map_data.map { |t| WorkDay.from_map t })
end
new(*days, official_work_days: {}) click to toggle source
Calls superclass method
# File lib/time_distribution/work_day_collection.rb, line 31
def initialize(*days, official_work_days: {})
  super(days)
  provide_methods_for_setting_work_days_in_months
  @official_work_days = official_work_days.dup
  MONTHS.each do |m|
    @official_work_days[m] ||= 0
  end
end

Public Instance Methods

avg_hours_per_day_worked(*subjects) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 65
def avg_hours_per_day_worked(*subjects)
  hours(*subjects) / length.to_f
end
avg_hours_per_official_work_day(*subjects) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 61
def avg_hours_per_official_work_day(*subjects)
  hours(*subjects) / @official_work_days.values.inject(:+).to_f
end
days_to_yaml(opts = {}) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 40
def days_to_yaml(opts = {})
  map { |e| e.to_h }.to_yaml opts
end
hours(*subjects) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 69
def hours(*subjects)
  inject(0) { |sum, d| sum += d.to_hours(*subjects) }
end
hours_per_day_by_weeks(*task_types) { |last| ... } click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 118
def hours_per_day_by_weeks(*task_types)
  array = []
  work_days_by_weeks do |week|
    array << (
      week.map do |day|
        day.to_hours(*task_types)
      end
    )
    yield array.last if block_given?
  end
  array
end
hours_per_week(*task_types) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 131
def hours_per_week(*task_types)
  hours = []
  hours_per_day_by_weeks(*task_types) do |week|
    hours << week.inject(:+)
  end
  hours
end
hours_per_week_ssv(*task_types) click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 143
def hours_per_week_ssv(*task_types)
  string = ''
  hours = []
  week = 1
  work_days_by_weeks do |week_collection|
    subjects_this_week = week_collection.subjects
    unless task_types.empty?
      subjects_this_week &= task_types
    end
    string += (
      "# #{week_collection.first.date.strftime('%b %-d, %Y')}\n" +
      "# #{subjects_this_week.join(', ')}\n"
    )
    week_hours = week_collection.map do |day|
      day.to_hours(*task_types)
    end
    hours << week_hours.inject(:+)
    string += (
      "# " + week_hours.join(', ') + "\n" +
      format("%-10d%0.2f\n", week, hours.last)
    )
    week += 1
  end
  string += format("%-10s%0.2f\n", 'Avg', hours.inject(:+) / hours.length)
end
subjects() click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 139
def subjects
  map { |day| day.tasks.map { |task| task.subject } }.flatten.sort.uniq
end
time_worked() click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 44
def time_worked
  inject({}) do |hash, d|
    t = d.time_worked
    if t.respond_to?(:each_key)
      t.each_key do |key|
        hash[key] = Duration.new(0) unless hash[key]
        hash[key] += t[key]
      end
    else
      d_subject = d.tasks.first.subject
      hash[d_subject] = Duration.new(0) unless hash[d_subject]
      hash[d_subject] += t
    end
    hash
  end
end
time_worked_to_ssv() { |k| ... } click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 87
def time_worked_to_ssv
  string = ''
  time_worked.each do |k,j|
    string += format(
      "%-30s%10s\n",
      (
        if block_given?
          yield(k)
        else
          k
        end
      ),
      format("%0.2f", j.total / 3600.0)
    )
  end
  string
end
to_md() click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 73
def to_md
  inject('') { |s, d| s += "#{d.to_md}\n" }
end
to_ssv() click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 77
def to_ssv
  inject('') do |s, d|
    s += if block_given?
      "#{d.to_ssv { |key| yield(key) }}"
    else
      "#{d.to_ssv}"
    end
  end
end
work_days_by_weeks() { |last| ... } click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 105
def work_days_by_weeks
  (0..length / 7).inject([]) do |array, week|
    week_start = 7*week
    week_end = week_start+6
    days = self[week_start..week_end]
    next array if days.empty?

    array << WorkDayCollection.new(*days)
    yield array.last if block_given?
    array
  end
end

Private Instance Methods

provide_methods_for_setting_work_days_in_months() click to toggle source
# File lib/time_distribution/work_day_collection.rb, line 21
def provide_methods_for_setting_work_days_in_months
  MONTHS.each do |m|
    self.class().send(:define_method, "set_official_work_days_in_#{m.to_s}!") do |num_days|
      @official_work_days[m] = num_days
    end
  end
end