timeboss 0.0.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +23 -0
  3. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  4. data/.gitignore +5 -3
  5. data/.yardopts +1 -0
  6. data/CODE_OF_CONDUCT.md +76 -0
  7. data/README.md +29 -11
  8. data/lib/timeboss.rb +4 -0
  9. data/lib/timeboss/calendar.rb +17 -0
  10. data/lib/timeboss/calendar/day.rb +18 -6
  11. data/lib/timeboss/calendar/half.rb +6 -2
  12. data/lib/timeboss/calendar/month.rb +6 -2
  13. data/lib/timeboss/calendar/period.rb +25 -11
  14. data/lib/timeboss/calendar/quarter.rb +6 -2
  15. data/lib/timeboss/calendar/support/formatter.rb +4 -2
  16. data/lib/timeboss/calendar/support/month_basis.rb +1 -0
  17. data/lib/timeboss/calendar/support/{month_based.rb → monthly_unit.rb} +22 -18
  18. data/lib/timeboss/calendar/support/navigable.rb +72 -0
  19. data/lib/timeboss/calendar/support/shiftable.rb +218 -28
  20. data/lib/timeboss/calendar/support/translatable.rb +92 -0
  21. data/lib/timeboss/calendar/support/unit.rb +29 -0
  22. data/lib/timeboss/calendar/waypoints.rb +5 -80
  23. data/lib/timeboss/calendar/waypoints/absolute.rb +113 -0
  24. data/lib/timeboss/calendar/waypoints/relative.rb +267 -0
  25. data/lib/timeboss/calendar/week.rb +26 -18
  26. data/lib/timeboss/calendar/year.rb +5 -5
  27. data/lib/timeboss/calendars.rb +15 -0
  28. data/lib/timeboss/calendars/broadcast.rb +4 -0
  29. data/lib/timeboss/calendars/gregorian.rb +24 -0
  30. data/lib/timeboss/version.rb +1 -1
  31. data/spec/calendar/day_spec.rb +1 -1
  32. data/spec/calendar/support/{month_based_spec.rb → monthly_unit_spec.rb} +24 -8
  33. data/spec/calendar/week_spec.rb +11 -14
  34. data/spec/calendars/broadcast_spec.rb +27 -7
  35. data/timeboss.gemspec +1 -0
  36. metadata +28 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '082c0c9219e3f40e3e8f98a9850212b678fa895afd0c9062fe4dd21219e879e7'
4
- data.tar.gz: 95f5b7c19bd48f94b458aed504f258a7942daf98e0e5f30d8eed50fdfb572811
3
+ metadata.gz: c8b4b4481ef5554103855a604563c4db3a4deddb298af24a219315f1a658c7ba
4
+ data.tar.gz: 8be65df968e3a044a81cf492f3be16a03e9d79a61fb80ac6ff4218c8a5b71aa3
5
5
  SHA512:
6
- metadata.gz: a38a6b535c9b65bb8051e1c54003a05e2ce50c4b1e8d6575ef89f9d7f1ce8976f99b713a8a34e2380c98694696f5b8a4765416631f970ed035c85e7d1744a672
7
- data.tar.gz: 3db1f8f1d6551f9c94c14496f2a13c9133d59ef9352cf638d91b3953a6d17981fef6f70b5a045de259f610cbed8d12fcd109b70df3b3dab08aeaae8b86567d69
6
+ metadata.gz: df28f604bbea294960ff8131b65b5b3315bf2cd2c2663866ee3b28e208abc25ef7811535482931620d4886ec8b39249967ed38cfa63cf4a865eba6ea31f11022
7
+ data.tar.gz: 8b8f043b358f21970af342263154ac3bcd40f9322b37c7e7c0587740c703214e77798e8b28ff56d627c9b6bab3f5306bb1e6e21ca52dd7589fa2d6e450a75178
@@ -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/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
- /*.gem
2
- /Gemfile.lock
3
- /.bundle
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ .yardoc
5
+ doc
@@ -0,0 +1 @@
1
+ --protected lib/**/*.rb
@@ -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/README.md CHANGED
@@ -1,19 +1,25 @@
1
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)
2
- A gem providing convenient navigation of the [Broadcast Calendar](https://en.wikipedia.org/wiki/Broadcast_calendar).
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).
3
6
 
4
7
  ## Installation
5
8
  Add this line to your application's Gemfile:
9
+
6
10
  ```ruby
7
11
  # update with the version of your choice
8
12
  gem 'timeboss'
9
13
  ```
10
14
 
11
15
  And then execute:
16
+
12
17
  ```bash
13
18
  $ bundle install
14
19
  ```
15
20
 
16
21
  Or install it yourself as:
22
+
17
23
  ```bash
18
24
  $ gem install timeboss
19
25
  ```
@@ -22,6 +28,7 @@ $ gem install timeboss
22
28
  Supports `year`, `half`, `quarter`, `month`, `week`, and `day`.
23
29
 
24
30
  Prepare your calendar for use:
