Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce current? to check if the period overlays the current date #12

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.3.3

* Add current? to check if the period containts the current date.

*Edwin Vlieg*

## 0.3.2

* Add this_month?, this_quarter? and this_year? to check if a period is the current month, quarter or year
Expand Down
23 changes: 6 additions & 17 deletions lib/active_date_range/date_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,26 +232,15 @@ def same_year?
end
end

# Return true when the range is equal to the current month
def this_month?
memoize(:@this_month) do
self == DateRange.this_month
def current?
memoize(:@current) do
cover?(Time.zone.today)
end
end

# Return true when the range is equal to the current quarter
def this_quarter?
memoize(:@this_quarter) do
self == DateRange.this_quarter
end
end

# Return true when the range is equal to the current year
def this_year?
memoize(:@this_year) do
self == DateRange.this_year
end
end
alias :this_month? :current?
alias :this_quarter? :current?
alias :this_year? :current?

# Returns true when the date range is before the given date. Accepts both a <tt>Date</tt>
# and <tt>DateRange</tt> as input.
Expand Down
12 changes: 12 additions & 0 deletions test/active_date_range/date_range_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ def test_same_year
assert_not described_class.parse("202001..202112").same_year?
end

def test_current
assert described_class.this_month.current?
assert described_class.this_quarter.current?
assert described_class.this_year.current?
assert_not described_class.prev_month.current?
assert_not described_class.prev_quarter.current?
assert_not described_class.prev_year.current?
assert_not described_class.next_month.current?
assert_not described_class.next_quarter.current?
assert_not described_class.next_year.current?
end

def test_this_month
assert described_class.this_month.this_month?
assert_not described_class.prev_month.this_month?
Expand Down
Loading