Module: Prekin

Defined in:
lib/prekin.rb,
lib/prekin/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'1.1.0'.freeze

Class Method Summary collapse

Class Method Details

.next(year_month_str = nil) ⇒ Object

TODO: 月および年をまたぐ場合の処理が都度都度になっており、複雑すぎる



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/prekin.rb', line 12

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