Skip to content

Commit

Permalink
changes to some of last_ methods for consistency with other existing …
Browse files Browse the repository at this point in the history
…ones
  • Loading branch information
robertino committed Jan 16, 2018
1 parent e02100f commit 4dfc12a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.5.0
Changes to Relative
Change behaviour of last_n_days to return non-inclusive period of n days before provided
last_n_days :exclusive parameter is gone, :inclusive parameter replaces it. Returned result is not inclusive by default.
Introduced last_day for consistency (same as day_before)

0.4.3
Changed behaviour of last_week to be more consistent with other last_ methods particularly on Sunday

Expand Down
10 changes: 7 additions & 3 deletions lib/datebox/relative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class << self
def last(period, options = {})
raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
case period.to_sym
when :day then day_before
when :day then last_day
when :n_days then last_n_days(options)
when :week then last_week(options)
when :month then last_month
Expand All @@ -41,10 +41,14 @@ def same_day
new Proc.new {|relative_to| Period.new(relative_to, relative_to) }, __method__.to_s
end

def day_before
def last_day
new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }, __method__.to_s
end

# for backwards compatibility, we had only 'day_before' but last_day is more consistent with other calls
# @deprecated
def day_before; last_day; end

def day_apart(difference)
new Proc.new {|relative_to| Period.new(relative_to + difference, relative_to + difference) }, __method__.to_s
end
Expand All @@ -66,7 +70,7 @@ def method_missing(m, *args, &block)

def last_n_days(options = {})
days = (options[:days] || options['days']).to_i
inclusive = (options[:exclusive] || options['exclusive']) ? false : true # inclusive by default
inclusive = (options[:inclusive] || options['inclusive']) ? true : false # NOT inclusive by default
days = 1 if days.nil? || days <= 0 # days should always > 0 since it only return last x days
proc = inclusive ?
Proc.new {|relative_to| Period.new(relative_to - days + 1, relative_to) } :
Expand Down
2 changes: 1 addition & 1 deletion lib/datebox/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Datebox
VERSION = "0.4.3"
VERSION = "0.5.0"
end
19 changes: 12 additions & 7 deletions test/unit/test_relative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ def test_calculates_correctly
# day
assert_equal [Date.parse('2013-07-07')], Datebox::Relative.same_day.to('2013-07-07').dates
assert_equal [Date.parse('2013-07-06')], Datebox::Relative.day_before.to('2013-07-07').dates
assert_equal [Date.parse('2013-07-06')], Datebox::Relative.last_day.to('2013-07-07').dates
assert_equal [Date.parse('2013-07-02')], Datebox::Relative.day_apart(1).to('2013-07-01').dates
assert_equal [Date.parse('2013-08-03')], Datebox::Relative.day_apart(-2).to('2013-08-05').dates
assert_equal [Date.parse('2013-08-03')], Datebox::Relative.day_apart(-2).to('2013-08-05').dates
assert_equal [Date.parse('2013-08-03')], Datebox::Relative.day_ago_2.to('2013-08-05').dates
assert_equal [Date.parse('2013-08-08')], Datebox::Relative.day_in_3.to('2013-08-05').dates
assert_equal [Date.parse('2013-08-20')], Datebox::Relative.day_in_19.to('2013-08-01').dates

assert_equal Datebox::Period.new('2017-12-24', '2017-12-30'), Datebox::Relative.last_n_days({'days' => 7}).to('2017-12-31')
assert_equal Datebox::Period.new('2017-12-25', '2017-12-31'), Datebox::Relative.last_n_days({'days' => 7, 'inclusive' => true}).to('2017-12-31')

# n days
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {:days => 30}).to('2014-06-30')
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {days: 30, exclusive: true}).to('2014-07-01')
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {days: 30, inclusive: true}).to('2014-06-30')
assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {days: 30}).to('2014-07-01')

# week
assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.last_week.to('2013-07-09') # tue
Expand All @@ -43,22 +47,23 @@ def test_calculates_correctly
assert_equal Datebox::Period.new('2014-02-03', '2014-02-09'), Datebox::Relative.last(:week).to('2014-02-10')
assert_equal Datebox::Period.new('2014-02-02', '2014-02-08'), Datebox::Relative.last(:week, {:last_weekday => "Saturday"}).to('2014-02-10')

# andything, up to date
# anything, up to date
assert_equal Datebox::Period.new('2015-03-15', '2015-03-21'), Datebox::Relative.to_date(:week, {:last_weekday => "Saturday"}).to('2015-03-21')
assert_equal Datebox::Period.new('2015-03-15', '2015-03-20'), Datebox::Relative.to_date(:week, {:last_weekday => "Saturday"}).to('2015-03-20')
assert_equal Datebox::Period.new('2015-03-16', '2015-03-20'), Datebox::Relative.to_date(:week, {:last_weekday => "Sunday"}).to('2015-03-20')
assert_equal Datebox::Period.new('2015-03-01', '2015-03-20'), Datebox::Relative.to_date(:month).to('2015-03-20')
assert_equal Datebox::Period.new('2015-01-01', '2015-03-20'), Datebox::Relative.to_date(:year).to('2015-03-20')
assert_equal Datebox::Period.new('2015-01-01', '2015-03-20'), Datebox::Relative.year_to_date.to('2015-03-20')

# the one that's different
assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.last_weeks_weekdays_as!("Monday", "Wednesday").to('2013-07-05') #fri

assert_equal Datebox::Period.new('2015-11-02', '2015-11-08'), Datebox::Relative.same_week.to('2015-11-02') # mon
assert_equal Datebox::Period.new('2015-11-02', '2015-11-08'), Datebox::Relative.same_week.to('2015-11-03') # tue
assert_equal Datebox::Period.new('2015-11-02', '2015-11-08'), Datebox::Relative.same_week.to('2015-11-08') # sun

assert_equal Datebox::Period.new('2015-11-01', '2015-11-07'), Datebox::Relative.same_week(last_weekday: 'Saturday').to('2015-11-07') # sat

assert_equal Datebox::Period.new('2015-11-01', '2015-11-30'), Datebox::Relative.same_month.to('2015-11-30')

# the one method that's different (does not return period but array)
assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.last_weeks_weekdays_as!("Monday", "Wednesday").to('2013-07-05') #fri
end

end

0 comments on commit 4dfc12a

Please sign in to comment.