timeboss 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +23 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.gitignore +5 -0
- data/.replit +2 -0
- data/.rspec +2 -0
- data/.travis.yml +16 -0
- data/.yardopts +1 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +233 -0
- data/Rakefile +5 -0
- data/bin/tbsh +15 -0
- data/lib/tasks/calendars.rake +22 -0
- data/lib/tasks/timeboss.rake +6 -0
- data/lib/timeboss.rb +6 -0
- data/lib/timeboss/calendar.rb +64 -0
- data/lib/timeboss/calendar/day.rb +48 -0
- data/lib/timeboss/calendar/half.rb +22 -0
- data/lib/timeboss/calendar/month.rb +22 -0
- data/lib/timeboss/calendar/parser.rb +53 -0
- data/lib/timeboss/calendar/period.rb +83 -0
- data/lib/timeboss/calendar/quarter.rb +22 -0
- data/lib/timeboss/calendar/support/formatter.rb +33 -0
- data/lib/timeboss/calendar/support/month_basis.rb +21 -0
- data/lib/timeboss/calendar/support/monthly_unit.rb +55 -0
- data/lib/timeboss/calendar/support/navigable.rb +72 -0
- data/lib/timeboss/calendar/support/shiftable.rb +241 -0
- data/lib/timeboss/calendar/support/translatable.rb +93 -0
- data/lib/timeboss/calendar/support/unit.rb +88 -0
- data/lib/timeboss/calendar/waypoints.rb +12 -0
- data/lib/timeboss/calendar/waypoints/absolute.rb +113 -0
- data/lib/timeboss/calendar/waypoints/relative.rb +267 -0
- data/lib/timeboss/calendar/week.rb +53 -0
- data/lib/timeboss/calendar/year.rb +18 -0
- data/lib/timeboss/calendars.rb +53 -0
- data/lib/timeboss/calendars/broadcast.rb +32 -0
- data/lib/timeboss/calendars/gregorian.rb +30 -0
- data/lib/timeboss/support/shellable.rb +17 -0
- data/lib/timeboss/version.rb +4 -0
- data/spec/calendar/day_spec.rb +60 -0
- data/spec/calendar/quarter_spec.rb +32 -0
- data/spec/calendar/support/monthly_unit_spec.rb +85 -0
- data/spec/calendar/support/unit_spec.rb +90 -0
- data/spec/calendar/week_spec.rb +80 -0
- data/spec/calendars/broadcast_spec.rb +796 -0
- data/spec/calendars/gregorian_spec.rb +684 -0
- data/spec/calendars_spec.rb +50 -0
- data/spec/spec_helper.rb +12 -0
- data/timeboss.gemspec +31 -0
- metadata +216 -0
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TimeBoss
|
3
|
+
class Calendar
|
4
|
+
module Waypoints
|
5
|
+
module Absolute
|
6
|
+
%i[month quarter half year].each do |type|
|
7
|
+
klass = TimeBoss::Calendar.const_get(type.to_s.classify)
|
8
|
+
size = klass.const_get("NUM_MONTHS")
|
9
|
+
|
10
|
+
define_method type do |year_index, index = 1|
|
11
|
+
month = (index * size) - size + 1
|
12
|
+
months = (month .. month + size - 1).map { |i| basis.new(year_index, i) }
|
13
|
+
klass.new(self, year_index, index, months.first.start_date, months.last.end_date)
|
14
|
+
end
|
15
|
+
|
16
|
+
define_method "#{type}_for" do |date|
|
17
|
+
window = send(type, date.year - 1, 1)
|
18
|
+
while true
|
19
|
+
break window if window.to_range.include?(date)
|
20
|
+
window = window.next
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get the specified week by index within the specified year.
|
26
|
+
# @param year_index [Integer] the year to examine
|
27
|
+
# @param index [Integer] the index of the week within the year
|
28
|
+
# @return [Calendar::Week]
|
29
|
+
def week(year_index, index)
|
30
|
+
year(year_index).weeks[index - 1]
|
31
|
+
end
|
32
|
+
|
33
|
+
# Get the week that contains the specified date.
|
34
|
+
# @param date [Date] the date for which to locate the calendar week
|
35
|
+
# @return [Calendar::Week]
|
36
|
+
def week_for(date)
|
37
|
+
year_for(date).weeks.find { |w| w.to_range.include?(date) }
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get the specified day by index within the specified year.
|
41
|
+
# @param year_index [Integer] the year to examine
|
42
|
+
# @param index [Integer] the index of the day within the year
|
43
|
+
# @return [Calendar::Day]
|
44
|
+
def day(year_index, index)
|
45
|
+
year(year_index).days[index - 1]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Get the day that contains the specified date.
|
49
|
+
# @param date [Date] the date for which to locate the calendar day
|
50
|
+
# @return [Calendar::Day]
|
51
|
+
def day_for(date)
|
52
|
+
Day.new(self, date)
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# i hate this
|
57
|
+
#
|
58
|
+
|
59
|
+
### Month
|
60
|
+
|
61
|
+
# @!method month
|
62
|
+
# Get the specified month by index within the specified year
|
63
|
+
# @param year_index [Integer] the year to examine
|
64
|
+
# @param index [Integer] the index of the month within the year
|
65
|
+
# @return [Calendar::Month]
|
66
|
+
|
67
|
+
# @!method month_for
|
68
|
+
# Get the month that contains the specified date.
|
69
|
+
# @param date [Date] the date for which to locate the calendar month
|
70
|
+
# @return [Calendar::Month]
|
71
|
+
|
72
|
+
### Quarter
|
73
|
+
|
74
|
+
# @!method quarter
|
75
|
+
# Get the specified quarter by index within the specified year
|
76
|
+
# @param year_index [Integer] the year to examine
|
77
|
+
# @param index [Integer] the index of the quarter within the year
|
78
|
+
# @return [Calendar::Quarter]
|
79
|
+
|
80
|
+
# @!method quarter_for
|
81
|
+
# Get the quarter that contains the specified date.
|
82
|
+
# @param date [Date] the date for which to locate the calendar quarter
|
83
|
+
# @return [Calendar::Quarter]
|
84
|
+
|
85
|
+
### Half
|
86
|
+
|
87
|
+
# @!method half
|
88
|
+
# Get the specified half by index within the specified year
|
89
|
+
# @param year_index [Integer] the year to examine
|
90
|
+
# @param index [Integer] the index of the half within the year
|
91
|
+
# @return [Calendar::Half]
|
92
|
+
|
93
|
+
# @!method half_for
|
94
|
+
# Get the half that contains the specified date.
|
95
|
+
# @param date [Date] the date for which to locate the calendar half
|
96
|
+
# @return [Calendar::Half]
|
97
|
+
|
98
|
+
### Year
|
99
|
+
|
100
|
+
# @!method year
|
101
|
+
# Get the specified year by index within the specified year
|
102
|
+
# @param year_index [Integer] the year to examine
|
103
|
+
# @param index [Integer] the index of the year within the year
|
104
|
+
# @return [Calendar::Year]
|
105
|
+
|
106
|
+
# @!method year_for
|
107
|
+
# Get the year that contains the specified date.
|
108
|
+
# @param date [Date] the date for which to locate the calendar year
|
109
|
+
# @return [Calendar::Year]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,267 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TimeBoss
|
3
|
+
class Calendar
|
4
|
+
module Waypoints
|
5
|
+
module Relative
|
6
|
+
%i[day week month quarter half year].each do |type|
|
7
|
+
types = type.to_s.pluralize
|
8
|
+
|
9
|
+
define_method("this_#{type}") { send("#{type}_for", Date.today) }
|
10
|
+
define_method("last_#{type}") { send("this_#{type}").previous }
|
11
|
+
define_method("next_#{type}") { send("this_#{type}").next }
|
12
|
+
|
13
|
+
define_method("#{types}_for") { |p| send("#{type}_for", p.start_date).until(p.end_date) }
|
14
|
+
|
15
|
+
define_method("#{types}_back") { |q| send("this_#{type}").previous(q) }
|
16
|
+
define_method("#{types}_ago") { |q| send("this_#{type}").ago(q) }
|
17
|
+
|
18
|
+
define_method("#{types}_forward") { |q| send("this_#{type}").next(q) }
|
19
|
+
define_method("#{types}_ahead") { |q| send("this_#{type}").ahead(q) }
|
20
|
+
alias_method types.to_sym, "#{types}_forward".to_sym
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :yesterday, :last_day
|
24
|
+
alias_method :today, :this_day
|
25
|
+
alias_method :tomorrow, :next_day
|
26
|
+
|
27
|
+
#
|
28
|
+
# i hate this
|
29
|
+
#
|
30
|
+
|
31
|
+
### Days
|
32
|
+
|
33
|
+
# @!method this_day
|
34
|
+
# Get the current day within the active calendar.
|
35
|
+
# @return [Calendar::Day]
|
36
|
+
|
37
|
+
# @!method last_day
|
38
|
+
# Get the previous day within the active calendar.
|
39
|
+
# @return [Calendar::Day]
|
40
|
+
|
41
|
+
# @!method next_day
|
42
|
+
# Get the next day within the active calendar.
|
43
|
+
# @return [Calendar::Day]
|
44
|
+
|
45
|
+
# @!method days_for
|
46
|
+
# Get a list of days within the specified period unit.
|
47
|
+
# @param period [Calendar::Support::Unit] the containing period unit
|
48
|
+
# @return [Array<Calendar::Day>]
|
49
|
+
|
50
|
+
# @!method days_back
|
51
|
+
# Get a quantity of days back from today.
|
52
|
+
# @param quantity [Integer] the number of days requested
|
53
|
+
# @return [Array<Calendar::Day>]
|
54
|
+
|
55
|
+
# @!method days_ago
|
56
|
+
# Get the day that occurred the specified number of days ago.
|
57
|
+
# @param quantity [Integer] the number of days before today
|
58
|
+
# @return [Calendar::Day]
|
59
|
+
|
60
|
+
# @!method days_forward
|
61
|
+
# Get a quantity of days forward from today.
|
62
|
+
# @param quantity [Integer] the number of days requested
|
63
|
+
# @return [Array<Calendar::Day>]
|
64
|
+
|
65
|
+
# @!method days_ahead
|
66
|
+
# Get the day that occurred the specified number of days ahead.
|
67
|
+
# @param quantity [Integer] the number of days after today
|
68
|
+
# @return [Calendar::Day]
|
69
|
+
|
70
|
+
### Weeks
|
71
|
+
|
72
|
+
# @!method this_week
|
73
|
+
# Get the current week within the active calendar.
|
74
|
+
# @return [Calendar::Week]
|
75
|
+
|
76
|
+
# @!method last_week
|
77
|
+
# Get the previous week within the active calendar.
|
78
|
+
# @return [Calendar::Week]
|
79
|
+
|
80
|
+
# @!method next_week
|
81
|
+
# Get the next week within the active calendar.
|
82
|
+
# @return [Calendar::Week]
|
83
|
+
|
84
|
+
# @!method weeks_for
|
85
|
+
# Get a list of weeks within the specified period unit.
|
86
|
+
# @param period [Calendar::Support::Unit] the containing period unit
|
87
|
+
# @return [Array<Calendar::Week>]
|
88
|
+
|
89
|
+
# @!method weeks_back
|
90
|
+
# Get a quantity of weeks back from this week.
|
91
|
+
# @param quantity [Integer] the number of weeks requested
|
92
|
+
# @return [Array<Calendar::Week>]
|
93
|
+
|
94
|
+
# @!method weeks_ago
|
95
|
+
# Get the week that occurred the specified number of weeks ago.
|
96
|
+
# @param quantity [Integer] the number of weeks before this week
|
97
|
+
# @return [Calendar::Week]
|
98
|
+
|
99
|
+
# @!method weeks_forward
|
100
|
+
# Get a quantity of weeks forward from this week.
|
101
|
+
# @param quantity [Integer] the number of weeks requested
|
102
|
+
# @return [Array<Calendar::Week>]
|
103
|
+
|
104
|
+
# @!method weeks_ahead
|
105
|
+
# Get the week that occurred the specified number of weeks ahead.
|
106
|
+
# @param quantity [Integer] the number of weeks after this week
|
107
|
+
# @return [Calendar::Week]
|
108
|
+
|
109
|
+
### Months
|
110
|
+
|
111
|
+
# @!method this_month
|
112
|
+
# Get the current month within the active calendar.
|
113
|
+
# @return [Calendar::Month]
|
114
|
+
|
115
|
+
# @!method last_month
|
116
|
+
# Get the previous month within the active calendar.
|
117
|
+
# @return [Calendar::Month]
|
118
|
+
|
119
|
+
# @!method next_month
|
120
|
+
# Get the next month within the active calendar.
|
121
|
+
# @return [Calendar::Month]
|
122
|
+
|
123
|
+
# @!method months_for
|
124
|
+
# Get a list of months within the specified period unit.
|
125
|
+
# @param period [Calendar::Support::Unit] the containing period unit
|
126
|
+
# @return [Array<Calendar::Month>]
|
127
|
+
|
128
|
+
# @!method months_back
|
129
|
+
# Get a quantity of months back from this month.
|
130
|
+
# @param quantity [Integer] the number of months requested
|
131
|
+
# @return [Array<Calendar::Month>]
|
132
|
+
|
133
|
+
# @!method months_ago
|
134
|
+
# Get the month that occurred the specified number of months ago.
|
135
|
+
# @param quantity [Integer] the number of months before this month
|
136
|
+
# @return [Calendar::Month]
|
137
|
+
|
138
|
+
# @!method months_forward
|
139
|
+
# Get a quantity of months forward from this month.
|
140
|
+
# @param quantity [Integer] the number of months requested
|
141
|
+
# @return [Array<Calendar::Month>]
|
142
|
+
|
143
|
+
# @!method months_ahead
|
144
|
+
# Get the month that occurred the specified number of months ahead.
|
145
|
+
# @param quantity [Integer] the number of months after this month
|
146
|
+
# @return [Calendar::Month]
|
147
|
+
|
148
|
+
### Quarters
|
149
|
+
|
150
|
+
# @!method this_quarter
|
151
|
+
# Get the current quarter within the active calendar.
|
152
|
+
# @return [Calendar::Quarter]
|
153
|
+
|
154
|
+
# @!method last_quarter
|
155
|
+
# Get the previous quarter within the active calendar.
|
156
|
+
# @return [Calendar::Quarter]
|
157
|
+
|
158
|
+
# @!method next_quarter
|
159
|
+
# Get the next quarter within the active calendar.
|
160
|
+
# @return [Calendar::Quarter]
|
161
|
+
|
162
|
+
# @!method quarters_for
|
163
|
+
# Get a list of quarters within the specified period unit.
|
164
|
+
# @param period [Calendar::Support::Unit] the containing period unit
|
165
|
+
# @return [Array<Calendar::Quarter>]
|
166
|
+
|
167
|
+
# @!method quarters_back
|
168
|
+
# Get a quantity of quarters back from this quarter.
|
169
|
+
# @param quantity [Integer] the number of quarters requested
|
170
|
+
# @return [Array<Calendar::Quarter>]
|
171
|
+
|
172
|
+
# @!method quarters_ago
|
173
|
+
# Get the quarter that occurred the specified number of quarters ago.
|
174
|
+
# @param quantity [Integer] the number of quarters before this quarter
|
175
|
+
# @return [Calendar::Quarter]
|
176
|
+
|
177
|
+
# @!method quarters_forward
|
178
|
+
# Get a quantity of quarters forward from this quarter.
|
179
|
+
# @param quantity [Integer] the number of quarters requested
|
180
|
+
# @return [Array<Calendar::Quarter>]
|
181
|
+
|
182
|
+
# @!method quarters_ahead
|
183
|
+
# Get the quarter that occurred the specified number of days ahead.
|
184
|
+
# @param quantity [Integer] the number of quarters after this quarter
|
185
|
+
# @return [Calendar::Quarter]
|
186
|
+
|
187
|
+
### Halves
|
188
|
+
|
189
|
+
# @!method this_half
|
190
|
+
# Get the current half within the active calendar.
|
191
|
+
# @return [Calendar::Half]
|
192
|
+
|
193
|
+
# @!method last_half
|
194
|
+
# Get the previous half within the active calendar.
|
195
|
+
# @return [Calendar::Half]
|
196
|
+
|
197
|
+
# @!method next_half
|
198
|
+
# Get the next half within the active calendar.
|
199
|
+
# @return [Calendar::Half]
|
200
|
+
|
201
|
+
# @!method halves_for
|
202
|
+
# Get a list of halves within the specified period unit.
|
203
|
+
# @param period [Calendar::Support::Unit] the containing period unit
|
204
|
+
# @return [Array<Calendar::Half>]
|
205
|
+
|
206
|
+
# @!method halves_back
|
207
|
+
# Get a quantity of halves back from this half.
|
208
|
+
# @param quantity [Integer] the number of halves requested
|
209
|
+
# @return [Array<Calendar::Half>]
|
210
|
+
|
211
|
+
# @!method halves_ago
|
212
|
+
# Get the half that occurred the specified number of halves ago.
|
213
|
+
# @param quantity [Integer] the number of halves before this half
|
214
|
+
# @return [Calendar::Half]
|
215
|
+
|
216
|
+
# @!method halves_forward
|
217
|
+
# Get a quantity of halves forward from this half.
|
218
|
+
# @param quantity [Integer] the number of halves requested
|
219
|
+
# @return [Array<Calendar::Half>]
|
220
|
+
|
221
|
+
# @!method halves_ahead
|
222
|
+
# Get the half that occurred the specified number of halves ahead.
|
223
|
+
# @param quantity [Integer] the number of halves after this half
|
224
|
+
# @return [Calendar::Half]
|
225
|
+
|
226
|
+
### Years
|
227
|
+
|
228
|
+
# @!method this_year
|
229
|
+
# Get the current year within the active calendar.
|
230
|
+
# @return [Calendar::Year]
|
231
|
+
|
232
|
+
# @!method last_year
|
233
|
+
# Get the previous year within the active calendar.
|
234
|
+
# @return [Calendar::Year]
|
235
|
+
|
236
|
+
# @!method next_year
|
237
|
+
# Get the next year within the active calendar.
|
238
|
+
# @return [Calendar::Year]
|
239
|
+
|
240
|
+
# @!method years_for
|
241
|
+
# Get a list of years within the specified period unit.
|
242
|
+
# @param period [Calendar::Support::Unit] the containing period unit
|
243
|
+
# @return [Array<Calendar::Year>]
|
244
|
+
|
245
|
+
# @!method years_back
|
246
|
+
# Get a quantity of years back from this year.
|
247
|
+
# @param quantity [Integer] the number of years requested
|
248
|
+
# @return [Array<Calendar::Year>]
|
249
|
+
|
250
|
+
# @!method years_ago
|
251
|
+
# Get the year that occurred the specified number of years ago.
|
252
|
+
# @param quantity [Integer] the number of years before this year
|
253
|
+
# @return [Calendar::Year]
|
254
|
+
|
255
|
+
# @!method years_forward
|
256
|
+
# Get a quantity of years forward from this year.
|
257
|
+
# @param quantity [Integer] the number of years requested
|
258
|
+
# @return [Array<Calendar::Year>]
|
259
|
+
|
260
|
+
# @!method years_ahead
|
261
|
+
# Get the year that occurred the specified number of years ahead.
|
262
|
+
# @param quantity [Integer] the number of years after this year
|
263
|
+
# @return [Calendar::Year]
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative './support/unit'
|
3
|
+
|
4
|
+
module TimeBoss
|
5
|
+
class Calendar
|
6
|
+
class Week < Support::Unit
|
7
|
+
def initialize(calendar, start_date, end_date)
|
8
|
+
raise UnsupportedUnitError unless calendar.supports_weeks?
|
9
|
+
super(calendar, start_date, end_date)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Get a simple representation of this week.
|
13
|
+
# @return [String] (e.g. "2020W32")
|
14
|
+
def name
|
15
|
+
"#{year_index}W#{index}"
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get a "pretty" representation of this week.
|
19
|
+
# @return [String] (e.g. "Week of August 3, 2020")
|
20
|
+
def title
|
21
|
+
"Week of #{start_date.strftime('%B %-d, %Y')}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Get a stringified representation of this week.
|
25
|
+
# @return [String] (e.g. "2020W32: 2020-08-03 thru 2020-08-09")
|
26
|
+
def to_s
|
27
|
+
"#{name}: #{start_date} thru #{end_date}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get the index of this week within its containing year.
|
31
|
+
# @return [Integer]
|
32
|
+
def index
|
33
|
+
@_index ||= (((start_date - year.start_date) + 1) / 7.0).to_i + 1
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get the year number for this week.
|
37
|
+
# @return [Integer] (e.g. 2020)
|
38
|
+
def year_index
|
39
|
+
@_year_index ||= year.year_index
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def down
|
45
|
+
self.class.new(calendar, start_date - 1.week, end_date - 1.week)
|
46
|
+
end
|
47
|
+
|
48
|
+
def up
|
49
|
+
self.class.new(calendar, start_date + 1.week, end_date + 1.week)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative './support/monthly_unit'
|
3
|
+
|
4
|
+
module TimeBoss
|
5
|
+
class Calendar
|
6
|
+
class Year < Support::MonthlyUnit
|
7
|
+
NUM_MONTHS = 12
|
8
|
+
|
9
|
+
# Get a simple representation of this year.
|
10
|
+
# @return [String] (e.g. "2020")
|
11
|
+
def name
|
12
|
+
year_index.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :title, :name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|