31
+
25
32
  ```ruby
26
33
  require 'timeboss/calendars/broadcast'
27
34
 
@@ -30,6 +37,7 @@ calendar = TimeBoss::Calendars::Broadcast.new
30
37
  ```
31
38
 
32
39
  You can ask simple questions of the calendar:
40
+
33
41
  ```ruby
34
42
  period = calendar.parse('2019Q4') # or '2018', or '2019-12-21', or '2020W32', or '2020M3W2'
35
43
  # => #<TimeBoss::Calendar::Quarter:0x007f82d50e2478>
@@ -56,11 +64,12 @@ calendar.years_back(2).map { |y| y.start_date.to_s } # run in 2020
56
64
  calendar.months_ago(3).name # run in 2020M7
57
65
  # => "2020M4"
58
66
 
59
- calendar.weeks_hence(3).name # run in 2020W29
67
+ calendar.weeks_ahead(3).name # run in 2020W29
60
68
  # => "2020W32"
61
69
  ```
62
70
 
63
71
  The resulting periods can be formatted a variety of (parsable) ways:
72
+
64
73
  ```ruby
65
74
  entry = calendar.parse('2020M24')
66
75
  entry.format
@@ -72,9 +81,11 @@ entry.format(:quarter)
72
81
  entry.format(:quarter, :month)
73
82
  # => "2020Q2M3W2"
74
83
  ```
84
+
75
85
  _Note: all parsable descriptors should be ordered by chronological specificity (from least to most)_
76
86
 
77
87
  Each type of period can give you information about its constituent periods:
88
+
78
89
  ```ruby
79
90
  calendar.this_month.weeks.map(&:to_s)
80
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"]
@@ -90,6 +101,7 @@ calendar.parse('2020Q1').months.map(&:name)
90
101
  ```
91
102
 
92
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
+
93
105
  ```ruby
94
106
  calendar.parse('Q3').years_ago(5).title
95
107
  # => "Q3 2015"
@@ -108,6 +120,7 @@ calendar.this_week.next_year.to_s # run 2020W29
108
120
  ```
109
121
 
110
122
  Complicated range expressions can be parsed using the `..` range operator, or evaluated with `thru`:
123
+
111
124
  ```ruby
112
125
  calendar.parse('2020M1 .. 2020M2').weeks.map(&:title)
113
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"]
@@ -125,6 +138,7 @@ The examples above are just samples. Try different periods, operators, etc.
125
138
  ### Shell
126
139
  To open an IRB shell for the broadcast calendar, use the `tbsh` executable, or the `timeboss:calendars:broadcast:shell` rake task.
127
140
  You will find yourself in the context of an instantiated `TimeBoss::Calendars::Broadcast` object:
141
+
128
142
  ```bash
129
143
  $ tbsh
130
144
  2.4.1 :001 > next_quarter
