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.



31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#official_work_daysObject (readonly)

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



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

def official_work_days
  @official_work_days
end

Class Method Details

.from_map(map_data) ⇒ Object



16
17
18
# 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

Instance Method Details

#avg_hours_per_day_worked(*subjects) ⇒ Object



65
66
67
# 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) ⇒ Object



61
62
63
# 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 = {}) ⇒ Object



40
41
42
# 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) ⇒ Object



69
70
71
# 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) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object



131
132
133
134
135
136
137
# 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) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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

#subjectsObject



139
140
141
# 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_workedObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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_ssvObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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_mdObject



73
74
75
# File 'lib/time_distribution/work_day_collection.rb', line 73

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

#to_ssvObject



77
78
79
80
81
82
83
84
85
# 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_weeksObject



105
106
107
108
109
110
111
112
113
114
115
116
# 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