From 4dfc12ab260a1dfa536108455a4eccc0c0c927a8 Mon Sep 17 00:00:00 2001 From: Robert Borkowski Date: Tue, 16 Jan 2018 15:40:58 +0000 Subject: [PATCH] changes to some of last_ methods for consistency with other existing ones --- CHANGELOG.txt | 6 ++++++ lib/datebox/relative.rb | 10 +++++++--- lib/datebox/version.rb | 2 +- test/unit/test_relative.rb | 19 ++++++++++++------- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 7dcc5e1..2c8c95a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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 diff --git a/lib/datebox/relative.rb b/lib/datebox/relative.rb index 99f0113..d4e2cc9 100644 --- a/lib/datebox/relative.rb +++ b/lib/datebox/relative.rb @@ -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 @@ -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 @@ -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) } : diff --git a/lib/datebox/version.rb b/lib/datebox/version.rb index 2f275de..67b0861 100644 --- a/lib/datebox/version.rb +++ b/lib/datebox/version.rb @@ -1,3 +1,3 @@ module Datebox - VERSION = "0.4.3" + VERSION = "0.5.0" end diff --git a/test/unit/test_relative.rb b/test/unit/test_relative.rb index a99c010..602b07c 100644 --- a/test/unit/test_relative.rb +++ b/test/unit/test_relative.rb @@ -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 @@ -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