Class: TimeDistribution::WorkDayCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/time_distribution/work_day_collection.rb

Constant Summary collapse

MONTHS =
[:january, :february, :march, :april, :may, :june, :july, :august, :september, :october, :november, :december]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*days, official_work_days: {}) ⇒ WorkDayCollection

Returns a new instance of WorkDayCollection.



30
31
32
33
34
35
36
37
# File 'lib/time_distribution/work_day_collection.rb', line 30

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

Instance Attribute Details

#official_work_daysObject (readonly)

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



11
12
13
# File 'lib/time_distribution/work_day_collection.rb', line 11

def official_work_days
  @official_work_days
end

Class Method Details

.from_map(map_data) ⇒ Object



15
16
17
# File 'lib/time_distribution/work_day_collection.rb', line 15

def self.from_map(map_data)
  self.new(*map_data.map { |t| WorkDay.from_map t })
end

Instance Method Details

#avg_hours_per_day_worked(*subjects) ⇒ Object



60
61
62
# File 'lib/time_distribution/work_day_collection.rb', line 60

def avg_hours_per_day_worked(*subjects)
  hours(*subjects) / length.to_f
end

#avg_hours_per_official_work_day(*subjects) ⇒ Object



56
57
58
# File 'lib/time_distribution/work_day_collection.rb', line 56

def avg_hours_per_official_work_day(*subjects)
  hours(*subjects) / @official_work_days.values.inject(:+).to_f
end

#hours(*subjects) ⇒ Object



64
65
66
# File 'lib/time_distribution/work_day_collection.rb', line 64

def hours(*subjects)
  inject(0) { |sum, d| sum += d.to_hours(*subjects) }
end

#hours_per_day_by_weeks(*task_types) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/time_distribution/work_day_collection.rb', line 113

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) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/time_distribution/work_day_collection.rb', line 126

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) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/time_distribution/work_day_collection.rb', line 138

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

#subjectsObject



134
135
136
# File 'lib/time_distribution/work_day_collection.rb', line 134

def subjects
  map { |day| day.tasks.map { |task| task.subject } }.flatten.sort.uniq
end

#time_workedObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/time_distribution/work_day_collection.rb', line 39

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_ssvObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/time_distribution/work_day_collection.rb', line 82

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_mdObject



68
69
70
# File 'lib/time_distribution/work_day_collection.rb', line 68

def to_md
  inject('') { |s, d| s += "#{d.to_md}\n" }
end

#to_ssvObject



72
73
74
75
76
77
78
79
80
# File 'lib/time_distribution/work_day_collection.rb', line 72

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_weeksObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/time_distribution/work_day_collection.rb', line 100

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