timeboss 0.3.1
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 +154 -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 +215 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cfac166bdd92d5f95689183603c5af386100642526cea8cbc405765655a1eb26
|
4
|
+
data.tar.gz: 00dfb8323ed9317cb214f532a7431bb9dc40d0ca3027ecda5f922bf72949ff0d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf61f841ed2aa51150656da6d849b01a44511e478a8a0012e0074ec9a85fee8569a0bd162c22f3105b33d565c863df556bbee8bb65ea8d9b938272ff4b702b76
|
7
|
+
data.tar.gz: 1dff56f1577ab2e91eb4b5fcfee2928979c261ae02812eec1d2ab0c8788bd7078fbba7f95aec56c830f219d3c07f92513402cd3bb9035233283ffabdcb17fb0e
|
@@ -0,0 +1,23 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: kevinstuffandthings
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
**Describe the bug**
|
11
|
+
A clear and concise description of what the bug is.
|
12
|
+
|
13
|
+
**To Reproduce**
|
14
|
+
Steps to reproduce the behavior.
|
15
|
+
|
16
|
+
**Expected behavior**
|
17
|
+
A clear and concise description of what you expected to happen.
|
18
|
+
|
19
|
+
**Logs**
|
20
|
+
If applicable, add code/outputs to help explain your problem.
|
21
|
+
|
22
|
+
**Additional context**
|
23
|
+
Add any other context about the problem here.
|
@@ -0,0 +1,20 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: kevinstuffandthings
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
**Is your feature request related to a problem? Please describe.**
|
11
|
+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
12
|
+
|
13
|
+
**Describe the solution you'd like**
|
14
|
+
A clear and concise description of what you want to happen.
|
15
|
+
|
16
|
+
**Describe alternatives you've considered**
|
17
|
+
A clear and concise description of any alternative solutions or features you've considered.
|
18
|
+
|
19
|
+
**Additional context**
|
20
|
+
Add any other context or screenshots about the feature request here.
|
data/.replit
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.4
|
4
|
+
- 2.5
|
5
|
+
- 2.6
|
6
|
+
script:
|
7
|
+
- bundle exec rspec
|
8
|
+
deploy:
|
9
|
+
provider: rubygems
|
10
|
+
api_key:
|
11
|
+
secure: jUWa2Jx0HRVv7Dl0zYPTskHQ+RJGy2TUcBWOjyFbBUbeDKiRh8J/NnbMuCOvmL94htSaFk5lcs6k74Itu6jW12+KyT98OAnOardFPFuI0cHHhIMEhF6ms8EhyOCZ0vvR9zav11gfNzyF716qO09DQSDuoKWWNdhrYj8a5paLdbHuRqQiw3YUmnjVQ/v9SPYaZ7ziXC4X4tBxQhK2ujNsNfztSVrpYb6pnsXnt+hFQOnskzi0+er4854iymykeyNVgB71KgivrE3T55ZTXPPglshsZQE7M7FlYA5L3bwzR7SlA202yFYHB3hUN3obwO3BjO4OiAdKQnSosBHaXI13ZvCOz02AfEJhwoxkBXw1/2CvSQ12QQPVgqkpxWY03h+URk+gGHCV+JaqZPCV9excmVWg/IOQou0IJ6h84VFViyVc2aFVN2AIdoaG3NsppbV3j4VuLG6J2VVpNmUzN3pGgi5YSjVq9Hp+huXZFfWpnUgAxc680hKgcFajE3JNANG+NbNEeoDmITvZyV5kvJklgUKPsl2wNk+wY/GGrSBgNOVlyXqTWLZb/8Z/cSrN5DEc6D5BIS/SG95wlCRT8jUqbyEPjT+H77TH2ySfwWZzblLqbIxaLiZr3hX16oMdUx7Cya5LKLFgQBpU6cFUxvZX7SqHwjk1vnuG592Kxd9GkX4=
|
12
|
+
gem: timeboss
|
13
|
+
on:
|
14
|
+
tags: true
|
15
|
+
repo: kevinstuffandthings/timeboss
|
16
|
+
skip_cleanup: 'true'
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--protected lib/**/*.rb
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
9
|
+
level of experience, education, socio-economic status, nationality, personal
|
10
|
+
appearance, race, religion, or sexual identity and orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at kevinstuffandthings@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
72
|
+
|
73
|
+
[homepage]: https://www.contributor-covenant.org
|
74
|
+
|
75
|
+
For answers to common questions about this code of conduct, see
|
76
|
+
https://www.contributor-covenant.org/faq
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kevin McDonald
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
# TimeBoss [![Build Status](https://travis-ci.com/kevinstuffandthings/timeboss.svg?branch=master)](https://travis-ci.com/kevinstuffandthings/timeboss) [![Gem Version](https://badge.fury.io/rb/timeboss.svg)](https://badge.fury.io/rb/timeboss) [![Run on Repl.it](https://repl.it/badge/github/kevinstuffandthings/timeboss)](https://repl.it/github/kevinstuffandthings/timeboss)
|
2
|
+
|
3
|
+
A gem providing convenient navigation of the [Broadcast Calendar](https://en.wikipedia.org/wiki/Broadcast_calendar), the standard Gregorian calendar, and is easily extensible to support multiple financial calendars.
|
4
|
+
|
5
|
+
Originally developed for [Simulmedia](https://simulmedia.com).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
# update with the version of your choice
|
12
|
+
gem 'timeboss'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
$ gem install timeboss
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
Supports `year`, `half`, `quarter`, `month`, `week` (non-gregorian calendars only), and `day`.
|
29
|
+
|
30
|
+
Prepare your calendar for use:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'timeboss/calendars/broadcast'
|
34
|
+
|
35
|
+
calendar = TimeBoss::Calendars::Broadcast.new
|
36
|
+
# => #<TimeBoss::Calendars::Broadcast:0x007f82d50f0af0 @basis=TimeBoss::Calendars::Broadcast::Basis>
|
37
|
+
```
|
38
|
+
|
39
|
+
You can ask simple questions of the calendar:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
period = calendar.parse('2019Q4') # or '2018', or '2019-12-21', or '2020W32', or '2020M3W2'
|
43
|
+
# => #<TimeBoss::Calendar::Quarter:0x007f82d50e2478>
|
44
|
+
period.to_s
|
45
|
+
# => "2019Q4: 2019-09-30 thru 2019-12-29"
|
46
|
+
period.next.start_date.to_s # try previous, too!
|
47
|
+
# => "2019-12-30"
|
48
|
+
(period + 3).start_date.to_s # try subtraction, too!
|
49
|
+
# => "2020-06-29"
|
50
|
+
period.offset(3).start_date.to_s # works with negatives, too!
|
51
|
+
# => "2020-06-29"
|
52
|
+
period.current? # does today fall within this period?
|
53
|
+
# => false
|
54
|
+
|
55
|
+
calendar.year_for(Date.parse('2018-12-31')).to_s
|
56
|
+
# => "2019: 2018-12-31 thru 2019-12-29"
|
57
|
+
|
58
|
+
calendar.this_month.to_s # your results may vary
|
59
|
+
# => "2019M12: 2019-11-25 thru 2019-12-29"
|
60
|
+
|
61
|
+
calendar.years_back(2).map { |y| y.start_date.to_s } # run in 2020
|
62
|
+
# => ["2018-12-31", "2019-12-30"]
|
63
|
+
|
64
|
+
calendar.months_ago(3).name # run in 2020M7
|
65
|
+
# => "2020M4"
|
66
|
+
|
67
|
+
calendar.weeks_ahead(3).name # run in 2020W29
|
68
|
+
# => "2020W32"
|
69
|
+
```
|
70
|
+
|
71
|
+
The resulting periods can be formatted a variety of (parsable) ways:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
entry = calendar.parse('2020M24')
|
75
|
+
entry.format
|
76
|
+
# => "2020H1Q2M3W2"
|
77
|
+
|
78
|
+
entry.format(:quarter)
|
79
|
+
# => "2020Q2W11"
|
80
|
+
|
81
|
+
entry.format(:quarter, :month)
|
82
|
+
# => "2020Q2M3W2"
|
83
|
+
```
|
84
|
+
|
85
|
+
_Note: all parsable descriptors should be ordered by chronological specificity (from least to most)_
|
86
|
+
|
87
|
+
Each type of period can give you information about its constituent periods:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
calendar.this_month.weeks.map(&:to_s)
|
91
|
+
# => ["2020M1W1: 2019-12-30 thru 2020-01-05", "2020M1W2: 2020-01-06 thru 2020-01-12", "2020M1W3: 2020-01-13 thru 2020-01-19", "2020M1W4: 2020-01-20 thru 2020-01-26"]
|
92
|
+
|
93
|
+
calendar.this_year.weeks.last.to_s
|
94
|
+
# => "2020W52: 2020-12-21 thru 2020-12-27"
|
95
|
+
|
96
|
+
calendar.last_month.quarter.title # today is 2020-07-15
|
97
|
+
# => "Q2 2020"
|
98
|
+
|
99
|
+
calendar.parse('2020Q1').months.map(&:name)
|
100
|
+
# => ["2020M1", "2020M2", "2020M3"]
|
101
|
+
```
|
102
|
+
|
103
|
+
Period shifting is easy. Note that the shifts are relative to today, not the base date. A shift examines the base period to find its offset into the shifting period size, and project it relative to now.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
calendar.parse('Q3').years_ago(5).title
|
107
|
+
# => "Q3 2015"
|
108
|
+
|
109
|
+
week = calendar.this_week # run 2020W29
|
110
|
+
"#{week.name}: #{week.in_quarter} of #{week.quarter.name}; #{week.in_year} of #{week.year.name}"
|
111
|
+
# => "2020W29: 3 of 2020Q3; 29 of 2020"
|
112
|
+
|
113
|
+
# run 2020W29, this generates the same as above, because shifts are relative to date run!
|
114
|
+
week = calendar.parse('2014W29').this_week
|
115
|
+
"#{week.name}: #{week.in_quarter} of #{week.quarter.name}; #{week.in_year} of #{week.year.name}"
|
116
|
+
# => "2020W29: 3 of 2020Q3; 29 of 2020"
|
117
|
+
|
118
|
+
calendar.this_week.next_year.to_s # run 2020W29
|
119
|
+
# => "2021W29: 2021-07-12 thru 2021-07-18"
|
120
|
+
```
|
121
|
+
|
122
|
+
Complicated range expressions can be parsed using the `..` range operator, or evaluated with `thru`:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
calendar.parse('2020M1 .. 2020M2').weeks.map(&:title)
|
126
|
+
# => ["Week of December 30, 2019", "Week of January 6, 2020", "Week of January 13, 2020", "Week of January 20, 2020", "Week of January 27, 2020", "Week of February 3, 2020", "Week of February 10, 2020", "Week of February 17, 2020"]
|
127
|
+
|
128
|
+
calendar.this_quarter.thru(calendar.this_quarter+2).months.map(&:name) # run in 2020Q3
|
129
|
+
# => ["2020M7", "2020M8", "2020M9", "2020M10", "2020M11", "2020M12", "2021M1", "2021M2", "2021M3"]
|
130
|
+
|
131
|
+
period = calendar.parse('2020W3..2020Q1')
|
132
|
+
"#{period.name}: from #{period.start_date} thru #{period.end_date} (current=#{period.current?})"
|
133
|
+
# => "2020W3 .. 2020Q1: from 2020-01-13 thru 2020-03-29 (current=false)"
|
134
|
+
```
|
135
|
+
|
136
|
+
The examples above are just samples. Try different periods, operators, etc. All of the non-week-based operations will work similarly on the `TimeBoss::Calendars::Gregorian` calendar.
|
137
|
+
|
138
|
+
### REPL
|
139
|
+
To open a REPL for the broadcast calendar, use the `tbsh` executable, or the `timeboss:calendars:broadcast:repl` rake task.
|
140
|
+
|
141
|
+
For the Gregorian calendar (or any other implemented calendars), supply the name on the command line.
|
142
|
+
- `tbsh gregorian`
|
143
|
+
- `rake timeboss:calendars:gregorian:repl`
|
144
|
+
|
145
|
+
You will find yourself in the context of an instantiated `TimeBoss::Calendar` object:
|
146
|
+
|
147
|
+
```bash
|
148
|
+
$ tbsh
|
149
|
+
2.4.1 :001 > next_quarter
|
150
|
+
=> #<TimeBoss::Calendar::Quarter:0x007fe04c16a1c8 @calendar=#<TimeBoss::Calendars::Broadcast:0x007fe04c1a0458 @basis=TimeBoss::Calendars::Broadcast::Basis>, @year_index=2020, @index=4, @start_date=#<Date: 2020-09-28 ((2459121j,0s,0n),+0s,2299161j)>, @end_date=#<Date: 2020-12-27 ((2459211j,0s,0n),+0s,2299161j)>>
|
151
|
+
2.4.1 :002 > last_year
|
152
|
+
=> #<TimeBoss::Calendar::Year:0x007fe04c161ca8 @calendar=#<TimeBoss::Calendars::Broadcast:0x007fe04c1a0458 @basis=TimeBoss::Calendars::Broadcast::Basis>, @year_index=2019, @index=1, @start_date=#<Date: 2018-12-31 ((2458484j,0s,0n),+0s,2299161j)>, @end_date=#<Date: 2019-12-29 ((2458847j,0s,0n),+0s,2299161j)>>
|
153
|
+
2.4.1 :003 > parse('this_quarter .. this_quarter+4').months.map(&:name)
|
154
|
+
=> ["2020M7", "2020M8", "2020M9", "2020M10", "2020M11", "2020M12", "2021M1", "2021M2", "2021M3", "2021M4", "2021M5", "2021M6", "2021M7", "2021M8", "2021M9"]
|
155
|
+
```
|
156
|
+
|
157
|
+
_Having trouble with the REPL? If you are using the examples from the [Usage](#Usage) section, keep in mind that the REPL is already in the context of the calendar -- so you don't need to specify the receiver!_
|
158
|
+
|
159
|
+
## Creating new calendars
|
160
|
+
To create a custom calendar, simply extend the `TimeBoss::Calendar` class, and implement a new `TimeBoss::Calendar::Support::MonthBasis` for it.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
require 'timeboss/calendar'
|
164
|
+
|
165
|
+
module MyCalendars
|
166
|
+
class AugustFiscal < TimeBoss::Calendar
|
167
|
+
def initialize
|
168
|
+
# For each calendar, operation, the class will be instantiated with an ordinal value
|
169
|
+
# for `year` and `month`. It is the instance's job to translate those ordinals into
|
170
|
+
# `start_date` and `end_date` values, based on the desired behavior of the calendar.
|
171
|
+
# With month rules defined, TimeBoss will be able to navigate all the relative periods
|
172
|
+
# within the calendar.
|
173
|
+
super(basis: Basis)
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
class Basis < TimeBoss::Calendar::Support::MonthBasis
|
179
|
+
# In this example, August is the first month of the fiscal year. So an incoming 2020/1
|
180
|
+
# value would translate to a gregorian 2019/8.
|
181
|
+
START_MONTH = 8
|
182
|
+
|
183
|
+
def start_date
|
184
|
+
@_start_date ||= begin
|
185
|
+
date = Date.civil(year_index, month_index, 1)
|
186
|
+
date - (date.wday + 7) % 7 # In this calendar, months start Sunday.
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def end_date
|
191
|
+
@_end_date ||= begin
|
192
|
+
date = Date.civil(year_index, month_index, -1)
|
193
|
+
date - (date.wday + 1)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
private
|
198
|
+
|
199
|
+
def month_index
|
200
|
+
((month + START_MONTH - 2) % 12) + 1
|
201
|
+
end
|
202
|
+
|
203
|
+
def year_index
|
204
|
+
month >= START_MONTH ? year : year - 1
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
With the new calendar implemented, it can be accessed in one of 2 ways:
|
212
|
+
|
213
|
+
- via traditional instantiation:
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
calendar = MyCalendars::AugustFiscal.new
|
217
|
+
calendar.this_year
|
218
|
+
```
|
219
|
+
|
220
|
+
- via `TimeBoss::Calendars`:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
require 'timeboss/calendars'
|
224
|
+
TimeBoss::Calendars.register(:august_fiscal, MyCalendars::AugustFiscal)
|
225
|
+
|
226
|
+
# You'll get a cached instance of your calendar here.
|
227
|
+
# Handy when switching back and forth between different calendar implementations.
|
228
|
+
calendar = TimeBoss::Calendars[:august_fiscal]
|
229
|
+
calendar.this_year
|
230
|
+
```
|
231
|
+
|
232
|
+
## TODO
|
233
|
+
- [ ] Add comprehensive documentation
|
data/Rakefile
ADDED
data/bin/tbsh
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'timeboss/calendars'
|
4
|
+
require 'timeboss/support/shellable'
|
5
|
+
|
6
|
+
calendar = if ARGV.length == 1
|
7
|
+
TimeBoss::Calendars[ARGV.first]
|
8
|
+
else
|
9
|
+
TimeBoss::Calendars.first.calendar
|
10
|
+
end
|
11
|
+
|
12
|
+
abort "Unknown calendar" if calendar.nil?
|
13
|
+
|
14
|
+
puts "Active calendar: #{calendar.title}"
|
15
|
+
TimeBoss::Support::Shellable.open(calendar)
|