From 1ae4ed3d89aa075f3c2939015db8e62747e713d0 Mon Sep 17 00:00:00 2001 From: Osamu Takiya Date: Tue, 5 Sep 2023 18:04:25 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Prekin.next=20=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E8=A3=85=E3=81=97=E3=80=81=E6=AC=A1=E3=81=AE=E3=83=97?= =?UTF-8?q?=E3=83=AC=E9=87=91=E3=81=AE=E5=B9=B4=E6=9C=88=E6=97=A5=E3=82=92?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=97=E3=81=9F=20(#182)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: ๐ŸŽธ Prekin.next ใ‚’ๅฎŸ่ฃ…ใ—ใ€ๆฌกใฎใƒ—ใƒฌ้‡‘ใฎๅนดๆœˆๆ—ฅใ‚’ๅ–ๅพ—ใงใใ‚‹ใ‚ˆใ†ใซใ—ใŸ * docs: โœ๏ธ README ใซ Prekin.next ใฎ่ชฌๆ˜Žใ‚’่ฟฝๅŠ ใ—ใŸ --- README.md | 6 +++ lib/prekin.rb | 76 ++++++++++++++++++++++++++++++++++++ spec/prekin_spec.rb | 93 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 spec/prekin_spec.rb diff --git a/README.md b/README.md index 4692d80..c3d86b6 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,12 @@ Or install it yourself as: #### 2-4. String class - `'2020/10/30 00:00:00 +09:00'.prekin? #=> true` +#### 2-5. Next Prekin +- `Prekin.next #=> "2020-10-30"` +- `Prekin.next('2023-12-28') #=> "2023-12-29"` +- `Prekin.next('2023-12-29') #=> "2023-12-29"` +- `Prekin.next('2023-12-30') #=> "2024-01-26"` + ## Note - This gem overwrites `Time`, `Date`, `DateTime` and `String` class. - So if you use `Time`, `Date`, `DateTime` and `String` class in your code, you should be careful. diff --git a/lib/prekin.rb b/lib/prekin.rb index 02d691d..cae3a9b 100644 --- a/lib/prekin.rb +++ b/lib/prekin.rb @@ -6,4 +6,80 @@ module Prekin class Error < StandardError; end + + class << self + # TODO: ๆœˆใŠใ‚ˆใณๅนดใ‚’ใพใŸใๅ ดๅˆใฎๅ‡ฆ็†ใŒ้ƒฝๅบฆ้ƒฝๅบฆใซใชใฃใฆใŠใ‚Šใ€่ค‡้›‘ใ™ใŽใ‚‹ + def next(year_month_str = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity + base_month = if year_month_str.nil? + Date.today.month + else + Date.parse(year_month_str).month + end + end_day_of_this_month = if year_month_str.nil? + Date.new(Date.today.year, base_month, -1) + else + Date.new( + Date.parse(year_month_str).year, + base_month, + -1 + ) + end + days_in_month = end_day_of_this_month.day + + # ็พๅœจใฎๆœˆใ‚’1ๆ—ฅใšใคๅข—ใ‚„ใ—ใชใŒใ‚‰ใ€ใใฎๆ—ฅใŒ้‡‘ๆ›œๆ—ฅใ‹ใฉใ†ใ‹ใ‚’ใƒใ‚งใƒƒใ‚ฏใ™ใ‚‹ + dates = [] + for i in 1..days_in_month # rubocop:disable Style/For + date = if year_month_str.nil? + Date.new(Date.today.year, base_month, i) + else + Date.new(Date.parse(year_month_str).year, base_month, i) + end + + dates << date if date.wday == 5 + end + + # ้‡‘ๆ›œๆ—ฅใงใ‚ใ‚‹ๆ—ฅไป˜ใ‚’้…ๅˆ—ใงๅพ—ใฆใ€ๆœ€็ต‚้‡‘ๆ›œๆ—ฅใ‚’่ฟ”ใ™ (String) + if dates.last < Date.parse(year_month_str || Date.today.to_s) + end_day_of_next_month = if year_month_str.nil? + if base_month == 12 + Date.new(Date.today.year + 1, 1, -1) + else + Date.new(Date.today.year, base_month + 1, -1) + end + elsif base_month == 12 + Date.new( + Date.parse(year_month_str).year + 1, + 1, + -1 + ) + else + Date.new( + Date.parse(year_month_str).year, + base_month + 1, + -1 + ) + end + days_in_month = end_day_of_next_month.day + + dates = [] + for i in 1..days_in_month # rubocop:disable Style/For + date = if year_month_str.nil? + if base_month == 12 # rubocop:disable Metrics/BlockNesting + Date.new(Date.today.year + 1, 1, i) + else + Date.new(Date.today.year, base_month + 1, i) + end + elsif base_month == 12 + Date.new(Date.parse(year_month_str).year + 1, 1, i) + else + Date.new(Date.parse(year_month_str).year, base_month + 1, i) + end + + dates << date if date.wday == 5 + end + end + + dates.last.to_s + end + end end diff --git a/spec/prekin_spec.rb b/spec/prekin_spec.rb new file mode 100644 index 0000000..faaa58a --- /dev/null +++ b/spec/prekin_spec.rb @@ -0,0 +1,93 @@ +# 10ๆœˆ 2020 +# ๆ—ฅ ๆœˆ ็ซ ๆฐด ๆœจ ้‡‘ ๅœŸ +# 1 2 3 +# 4 5 6 7 8 9 10 +# 11 12 13 14 15 16 17 +# 18 19 20 21 22 23 24 +# 25 26 27 28 29 30 31 +# +# 1ๆœˆ 2020 +# ๆ—ฅ ๆœˆ ็ซ ๆฐด ๆœจ ้‡‘ ๅœŸ +# 1 2 3 4 +# 5 6 7 8 9 10 11 +# 12 13 14 15 16 17 18 +# 19 20 21 22 23 24 25 +# 26 27 28 29 30 31 + +# 10ๆœˆ 2019 +# ๆ—ฅ ๆœˆ ็ซ ๆฐด ๆœจ ้‡‘ ๅœŸ +# 1 2 3 4 5 +# 6 7 8 9 10 11 12 +# 13 14 15 16 17 18 19 +# 20 21 22 23 24 25 26 +# 27 28 29 30 31 + +RSpec.describe Prekin do + describe 'ใ‚ฏใƒฉใ‚นใƒกใ‚ฝใƒƒใƒ‰' do + describe '#next' do + xit '2023/09/05 ใซใŠใ‘ใ‚‹ next ใฏ 2023/09/29 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใชใ—๏ผ‰' do + # TODO: travel_to ใ‚„ Timecop ใŒๅฟ…่ฆ + next_day = Prekin.next + + expect(next_day).to eq '2023-09-29' + end + + it '2023/09/05 ใซใŠใ‘ใ‚‹ next ใฏ 2023/09/29 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2023-09-05') + + expect(next_day).to eq '2023-09-29' + end + + it '2023/09/30 ใซใŠใ‘ใ‚‹ next ใฏ 2023/10/27 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2023-09-30') + + expect(next_day).to eq '2023-10-27' + end + + it '2023/12/28 ใซใŠใ‘ใ‚‹ next ใฏ 2023/12/29 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2023-12-28') + + expect(next_day).to eq '2023-12-29' + end + + it '2023/12/30 ใซใŠใ‘ใ‚‹ next ใฏ 2024/01/26 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2023-12-30') + + expect(next_day).to eq '2024-01-26' + end + + it '2022/12/24 ใซใŠใ‘ใ‚‹ next ใฏ 2022/12/30 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2022-12-24') + + expect(next_day).to eq '2022-12-30' + end + + it '2022/12/30 ใซใŠใ‘ใ‚‹ next ใฏ 2022/12/30 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2022-12-30') + + expect(next_day).to eq '2022-12-30' + end + + it '2022/12/31 ใซใŠใ‘ใ‚‹ next ใฏ 2023/01/27 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + next_day = Prekin.next('2022-12-31') + + expect(next_day).to eq '2023-01-27' + end + end + + describe '#previous' do + xit '2023/09/05 ใซใŠใ‘ใ‚‹ previous ใฏ 2023/08/25 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใชใ—๏ผ‰' do + # TODO: travel_to ใ‚„ Timecop ใŒๅฟ…่ฆ + previous_day = Prekin.previous + + expect(previous_day).to eq '2023-08-25' + end + + xit '2023/09/05 ใซใŠใ‘ใ‚‹ previous ใฏ 2023/08/25 ใงใ‚ใ‚‹ใ“ใจ๏ผˆๆ—ฅๆ™‚ๆŒ‡ๅฎšใ‚ใ‚Š๏ผ‰' do + previous_day = Prekin.previous('2023-09-05') + + expect(previous_day).to eq '2023-08-25' + end + end + end +end