@@ -190,17 +204,21 @@ end
190
204
  ```
191
205
 
192
206
  With the new calendar implemented, it can be accessed in one of 2 ways:
207
+
193
208
  - via traditional instantiation:
194
- ```ruby
195
- calendar = MyCalendars::AugustFiscal.new
196
- calendar.this_year
197
- ```
209
+
210
+ ```ruby
211
+ calendar = MyCalendars::AugustFiscal.new
212
+ calendar.this_year
213
+ ```
214
+
198
215
  - via `TimeBoss::Calendars`:
199
- ```ruby
200
- require 'timeboss/calendars'
201
- calendar = TimeBoss::Calendars[:august_fiscal]
202
- calendar.this_year
203
- ```
216
+
217
+ ```ruby
218
+ require 'timeboss/calendars'
219
+ calendar = TimeBoss::Calendars[:august_fiscal]
220
+ calendar.this_year
221
+ ```
204
222
 
205
223
  ## TODO
206
224
  - [ ] Add comprehensive documentation
@@ -1,2 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require "timeboss/version"
3
+
4
+ # TimeBoss
5
+ module TimeBoss
6
+ end
@@ -9,16 +9,33 @@ require_relative './calendar/support/month_basis'
9
9
  module TimeBoss
10
10
  class Calendar
11
11
  include Waypoints
12
+
13
+ # @!method parse
14
+ # Parse an identifier into a unit or period.
15
+ # Valid identifiers can include simple units (like "2020Q3", "2020M8W3", "last_quarter"),
16
+ # mathematical expressions (like "this_month+6"),
17
+ # or period expressions (like "2020W1..2020W8", "this_quarter-2..next_quarter")
18
+ # @param identifier [String]
19
+ # @return [Support::Unit, Period]
20
+
12
21
  delegate :parse, to: :parser
13
22
 
23
+ # Get a name by which this calendar can be referenced.
24
+ # @return [String]
14
25
  def name
15
26
  self.class.to_s.demodulize.underscore
16
27
  end
17
28
 
29
+ # Get a friendly title for this calendar.
30
+ # @return [String]
18
31
  def title
19
32
  name.titleize
20
33
  end
21
34
 
35
+ def supports_weeks?
36
+ false
37
+ end
38
+
22
39
  protected
23
40
 
24
41
  attr_reader :basis
@@ -8,27 +8,39 @@ module TimeBoss
8
8
  super(calendar, start_date, start_date)
9
9
  end
10
10
 
11
+ # Get a simple representation of this day.
12
+ # @return [String] (e.g. "2020-08-03")
11
13
  def name
12
14
  start_date.to_s
13
15
  end
14
16
 
17
+ # Get a "pretty" representation of this day.
18
+ # @return [String] (e.g. "August 3, 2020")
15
19
  def title
16
20
  start_date.strftime('%B %-d, %Y')
17
21
  end
18
22
 
19
- def to_s
20
- name
21
- end
23
+ alias_method :to_s, :name
22
24
 
25
+ # Get the index of this day within its containing year.
26
+ # @return [Integer]
23
27
  def index
24
- @_index ||= (start_date - calendar.year_for(start_date).start_date).to_i + 1
28
+ @_index ||= (start_date - year.start_date).to_i + 1
29
+ end
30
+
31
+ # Get the year number for this day.
32
+ # @return [Integer] (e.g. 2020)
33
+ def year_index
34
+ @_year_index ||= year.year_index
25
35
  end
26
36
 
27
- def previous
37
+ private
38
+
39
+ def down
28
40
  self.class.new(calendar, start_date - 1.day)
29
41
  end
30
42
 
31
- def next
43
+ def up
32
44
  self.class.new(calendar, start_date + 1.day)
33
45
  end
34
46
  end
@@ -1,15 +1,19 @@
1
1
  # frozen_string_literal: true
2
- require_relative './support/month_based'
2
+ require_relative './support/monthly_unit'
3
3
 
4
4
  module TimeBoss
5
5
  class Calendar
6
- class Half < Support::MonthBased
6
+ class Half < Support::MonthlyUnit
7
7
  NUM_MONTHS = 6
8
8
 
9
+ # Get a simple representation of this half.
10
+ # @return [String] (e.g. "2020H2")
9
11
  def name
10
12
  "#{year_index}H#{index}"
11
13
  end
12
14
 
15
+ # Get a "pretty" representation of this half.
16
+ # @return [String] (e.g. "H2 2020")
13
17
  def title
14
18
  "H#{index} #{year_index}"
15
19
  end
@@ -1,15 +1,19 @@
1
1
  # frozen_string_literal: true
2
- require_relative './support/month_based'
2
+ require_relative './support/monthly_unit'
3
3
 
4
4
  module TimeBoss
5
5
  class Calendar
6
- class Month < Support::MonthBased
6
+ class Month < Support::MonthlyUnit
7
7
  NUM_MONTHS = 1
8
8
 
9
+ # Get a simple representation of this month.
10
+ # @return [String] (e.g. "2020M8")
9
11
  def name
10
12
  "#{year_index}M#{index}"
11
13
  end
12
14
 
15
+ # Get a "pretty" representation of this month.
16
+ # @return [String] (e.g. "August 2020")
13
17
  def title
14
18
  "#{Date::MONTHNAMES[index]} #{year_index}"
15
19
  end
@@ -3,9 +3,29 @@ module TimeBoss
3
3
  class Calendar
4
4
  class Period
5
5
  attr_reader :begin, :end
6
+
7
+ # @!method start_date
8
+ # Get the start date of this period.
9
+ # @return [Date]
6
10
  delegate :start_date, to: :begin
11
+
12
+ # @!method end_date
13
+ # Get the end date of this period.
14
+ # @return [Date]
7
15
  delegate :end_date, to: :end
8
16
 
17
+ # @!method name
18
+ # Get a simple representation of this period.
19
+ # @return [String]
20
+
21
+ # @!method title
22
+ # Get a "pretty" representation of this period.
23
+ # @return [String]
24
+
25
+ # @!method to_s
26
+ # Get a stringified representation of this period.
27
+ # @return [String]
28
+
9
29
  %i[name title to_s].each do |message|
10
30
  define_method(message) do
11
31
  text = self.begin.send(message)
@@ -14,7 +34,7 @@ module TimeBoss
14
34
  end
15
35
  end
16
36
 
17
- %w[week month quarter half year].each do |size|
37
+ %w[day week month quarter half year].each do |size|
18
38
  define_method(size.pluralize) do
19
39
  entry = calendar.send("#{size}_for", self.begin.start_date)
20
40
  build_entries entry
@@ -27,20 +47,14 @@ module TimeBoss
27
47
  end
28
48
  end
29
49
 
50
+ # Does this period cover the current date?
51
+ # @return [Boolean]
30
52
  def current?
31
53
  to_range.include?(Date.today)
32
54
  end
33
55
 
34
- def days
35
- to_range.map { |d| Day.new(calendar, d) }
36
- end
37
-
38
- def day
39
- entries = days
40
- return nil unless entries.length == 1
41
- entries.first
42
- end
43
-
56
+ # Express this period as a date range.
57
+ # @return [Range<Date, Date>]
44
58
  def to_range
45
59
  @_to_range ||= start_date .. end_date
46
60
  end