From e3f1085dda4b9561317c4b24055a50b36f0f4c60 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Thu, 19 Sep 2019 20:33:38 +0200 Subject: [PATCH 01/10] Add new class method in_date_period to Faker::Date --- lib/faker/default/date.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index 8b3e16446b..78e8fa6246 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -76,6 +76,13 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 between(from: from, to: to).to_date end + def in_date_period(month: nil, year: ::Date.today.year) + from = ::Date.new(year, month || 1, 1) + to = ::Date.new(year, month || 12, ::Date.civil(year, month || 12, -1).day) + + between(from: from, to: to).to_date + end + private def birthday_date(date, age) From 574d292244237914bb724342664ea49b8032ba70 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Thu, 19 Sep 2019 20:43:41 +0200 Subject: [PATCH 02/10] Add unit test cases for Faker::Date.in_date_period class method --- test/faker/default/test_faker_date.rb | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/faker/default/test_faker_date.rb b/test/faker/default/test_faker_date.rb index a01f29614d..f4eb025fcf 100644 --- a/test/faker/default/test_faker_date.rb +++ b/test/faker/default/test_faker_date.rb @@ -144,4 +144,40 @@ def test_default_birthday assert birthday < birthdate_max, "Expect < \"#{birthdate_max}\", but got #{birthday}" end end + + def test_default_in_date_period + current_year = Date.today.year + 10.times do + date = @tester.in_date_period + assert date.year == current_year + end + end + + def test_in_date_period_with_year + year = 2015 + 10.times do + date = @tester.in_date_period(year: year) + assert date.year == year + end + end + + def test_in_date_period_with_month + month = 2 + current_year = Date.today.year + 10.times do + date = @tester.in_date_period(month: month) + assert date.month == month + assert date.year == current_year + end + end + + def test_in_date_period_date + year = 2008 + month = 3 + 10.times do + date = @tester.in_date_period(year: year, month: month) + assert date.month == month + assert date.year == year + end + end end From 5e82409ffc822d10461d2c2a60f7a55dc11193b7 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Thu, 19 Sep 2019 20:56:14 +0200 Subject: [PATCH 03/10] Add YARD documentation for Faker::Date.in_date_period class method --- lib/faker/default/date.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index 78e8fa6246..e92c243517 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -76,6 +76,27 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 between(from: from, to: to).to_date end + ## + # Produces a date from in range of year and/or month specified. + # + # @param month [Integer] represents the month of the date + # + # @param year [Integer] represents the year of the date + # + # + # @return [Date] + # + # @example + # Faker::Internet.in_date_period #=> # + # + # @example + # Faker::Date.in_date_period(year: 2018, month: 2) #=> # + # + # @example + # Faker::Date.in_date_period(month: 2) #=> # + # + # + # @faker.version 2.3.0 def in_date_period(month: nil, year: ::Date.today.year) from = ::Date.new(year, month || 1, 1) to = ::Date.new(year, month || 12, ::Date.civil(year, month || 12, -1).day) From 09d25ca294a7679853744d0658d03bc1cf5afb6b Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Thu, 19 Sep 2019 21:00:17 +0200 Subject: [PATCH 04/10] Add more documentation for Faker::Date.in_date_period class method --- doc/default/date.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/default/date.md b/doc/default/date.md index 7abb1aac40..40667907d4 100644 --- a/doc/default/date.md +++ b/doc/default/date.md @@ -20,4 +20,16 @@ Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014" # Random birthday date (maximum age between 18 and 65) # Keyword arguments: min_age, max_age Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986" + +# Random date for range of this year +Faker::Internet.in_date_period #=> # + +# Random date for range of year 2018 and month 2 +# Keyword arguments: year, month +Faker::Date.in_date_period(year: 2018, month: 2) #=> # + +# Random date for range of current year and month 2 +# Keyword arguments: month +Faker::Date.in_date_period(month: 2) #=> # + ``` From 49ec6a91218d5ed38aade6e5a4378073ff886aa9 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Fri, 20 Sep 2019 00:51:44 +0200 Subject: [PATCH 05/10] Fix method version to be next instead of 2.3.0 --- lib/faker/default/date.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index e92c243517..40ffa0190a 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -96,7 +96,7 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 # Faker::Date.in_date_period(month: 2) #=> # # # - # @faker.version 2.3.0 + # @faker.version next def in_date_period(month: nil, year: ::Date.today.year) from = ::Date.new(year, month || 1, 1) to = ::Date.new(year, month || 12, ::Date.civil(year, month || 12, -1).day) From 01829ffc91d5f0fbe5e7b77e64afa55716e2fa40 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Fri, 20 Sep 2019 01:32:21 +0200 Subject: [PATCH 06/10] Fix documentation description Co-Authored-By: Connor Shea --- lib/faker/default/date.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index 40ffa0190a..18f7bf2c2f 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -77,7 +77,7 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 end ## - # Produces a date from in range of year and/or month specified. + # Produces a date in the year and/or month specified. # # @param month [Integer] represents the month of the date # From ed554249e4575bd05a5f8ecbade41fc60fe396b0 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Fri, 20 Sep 2019 01:32:43 +0200 Subject: [PATCH 07/10] Fix documentation description Co-Authored-By: Connor Shea --- doc/default/date.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/default/date.md b/doc/default/date.md index 40667907d4..5362c0bc2e 100644 --- a/doc/default/date.md +++ b/doc/default/date.md @@ -21,7 +21,7 @@ Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014" # Keyword arguments: min_age, max_age Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986" -# Random date for range of this year +# Random date in current year Faker::Internet.in_date_period #=> # # Random date for range of year 2018 and month 2 From b3ba6447657687b19e634c46925d04a10a3a2ac8 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Fri, 20 Sep 2019 01:54:14 +0200 Subject: [PATCH 08/10] Update the documentation examples --- doc/default/date.md | 6 +++--- lib/faker/default/date.rb | 10 +++------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/doc/default/date.md b/doc/default/date.md index 5362c0bc2e..ff48daa7e6 100644 --- a/doc/default/date.md +++ b/doc/default/date.md @@ -22,14 +22,14 @@ Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014" Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986" # Random date in current year -Faker::Internet.in_date_period #=> # +Faker::Date.in_date_period #=> Tue, 28 May 2019 # Random date for range of year 2018 and month 2 # Keyword arguments: year, month -Faker::Date.in_date_period(year: 2018, month: 2) #=> # +Faker::Date.in_date_period(year: 2018, month: 2) #=> Sun, 04 Feb 2018 # Random date for range of current year and month 2 # Keyword arguments: month -Faker::Date.in_date_period(month: 2) #=> # +Faker::Date.in_date_period(month: 2) #=> Wed, 06 Feb 2019 ``` diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index 18f7bf2c2f..020417e785 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -80,21 +80,17 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 # Produces a date in the year and/or month specified. # # @param month [Integer] represents the month of the date - # # @param year [Integer] represents the year of the date - # - # # @return [Date] # # @example - # Faker::Internet.in_date_period #=> # + # Faker::Date.in_date_period #=> Tue, 28 May 2019 # # @example - # Faker::Date.in_date_period(year: 2018, month: 2) #=> # + # Faker::Date.in_date_period(year: 2018, month: 2) #=> Sun, 04 Feb 2018 # # @example - # Faker::Date.in_date_period(month: 2) #=> # - # + # Faker::Date.in_date_period(month: 2) #=> Wed, 06 Feb 2019 # # @faker.version next def in_date_period(month: nil, year: ::Date.today.year) From f06d5561fcd2200367cb6313d6f3c5274595fdf7 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sun, 22 Sep 2019 00:37:01 +0200 Subject: [PATCH 09/10] revert commit b3ba644 for updating docs --- doc/default/date.md | 6 +++--- lib/faker/default/date.rb | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/doc/default/date.md b/doc/default/date.md index ff48daa7e6..980e40aa54 100644 --- a/doc/default/date.md +++ b/doc/default/date.md @@ -22,14 +22,14 @@ Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014" Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986" # Random date in current year -Faker::Date.in_date_period #=> Tue, 28 May 2019 +Faker::Date.in_date_period #=> # # Random date for range of year 2018 and month 2 # Keyword arguments: year, month -Faker::Date.in_date_period(year: 2018, month: 2) #=> Sun, 04 Feb 2018 +Faker::Date.in_date_period(year: 2018, month: 2) #=> # # Random date for range of current year and month 2 # Keyword arguments: month -Faker::Date.in_date_period(month: 2) #=> Wed, 06 Feb 2019 +Faker::Date.in_date_period(month: 2) #=> # ``` diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index 020417e785..81ee4ed151 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -80,17 +80,21 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 # Produces a date in the year and/or month specified. # # @param month [Integer] represents the month of the date + # # @param year [Integer] represents the year of the date + # + # # @return [Date] # # @example - # Faker::Date.in_date_period #=> Tue, 28 May 2019 + # Faker::Date.in_date_period #=> # # # @example - # Faker::Date.in_date_period(year: 2018, month: 2) #=> Sun, 04 Feb 2018 + # Faker::Date.in_date_period(year: 2018, month: 2) #=> # # # @example - # Faker::Date.in_date_period(month: 2) #=> Wed, 06 Feb 2019 + # Faker::Date.in_date_period(month: 2) #=> # + # # # @faker.version next def in_date_period(month: nil, year: ::Date.today.year) From 9b97cd3784360a9aa517fa7c63e0cae4b21a5b70 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sun, 22 Sep 2019 13:53:01 +0200 Subject: [PATCH 10/10] Remove extra lines --- lib/faker/default/date.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/faker/default/date.rb b/lib/faker/default/date.rb index 81ee4ed151..1d4e10932e 100644 --- a/lib/faker/default/date.rb +++ b/lib/faker/default/date.rb @@ -80,10 +80,7 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 # Produces a date in the year and/or month specified. # # @param month [Integer] represents the month of the date - # # @param year [Integer] represents the year of the date - # - # # @return [Date] # # @example @@ -95,7 +92,6 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18 # @example # Faker::Date.in_date_period(month: 2) #=> # # - # # @faker.version next def in_date_period(month: nil, year: ::Date.today.year) from = ::Date.new(year, month || 1, 1)