moneybird / active-date-range

Powerful DateRanges for Ruby and ActiveSupport
MIT License
51 stars 0 forks source link

Add supports for weeks #2

Closed edwinv closed 3 years ago

edwinv commented 3 years ago

After a question on Reddit about bi-weekly/fortnightly, I've decided to add support for week ranges. This PR contains the required changes to support weeks:

range = DateRange.this_week
range.one_week? # => true
range.full_week? # => true
range.weeks # => 1
range.next # => DateRange.next_week
range.granularity # => :week

Because we use the ActiveSupport API, the beginning of the week is determined by Date.beginning_of_week. This allows settings a different beginning of the week and will automatically change the DateRange behaviour accordingly.

For bi-weekly ranges it's hard to add a specific API because we don't know where the range would start and end. Therefore it's better to create the bi-weekly range manually: biweekly = DateRange.this_week + DateRange.this_week.next.