Skip to content

Commit

Permalink
feat: ๐ŸŽธ Prekin.next ใ‚’ๅฎŸ่ฃ…ใ—ใ€ๆฌกใฎใƒ—ใƒฌ้‡‘ใฎๅนดๆœˆๆ—ฅใ‚’ๅ–ๅพ—ใงใใ‚‹ใ‚ˆใ†ใซใ—ใŸ (#182)
Browse files Browse the repository at this point in the history
* feat: ๐ŸŽธ Prekin.next ใ‚’ๅฎŸ่ฃ…ใ—ใ€ๆฌกใฎใƒ—ใƒฌ้‡‘ใฎๅนดๆœˆๆ—ฅใ‚’ๅ–ๅพ—ใงใใ‚‹ใ‚ˆใ†ใซใ—ใŸ

* docs: โœ๏ธ README ใซ Prekin.next ใฎ่ชฌๆ˜Žใ‚’่ฟฝๅŠ ใ—ใŸ
  • Loading branch information
nikukyugamer committed Sep 5, 2023
1 parent de55f0f commit 1ae4ed3
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 76 additions & 0 deletions lib/prekin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
93 changes: 93 additions & 0 deletions spec/prekin_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1ae4ed3

Please sign in to comment.