From a53ec81938a529854fd54aa1fa617e18c8fe7453 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 00:16:28 +0900 Subject: [PATCH 01/13] add table header --- Makefile | 7 +++++++ lib/sg_strange_calendar/table.rb | 21 +++++++++++++++++++++ test/sg_strange_calendar/table_test.rb | 14 ++++++++++++++ 3 files changed, 42 insertions(+) create mode 100755 Makefile create mode 100644 lib/sg_strange_calendar/table.rb create mode 100644 test/sg_strange_calendar/table_test.rb diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..8cd2982 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: test/calendar +test/calendar: + ruby test/sg_strange_calendar_test.rb --no-plugins + +.PHONY: test/table +test/table: + ruby test/sg_strange_calendar/table_test.rb --no-plugins \ No newline at end of file diff --git a/lib/sg_strange_calendar/table.rb b/lib/sg_strange_calendar/table.rb new file mode 100644 index 0000000..32428a9 --- /dev/null +++ b/lib/sg_strange_calendar/table.rb @@ -0,0 +1,21 @@ +module SgStrangeCalendar + class Table + def initialize(year, today = nil) + @year = year + @today = today || Date.today + end + + def generate + [ + header(@year), + + ] + end + + private + + def header(year) + [year] + %w[Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo] + end + end +end diff --git a/test/sg_strange_calendar/table_test.rb b/test/sg_strange_calendar/table_test.rb new file mode 100644 index 0000000..e2430f1 --- /dev/null +++ b/test/sg_strange_calendar/table_test.rb @@ -0,0 +1,14 @@ +require 'minitest/autorun' +require 'date' +require_relative '../../lib/sg_strange_calendar/table' + +class SgStrangeCalendar::TableTest < Minitest::Test + def test_header + expected = <<~TXT.chomp + 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo + TXT + table = SgStrangeCalendar::Table.new(2024).generate + assert_equal expected, table[0].join(' ') + + end +end \ No newline at end of file From c78868b1cb51b4dcfd3722a23a5bf417319f26c3 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 00:42:23 +0900 Subject: [PATCH 02/13] add table content --- lib/sg_strange_calendar/table.rb | 42 +++++++++++++++++++++++--- test/sg_strange_calendar/table_test.rb | 19 +++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/sg_strange_calendar/table.rb b/lib/sg_strange_calendar/table.rb index 32428a9..99fe2fc 100644 --- a/lib/sg_strange_calendar/table.rb +++ b/lib/sg_strange_calendar/table.rb @@ -1,21 +1,53 @@ module SgStrangeCalendar class Table + WEEKDAYS = %w[Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo] + def initialize(year, today = nil) @year = year @today = today || Date.today end def generate - [ - header(@year), - - ] + [ header(@year) ] + + ((0..11).to_a).map { |month| Row.new(@year, month, @today).generate } end private def header(year) - [year] + %w[Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo] + [year] + WEEKDAYS + end + + class Row + def initialize(year, month, today = nil) + @year = year + @month = month + @today = today || Date.today + end + + def generate + date = start_of_month + [ start_of_month.strftime('%b') ] + + start_blank_days + + (start_of_month..end_of_month).to_a + + end_blank_days + end + + def start_blank_days + start_of_month.strftime('%w').to_i.times.map { |_| nil } + end + + def end_blank_days + (WEEKDAYS.size - start_blank_days.length - end_of_month.day ).times.map { |_| nil } + end + + def start_of_month + Date.new(@year, @month + 1, 1) + end + + def end_of_month + Date.new(@year, @month + 1, -1) + end end end end diff --git a/test/sg_strange_calendar/table_test.rb b/test/sg_strange_calendar/table_test.rb index e2430f1..fd63b53 100644 --- a/test/sg_strange_calendar/table_test.rb +++ b/test/sg_strange_calendar/table_test.rb @@ -5,10 +5,27 @@ class SgStrangeCalendar::TableTest < Minitest::Test def test_header expected = <<~TXT.chomp - 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo + 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo TXT table = SgStrangeCalendar::Table.new(2024).generate assert_equal expected, table[0].join(' ') + end + + def test_rows_count + expected = 12 + 1 + table = SgStrangeCalendar::Table.new(2024).generate + assert_equal expected, table.size + end + def test_rows_length + expected = SgStrangeCalendar::Table::WEEKDAYS.size + 1 + table = SgStrangeCalendar::Table.new(2024).generate + table.each do |row| + assert_equal expected, row.size + end + end + + def test_contents + table = SgStrangeCalendar::Table.new(2024).generate end end \ No newline at end of file From fb509fbe6d70140e8c8d24f70215e02a0fdc041f Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 00:56:23 +0900 Subject: [PATCH 03/13] fix self review --- Makefile | 6 +++++- lib/sg_strange_calendar/table.rb | 2 +- test/sg_strange_calendar/table_test.rb | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8cd2982..c8c9bfe 100755 --- a/Makefile +++ b/Makefile @@ -4,4 +4,8 @@ test/calendar: .PHONY: test/table test/table: - ruby test/sg_strange_calendar/table_test.rb --no-plugins \ No newline at end of file + ruby test/sg_strange_calendar/table_test.rb --no-plugins + +.PHONY: test/horizonal +test/horizonal: + ruby test/sg_strange_calendar/presenter/horizonal_presenter_test.rb --no-plugins diff --git a/lib/sg_strange_calendar/table.rb b/lib/sg_strange_calendar/table.rb index 99fe2fc..67e4317 100644 --- a/lib/sg_strange_calendar/table.rb +++ b/lib/sg_strange_calendar/table.rb @@ -1,4 +1,4 @@ -module SgStrangeCalendar +class SgStrangeCalendar class Table WEEKDAYS = %w[Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo] diff --git a/test/sg_strange_calendar/table_test.rb b/test/sg_strange_calendar/table_test.rb index fd63b53..c2e1684 100644 --- a/test/sg_strange_calendar/table_test.rb +++ b/test/sg_strange_calendar/table_test.rb @@ -28,4 +28,4 @@ def test_rows_length def test_contents table = SgStrangeCalendar::Table.new(2024).generate end -end \ No newline at end of file +end From 3e8166a33751451b6b1f9d15eeba6b9278a538de Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 07:49:32 +0900 Subject: [PATCH 04/13] clear level 1 --- lib/sg_strange_calendar.rb | 14 +++++- .../presenter/base_presenter.rb | 7 +++ .../presenter/horizonal_presenter.rb | 33 ++++++++++++++ lib/sg_strange_calendar/table.rb | 1 + .../presenter/horizonal_presenter_test.rb | 43 +++++++++++++++++++ 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 lib/sg_strange_calendar/presenter/base_presenter.rb create mode 100644 lib/sg_strange_calendar/presenter/horizonal_presenter.rb create mode 100644 test/sg_strange_calendar/presenter/horizonal_presenter_test.rb diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index 5bfc5e5..713a458 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -1,9 +1,19 @@ +require_relative 'sg_strange_calendar/table' +require_relative 'sg_strange_calendar/presenter/horizonal_presenter' + class SgStrangeCalendar def initialize(year, today = nil) - # write your code here + @year = year + @today = today || Date.today end def generate(vertical: false) - # write your code here + # [ + # %w[year Su Mo Tu We Th Fr ... Sa Su Mo] + # [Mon, nil, nil, ... Date1, Date2, ... Date31, nil, nil, ...]] + # ... + # ] + table = SgStrangeCalendar::Table.new(@year, @today) + SgStrangeCalendar::Presenter::HorizonalPresenter.present(table.generate) end end diff --git a/lib/sg_strange_calendar/presenter/base_presenter.rb b/lib/sg_strange_calendar/presenter/base_presenter.rb new file mode 100644 index 0000000..1700b05 --- /dev/null +++ b/lib/sg_strange_calendar/presenter/base_presenter.rb @@ -0,0 +1,7 @@ +require_relative 'sg_strange_calendar/presenter/base_presenter' + +class BasePresenter + def present + raise NotImplementedError, "You must implement the present method" + end +end diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb new file mode 100644 index 0000000..feda85f --- /dev/null +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -0,0 +1,33 @@ +require 'date' + +class SgStrangeCalendar + class Presenter + class HorizonalPresenter + def self.present(array) + array.map do |row| + date = row[1..].map do |cell| + display_value(cell) + end.join(' ') + # row[0] is left-justified + # row[1..] is right-justified + # rsrip is to remove trailing spaces + [ + display_value(row[0]).ljust(4), + date + ].join(' ').rstrip + end.join("\n") + end + + class << self + def display_value(value) + # for debug + # return '-+' if(value.nil?) + return ' ' if(value.nil?) + return value.day.to_s.rjust(2) if(value.is_a?(Date)) + + value.to_s.rjust(2) + end + end + end + end +end diff --git a/lib/sg_strange_calendar/table.rb b/lib/sg_strange_calendar/table.rb index 67e4317..58f04b7 100644 --- a/lib/sg_strange_calendar/table.rb +++ b/lib/sg_strange_calendar/table.rb @@ -8,6 +8,7 @@ def initialize(year, today = nil) end def generate + # header + 12 months [ header(@year) ] + ((0..11).to_a).map { |month| Row.new(@year, month, @today).generate } end diff --git a/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb b/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb new file mode 100644 index 0000000..4d6cf24 --- /dev/null +++ b/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb @@ -0,0 +1,43 @@ +require 'minitest/autorun' +require_relative '../../../lib/sg_strange_calendar/presenter/horizonal_presenter' + +class SgStrangeCalendar::Presenter::HorizonalPresenterTest < Minitest::Test + def test_present_with_no_nil + expected = <<~TXT.chomp + Jan b c d e f g + Feb i j k l m n + TXT + array = [ + ['Jan', 'b', 'c', 'd', 'e', 'f', 'g'], + ['Feb', 'i', 'j', 'k', 'l', 'm', 'n'] + ] + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array) + assert_equal expected, presenter + end + + def test_present_with_nil + expected = <<~TXT.chomp + Jan b c d e f g + Feb i j k l m + TXT + array = [ + ['Jan', 'b', 'c', 'd', 'e', 'f', 'g'], + ['Feb', 'i', 'j', 'k', 'l', 'm', nil] + ] + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array) + assert_equal expected, presenter + end + + def test_present_date + expected = <<~TXT.chomp + Jan 1 2 3 4 5 6 + Feb i j k l m + TXT + array = [ + ['Jan'] + (1..6).to_a.map { |i| Date.new(2024, 1, i) }, + ['Feb', 'i', 'j', 'k', 'l', 'm', nil] + ] + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array) + assert_equal expected, presenter + end +end From ca0627a1a71e26960f0e8e365981f51811b14c46 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 09:02:14 +0900 Subject: [PATCH 05/13] clear level 2 --- lib/sg_strange_calendar.rb | 4 +- .../presenter/horizonal_presenter.rb | 26 ++++++-- lib/sg_strange_calendar/table.rb | 2 +- .../presenter/horizonal_presenter_test.rb | 61 ++++++++++++++++--- test/sg_strange_calendar_test.rb | 6 +- 5 files changed, 79 insertions(+), 20 deletions(-) diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index 713a458..a50db1a 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -4,7 +4,7 @@ class SgStrangeCalendar def initialize(year, today = nil) @year = year - @today = today || Date.today + @today = today end def generate(vertical: false) @@ -14,6 +14,6 @@ def generate(vertical: false) # ... # ] table = SgStrangeCalendar::Table.new(@year, @today) - SgStrangeCalendar::Presenter::HorizonalPresenter.present(table.generate) + SgStrangeCalendar::Presenter::HorizonalPresenter.present(table.generate, @today) end end diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb index feda85f..3acf689 100644 --- a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -3,30 +3,46 @@ class SgStrangeCalendar class Presenter class HorizonalPresenter - def self.present(array) + def self.present(array, today) array.map do |row| date = row[1..].map do |cell| - display_value(cell) + display_value(cell, today) end.join(' ') # row[0] is left-justified # row[1..] is right-justified # rsrip is to remove trailing spaces - [ - display_value(row[0]).ljust(4), + row = [ + display_value(row[0], today).ljust(4), date ].join(' ').rstrip + # [xx] を含む row は今日を含んでいると判断する + if row.match?(/[\[|\]]/) + # 右側へのはみ出しを削る + row = row.match?(/\] /) ? row.gsub(/\] /, ']') : row + # 2桁以上の場合は、左側へのはみ出しを削る + if today.day > 9 + row = row.match?(/ \[/) ? row.gsub(/ \[/, '[') : row + end + end + row end.join("\n") end class << self - def display_value(value) + def display_value(value, today) # for debug # return '-+' if(value.nil?) return ' ' if(value.nil?) + return "[#{value.day}]".rjust(2) if(is_today?(value, today)) return value.day.to_s.rjust(2) if(value.is_a?(Date)) value.to_s.rjust(2) end + + def is_today?(value, today) + false if value.is_a?(Date) + value == today + end end end end diff --git a/lib/sg_strange_calendar/table.rb b/lib/sg_strange_calendar/table.rb index 58f04b7..c400c28 100644 --- a/lib/sg_strange_calendar/table.rb +++ b/lib/sg_strange_calendar/table.rb @@ -4,7 +4,7 @@ class Table def initialize(year, today = nil) @year = year - @today = today || Date.today + @today = today end def generate diff --git a/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb b/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb index 4d6cf24..7a4c904 100644 --- a/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb +++ b/test/sg_strange_calendar/presenter/horizonal_presenter_test.rb @@ -4,40 +4,83 @@ class SgStrangeCalendar::Presenter::HorizonalPresenterTest < Minitest::Test def test_present_with_no_nil expected = <<~TXT.chomp - Jan b c d e f g - Feb i j k l m n + Jan b c d e f g + Feb i j k l m n TXT array = [ ['Jan', 'b', 'c', 'd', 'e', 'f', 'g'], ['Feb', 'i', 'j', 'k', 'l', 'm', 'n'] ] - presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array) + today = Date.new(2025, 1, 1) + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array, today) assert_equal expected, presenter end def test_present_with_nil expected = <<~TXT.chomp - Jan b c d e f g - Feb i j k l m + Jan b c d e f g + Feb i j k l m TXT array = [ ['Jan', 'b', 'c', 'd', 'e', 'f', 'g'], ['Feb', 'i', 'j', 'k', 'l', 'm', nil] ] - presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array) + today = Date.new(2025, 1, 1) + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array, today) assert_equal expected, presenter end def test_present_date expected = <<~TXT.chomp - Jan 1 2 3 4 5 6 - Feb i j k l m + Jan 1 2 3 4 5 6 + Feb i j k l m TXT array = [ ['Jan'] + (1..6).to_a.map { |i| Date.new(2024, 1, i) }, ['Feb', 'i', 'j', 'k', 'l', 'm', nil] ] - presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array) + today = Date.new(2025, 1, 1) + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array, today) + assert_equal expected, presenter + end + + def test_present_first_today + expected = <<~TXT.chomp + Jan [1] 2 3 4 5 6 + Feb i j k l m + TXT + array = [ + ['Jan'] + (1..6).to_a.map { |i| Date.new(2024, 1, i) }, + ['Feb', 'i', 'j', 'k', 'l', 'm', nil] + ] + today = Date.new(2024, 1, 1) + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array, today) + assert_equal expected, presenter + end + def test_present_second_today + expected = <<~TXT.chomp + Jan 1 [2] 3 4 5 6 + Feb i j k l m + TXT + array = [ + ['Jan'] + (1..6).to_a.map { |i| Date.new(2024, 1, i) }, + ['Feb', 'i', 'j', 'k', 'l', 'm', nil] + ] + today = Date.new(2024, 1, 2) + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array, today) + assert_equal expected, presenter + end + def test_present_end_today + expected = <<~TXT.chomp + Jan 26 27 28 29 30[31] + Feb i j k l m + TXT + array = [ + ['Jan'] + (26..31).to_a.map { |i| Date.new(2024, 1, i) }, + ['Feb', 'i', 'j', 'k', 'l', 'm', nil] + ] + today = Date.new(2024, 1, 31) + presenter = SgStrangeCalendar::Presenter::HorizonalPresenter.present(array, today) assert_equal expected, presenter end end diff --git a/test/sg_strange_calendar_test.rb b/test/sg_strange_calendar_test.rb index bc5016a..c3820fd 100644 --- a/test/sg_strange_calendar_test.rb +++ b/test/sg_strange_calendar_test.rb @@ -44,7 +44,7 @@ def test_level_1_for_2025 end def test_level_2_for_2024_01_01 - skip "レベル2にチャレンジする人はこの行を削除してください" + # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan [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 @@ -66,7 +66,7 @@ def test_level_2_for_2024_01_01 end def test_level_2_for_2024_12_09 - skip "レベル2にチャレンジする人はこの行を削除してください" + # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan 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 @@ -88,7 +88,7 @@ def test_level_2_for_2024_12_09 end def test_level_2_for_2025_03_31 - skip "レベル2にチャレンジする人はこの行を削除してください" + # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2025 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan 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 From 383563c530fee2d674f265b7cc31b357d0075cfe Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 09:29:03 +0900 Subject: [PATCH 06/13] add vertical_presenter --- Makefile | 4 ++ lib/sg_strange_calendar.rb | 6 ++- .../presenter/vertical_presenter.rb | 41 +++++++++++++++++++ .../presenter/vertical_presenter_test.rb | 29 +++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 lib/sg_strange_calendar/presenter/vertical_presenter.rb create mode 100644 test/sg_strange_calendar/presenter/vertical_presenter_test.rb diff --git a/Makefile b/Makefile index c8c9bfe..bfaa439 100755 --- a/Makefile +++ b/Makefile @@ -9,3 +9,7 @@ test/table: .PHONY: test/horizonal test/horizonal: ruby test/sg_strange_calendar/presenter/horizonal_presenter_test.rb --no-plugins + +.PHONY: test/vertical +test/vertical: + ruby test/sg_strange_calendar/presenter/vertical_presenter_test.rb --no-plugins diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index a50db1a..9d4e0fa 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -14,6 +14,10 @@ def generate(vertical: false) # ... # ] table = SgStrangeCalendar::Table.new(@year, @today) - SgStrangeCalendar::Presenter::HorizonalPresenter.present(table.generate, @today) + if vertical + table.generate.transpose + else + SgStrangeCalendar::Presenter::HorizonalPresenter.present(table.generate, @today) + end end end diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb new file mode 100644 index 0000000..c3cee60 --- /dev/null +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -0,0 +1,41 @@ +require 'date' + +class SgStrangeCalendar + class Presenter + class VerticalPresenter + def self.present(array, today) + rows = [([today.year()] + array[0]).join(' ')] + + # row[0] is left-justified + # row[1..] is right-justified + # rsrip is to remove trailing spaces + array[1..].map do |row| + date = row[1..].map do |cell| + display_value(cell, today) + end + + [ + row[0].ljust(4), + date + ].join(' ') + end + + rows.join("\n") + end + + class << self + def display_value(value, today) + # for debug + # return '-+/' if(value.nil?) + return ' ' if(value.nil?) + # return "[#{value.day}]".rjust(3) if(is_today?(value, today)) + return value.day.to_s.rjust(3) if(value.is_a?(Date)) + value.to_s.rjust(3) + end + + def is_today?(value, today) + false + end + end + end + end +end diff --git a/test/sg_strange_calendar/presenter/vertical_presenter_test.rb b/test/sg_strange_calendar/presenter/vertical_presenter_test.rb new file mode 100644 index 0000000..80e5316 --- /dev/null +++ b/test/sg_strange_calendar/presenter/vertical_presenter_test.rb @@ -0,0 +1,29 @@ +require 'minitest/autorun' +require_relative '../../../lib/sg_strange_calendar/presenter/vertical_presenter' + +class SgStrangeCalendar::Presenter::VerticalPresenterTest < Minitest::Test + def test_header + expected = <<~TXT.chomp + 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + TXT + array = [ + %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] + ] + today = Date.new(2024, 1, 1) + presenter = SgStrangeCalendar::Presenter::VerticalPresenter.present(array, today) + assert_equal expected, presenter + end + def test_first_row + expected = <<~TXT.chomp + 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + Su 1 1 + TXT + array = [ + %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec], + ['Su', nil, nil, nil, nil, nil, nil, nil, nil, 1, nil, nil, 1] + ] + today = Date.new(2024, 1, 1) + presenter = SgStrangeCalendar::Presenter::VerticalPresenter.present(array, today) + assert_equal expected, presenter + end +end From c82b7ba4abf9ff7b308c51816feb7701d1e8e1ba Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 10:04:37 +0900 Subject: [PATCH 07/13] clear level3 --- lib/sg_strange_calendar.rb | 11 ++++++++-- .../presenter/horizonal_presenter.rb | 10 ++++----- .../presenter/vertical_presenter.rb | 21 +++++++++++++------ .../presenter/vertical_presenter_test.rb | 18 ++++++++++++++-- test/sg_strange_calendar_test.rb | 7 ------- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index 9d4e0fa..b3a1657 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -1,5 +1,6 @@ require_relative 'sg_strange_calendar/table' require_relative 'sg_strange_calendar/presenter/horizonal_presenter' +require_relative 'sg_strange_calendar/presenter/vertical_presenter' class SgStrangeCalendar def initialize(year, today = nil) @@ -15,9 +16,15 @@ def generate(vertical: false) # ] table = SgStrangeCalendar::Table.new(@year, @today) if vertical - table.generate.transpose + SgStrangeCalendar::Presenter::VerticalPresenter.present( + table.generate.transpose, + @today + ) else - SgStrangeCalendar::Presenter::HorizonalPresenter.present(table.generate, @today) + SgStrangeCalendar::Presenter::HorizonalPresenter.present( + table.generate, + @today + ) end end end diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb index 3acf689..23fcc04 100644 --- a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -11,20 +11,20 @@ def self.present(array, today) # row[0] is left-justified # row[1..] is right-justified # rsrip is to remove trailing spaces - row = [ + str_row = [ display_value(row[0], today).ljust(4), date ].join(' ').rstrip # [xx] を含む row は今日を含んでいると判断する - if row.match?(/[\[|\]]/) + if str_row.match?(/[\[|\]]/) # 右側へのはみ出しを削る - row = row.match?(/\] /) ? row.gsub(/\] /, ']') : row + str_row = str_row.match?(/\] /) ? str_row.gsub(/\] /, ']') : str_row # 2桁以上の場合は、左側へのはみ出しを削る if today.day > 9 - row = row.match?(/ \[/) ? row.gsub(/ \[/, '[') : row + str_row = str_row.match?(/ \[/) ? str_row.gsub(/ \[/, '[') : str_row end end - row + str_row end.join("\n") end diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb index c3cee60..7118bad 100644 --- a/lib/sg_strange_calendar/presenter/vertical_presenter.rb +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -4,21 +4,27 @@ class SgStrangeCalendar class Presenter class VerticalPresenter def self.present(array, today) - rows = [([today.year()] + array[0]).join(' ')] + + rows = [(array[0]).join(' ')] + # row[0] is left-justified # row[1..] is right-justified # rsrip is to remove trailing spaces - array[1..].map do |row| + str_row = array[1..].map do |row| date = row[1..].map do |cell| display_value(cell, today) end - [ + str_row = [ row[0].ljust(4), date ].join(' ') + # [xx] を含む row は今日を含んでいると判断する + # 1文字右側にずらす + if !today.nil? && today.day < 10 + str_row = str_row.gsub(/\] /, ']') + str_row = str_row.gsub(/\[/, ' [') + end + str_row.rstrip end - rows.join("\n") end @@ -27,13 +33,16 @@ def display_value(value, today) # for debug # return '-+/' if(value.nil?) return ' ' if(value.nil?) - # return "[#{value.day}]".rjust(3) if(is_today?(value, today)) + if(is_today?(value, today)) + return "[#{value.day}]".rjust(3) + end return value.day.to_s.rjust(3) if(value.is_a?(Date)) value.to_s.rjust(3) end def is_today?(value, today) - false + false if value.is_a?(Date) + value == today end end end diff --git a/test/sg_strange_calendar/presenter/vertical_presenter_test.rb b/test/sg_strange_calendar/presenter/vertical_presenter_test.rb index 80e5316..eda1520 100644 --- a/test/sg_strange_calendar/presenter/vertical_presenter_test.rb +++ b/test/sg_strange_calendar/presenter/vertical_presenter_test.rb @@ -1,3 +1,4 @@ +require 'date' require 'minitest/autorun' require_relative '../../../lib/sg_strange_calendar/presenter/vertical_presenter' @@ -7,7 +8,7 @@ def test_header 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec TXT array = [ - %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] + %w[2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec] ] today = Date.new(2024, 1, 1) presenter = SgStrangeCalendar::Presenter::VerticalPresenter.present(array, today) @@ -19,11 +20,24 @@ def test_first_row Su 1 1 TXT array = [ - %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec], + %w[2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec], ['Su', nil, nil, nil, nil, nil, nil, nil, nil, 1, nil, nil, 1] ] today = Date.new(2024, 1, 1) presenter = SgStrangeCalendar::Presenter::VerticalPresenter.present(array, today) assert_equal expected, presenter end + def test_today_row + expected = <<~TXT.chomp + 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + Su [1] 1 + TXT + array = [ + %w[2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec], + ['Su', nil, nil, nil, nil, nil, nil, nil, nil, Date.new(2024, 9, 1), nil, nil, 1] + ] + today = Date.new(2024, 9, 1) + presenter = SgStrangeCalendar::Presenter::VerticalPresenter.present(array, today) + assert_equal expected, presenter + end end diff --git a/test/sg_strange_calendar_test.rb b/test/sg_strange_calendar_test.rb index c3820fd..f8bdbf0 100644 --- a/test/sg_strange_calendar_test.rb +++ b/test/sg_strange_calendar_test.rb @@ -44,7 +44,6 @@ def test_level_1_for_2025 end def test_level_2_for_2024_01_01 - # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan [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 @@ -66,7 +65,6 @@ def test_level_2_for_2024_01_01 end def test_level_2_for_2024_12_09 - # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan 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 @@ -88,7 +86,6 @@ def test_level_2_for_2024_12_09 end def test_level_2_for_2025_03_31 - # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2025 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan 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 @@ -110,7 +107,6 @@ def test_level_2_for_2025_03_31 end def test_level_3_for_2024 - skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Su 1 1 @@ -156,7 +152,6 @@ def test_level_3_for_2024 end def test_level_3_for_2024_01_01 - skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Su 1 1 @@ -203,7 +198,6 @@ def test_level_3_for_2024_01_01 end def test_level_3_for_2024_12_09 - skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Su 1 1 @@ -250,7 +244,6 @@ def test_level_3_for_2024_12_09 end def test_level_3_for_2025_03_31 - skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2025 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Su 1 From d8e989ebc98a02fa1f56b4f7c9d028b39ce74176 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 11:37:01 +0900 Subject: [PATCH 08/13] use base presenter --- .../presenter/base_presenter.rb | 43 +++++++++++++++++-- .../presenter/horizonal_presenter.rb | 40 +++++------------ .../presenter/vertical_presenter.rb | 40 +++++------------ 3 files changed, 60 insertions(+), 63 deletions(-) diff --git a/lib/sg_strange_calendar/presenter/base_presenter.rb b/lib/sg_strange_calendar/presenter/base_presenter.rb index 1700b05..e332511 100644 --- a/lib/sg_strange_calendar/presenter/base_presenter.rb +++ b/lib/sg_strange_calendar/presenter/base_presenter.rb @@ -1,7 +1,42 @@ -require_relative 'sg_strange_calendar/presenter/base_presenter' - class BasePresenter - def present - raise NotImplementedError, "You must implement the present method" + def self.present(array, today) + (header(array) + content(array, today)).join("\n") + end + class << self + def header(array) + [(array[0]).join(' ')] + end + + def content(array, today) + array[1..].map do |row| + str_row = normal_row(row, today) + # [xx] を含む row は今日を含んでいると判断する + if is_contain_today?(str_row) + str_row = update_today_str(str_row, today) + end + # rsrip is to remove trailing spaces + str_row.rstrip + end + end + + def normal_row(row, today) + # row[0] is left-justified + # row[1..] is right-justified + [ + row[0].ljust(4), # 4 equal years length + row[1..].map do |cell| + display_value(cell, today) + end + ].join(' ') + end + + def is_today?(value, today) + false if value.is_a?(Date) + value == today + end + + def is_contain_today?(str_row) + str_row.match?(/[\[|\]]/) + end end end diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb index 23fcc04..e107250 100644 --- a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -1,33 +1,9 @@ require 'date' +require_relative 'base_presenter' class SgStrangeCalendar class Presenter - class HorizonalPresenter - def self.present(array, today) - array.map do |row| - date = row[1..].map do |cell| - display_value(cell, today) - end.join(' ') - # row[0] is left-justified - # row[1..] is right-justified - # rsrip is to remove trailing spaces - str_row = [ - display_value(row[0], today).ljust(4), - date - ].join(' ').rstrip - # [xx] を含む row は今日を含んでいると判断する - if str_row.match?(/[\[|\]]/) - # 右側へのはみ出しを削る - str_row = str_row.match?(/\] /) ? str_row.gsub(/\] /, ']') : str_row - # 2桁以上の場合は、左側へのはみ出しを削る - if today.day > 9 - str_row = str_row.match?(/ \[/) ? str_row.gsub(/ \[/, '[') : str_row - end - end - str_row - end.join("\n") - end - + class HorizonalPresenter < BasePresenter class << self def display_value(value, today) # for debug @@ -39,9 +15,15 @@ def display_value(value, today) value.to_s.rjust(2) end - def is_today?(value, today) - false if value.is_a?(Date) - value == today + def update_today_str(str_row, today) + # 右側へのはみ出しを削る + str_row = str_row.match?(/\] /) ? str_row.gsub(/\] /, ']') : str_row + # 2桁以上の場合は、左側へのはみ出しを削る + if today.day > 9 + str_row = str_row.match?(/ \[/) ? str_row.gsub(/ \[/, '[') : str_row + end + + str_row end end end diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb index 7118bad..491b50a 100644 --- a/lib/sg_strange_calendar/presenter/vertical_presenter.rb +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -1,37 +1,13 @@ require 'date' +require_relative 'base_presenter' class SgStrangeCalendar class Presenter - class VerticalPresenter - def self.present(array, today) - rows = [(array[0]).join(' ')] + - # row[0] is left-justified - # row[1..] is right-justified - # rsrip is to remove trailing spaces - str_row = array[1..].map do |row| - date = row[1..].map do |cell| - display_value(cell, today) - end - - str_row = [ - row[0].ljust(4), - date - ].join(' ') - # [xx] を含む row は今日を含んでいると判断する - # 1文字右側にずらす - if !today.nil? && today.day < 10 - str_row = str_row.gsub(/\] /, ']') - str_row = str_row.gsub(/\[/, ' [') - end - str_row.rstrip - end - rows.join("\n") - end - + class VerticalPresenter < BasePresenter class << self def display_value(value, today) # for debug - # return '-+/' if(value.nil?) + # return '-+*' if(value.nil?) return ' ' if(value.nil?) if(is_today?(value, today)) return "[#{value.day}]".rjust(3) @@ -40,9 +16,13 @@ def display_value(value, today) value.to_s.rjust(3) end - def is_today?(value, today) - false if value.is_a?(Date) - value == today + def update_today_str(str_row, today) + if today.day < 10 + str_row = str_row.gsub(/\] /, ']') + str_row = str_row.gsub(/\[/, ' [') + end + + str_row end end end From 5296b96ad8a0e4eeadc3af61596d3e24bc2218f4 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 11:50:30 +0900 Subject: [PATCH 09/13] refactor --- .../presenter/horizonal_presenter.rb | 9 +++------ .../presenter/vertical_presenter.rb | 12 +++++------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb index e107250..b6a359d 100644 --- a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -16,14 +16,11 @@ def display_value(value, today) end def update_today_str(str_row, today) - # 右側へのはみ出しを削る - str_row = str_row.match?(/\] /) ? str_row.gsub(/\] /, ']') : str_row # 2桁以上の場合は、左側へのはみ出しを削る - if today.day > 9 - str_row = str_row.match?(/ \[/) ? str_row.gsub(/ \[/, '[') : str_row - end + str_row = str_row.gsub(/ \[/, '[') if today.day > 9 - str_row + # 右側へのはみ出しを削る + str_row.gsub(/\] /, ']') end end end diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb index 491b50a..a8e3486 100644 --- a/lib/sg_strange_calendar/presenter/vertical_presenter.rb +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -9,18 +9,16 @@ def display_value(value, today) # for debug # return '-+*' if(value.nil?) return ' ' if(value.nil?) - if(is_today?(value, today)) - return "[#{value.day}]".rjust(3) - end + return "[#{value.day}]".rjust(3) if(is_today?(value, today)) return value.day.to_s.rjust(3) if(value.is_a?(Date)) + value.to_s.rjust(3) end def update_today_str(str_row, today) - if today.day < 10 - str_row = str_row.gsub(/\] /, ']') - str_row = str_row.gsub(/\[/, ' [') - end + # 10 文字より小さかったら、日付の前にスペースを追加 + # 1 文字ずつずらす + return str_row.gsub(/\] /, ']').gsub(/\[/, ' [') if today.day < 10 str_row end From c72e73aa12448e2aceeaaa858ecace577158163a Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 19:09:07 +0900 Subject: [PATCH 10/13] refactor --- lib/sg_strange_calendar.rb | 11 +-- .../presenter/base_presenter.rb | 71 +++++++++++-------- .../presenter/horizonal_presenter.rb | 25 +++---- .../presenter/vertical_presenter.rb | 25 +++---- lib/sg_strange_calendar/table.rb | 2 +- 5 files changed, 65 insertions(+), 69 deletions(-) diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index b3a1657..6405ca3 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -14,15 +14,16 @@ def generate(vertical: false) # [Mon, nil, nil, ... Date1, Date2, ... Date31, nil, nil, ...]] # ... # ] - table = SgStrangeCalendar::Table.new(@year, @today) + table = SgStrangeCalendar::Table.new(@year, @today).generate + if vertical - SgStrangeCalendar::Presenter::VerticalPresenter.present( - table.generate.transpose, + SgStrangeCalendar::Presenter::VerticalPresenter.new.present( + table.transpose, @today ) else - SgStrangeCalendar::Presenter::HorizonalPresenter.present( - table.generate, + SgStrangeCalendar::Presenter::HorizonalPresenter.new.present( + table, @today ) end diff --git a/lib/sg_strange_calendar/presenter/base_presenter.rb b/lib/sg_strange_calendar/presenter/base_presenter.rb index e332511..385ceab 100644 --- a/lib/sg_strange_calendar/presenter/base_presenter.rb +++ b/lib/sg_strange_calendar/presenter/base_presenter.rb @@ -1,42 +1,51 @@ class BasePresenter - def self.present(array, today) + def present(array, today) (header(array) + content(array, today)).join("\n") end - class << self - def header(array) - [(array[0]).join(' ')] - end - def content(array, today) - array[1..].map do |row| - str_row = normal_row(row, today) - # [xx] を含む row は今日を含んでいると判断する - if is_contain_today?(str_row) - str_row = update_today_str(str_row, today) - end - # rsrip is to remove trailing spaces - str_row.rstrip + def header(array) + [(array[0]).join(' ')] + end + + def content(array, today) + array[1..].map do |row| + str_row = normal_row(row, today) + # [xx] を含む row は今日を含んでいると判断する + if is_contain_today?(str_row) + str_row = update_today_str(str_row, today) end + # rsrip is to remove trailing spaces + str_row.rstrip end + end - def normal_row(row, today) - # row[0] is left-justified - # row[1..] is right-justified - [ - row[0].ljust(4), # 4 equal years length - row[1..].map do |cell| - display_value(cell, today) - end - ].join(' ') - end + def normal_row(row, today) + # row[0] is left-justified + # row[1..] is right-justified + [ + row[0].ljust(4), # 4 equal years length + row[1..].map do |cell| + display_value(cell, today) + end + ].join(' ') + end - def is_today?(value, today) - false if value.is_a?(Date) - value == today - end + def display_value(value, today) + # for debug + # return '-+' if(value.nil?) + return ' '.rjust(space_size) if(value.nil?) + return "[#{value.day}]".rjust(space_size) if(is_today?(value, today)) + return value.day.to_s.rjust(space_size) if(value.is_a?(Date)) - def is_contain_today?(str_row) - str_row.match?(/[\[|\]]/) - end + value.to_s.rjust(space_size) + end + + def is_today?(value, today) + false if value.is_a?(Date) + value == today + end + + def is_contain_today?(str_row) + str_row.match?(/[\[|\]]/) end end diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb index b6a359d..3de0e71 100644 --- a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -4,24 +4,17 @@ class SgStrangeCalendar class Presenter class HorizonalPresenter < BasePresenter - class << self - def display_value(value, today) - # for debug - # return '-+' if(value.nil?) - return ' ' if(value.nil?) - return "[#{value.day}]".rjust(2) if(is_today?(value, today)) - return value.day.to_s.rjust(2) if(value.is_a?(Date)) - - value.to_s.rjust(2) - end + SPACE_SIZE = 2 + def space_size + SPACE_SIZE + end - def update_today_str(str_row, today) - # 2桁以上の場合は、左側へのはみ出しを削る - str_row = str_row.gsub(/ \[/, '[') if today.day > 9 + def update_today_str(str_row, today) + # 2桁以上の場合は、左側へのはみ出しを削る + str_row = str_row.gsub(/ \[/, '[') if today.day >= 10 - # 右側へのはみ出しを削る - str_row.gsub(/\] /, ']') - end + # 右側へのはみ出しを削る + str_row.gsub(/\] /, ']') end end end diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb index a8e3486..b0dfed8 100644 --- a/lib/sg_strange_calendar/presenter/vertical_presenter.rb +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -4,24 +4,17 @@ class SgStrangeCalendar class Presenter class VerticalPresenter < BasePresenter - class << self - def display_value(value, today) - # for debug - # return '-+*' if(value.nil?) - return ' ' if(value.nil?) - return "[#{value.day}]".rjust(3) if(is_today?(value, today)) - return value.day.to_s.rjust(3) if(value.is_a?(Date)) - - value.to_s.rjust(3) - end + SPACE_SIZE = 3 + def space_size + SPACE_SIZE + end - def update_today_str(str_row, today) - # 10 文字より小さかったら、日付の前にスペースを追加 - # 1 文字ずつずらす - return str_row.gsub(/\] /, ']').gsub(/\[/, ' [') if today.day < 10 + def update_today_str(str_row, today) + # 10 文字より小さかったら、日付の前にスペースを追加 + # 1 文字ずつずらす + return str_row.gsub(/\] /, ']').gsub(/\[/, ' [') if today.day < 10 - str_row - end + str_row end end end diff --git a/lib/sg_strange_calendar/table.rb b/lib/sg_strange_calendar/table.rb index c400c28..3e50b86 100644 --- a/lib/sg_strange_calendar/table.rb +++ b/lib/sg_strange_calendar/table.rb @@ -9,7 +9,7 @@ def initialize(year, today = nil) def generate # header + 12 months - [ header(@year) ] + + table ||= [ header(@year) ] + ((0..11).to_a).map { |month| Row.new(@year, month, @today).generate } end From ede4c1bfa8cedee550dd75642360017d0263aef5 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 20:00:46 +0900 Subject: [PATCH 11/13] add error message --- lib/sg_strange_calendar.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index 6405ca3..252086b 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -9,6 +9,7 @@ def initialize(year, today = nil) end def generate(vertical: false) + return invalid_message unless valid? # [ # %w[year Su Mo Tu We Th Fr ... Sa Su Mo] # [Mon, nil, nil, ... Date1, Date2, ... Date31, nil, nil, ...]] @@ -28,4 +29,23 @@ def generate(vertical: false) ) end end + + private + + def invalid_message + year = 1996 + today = Date.new(1996, 3, 2) + table = SgStrangeCalendar::Table.new(year, today).generate + "\n==== error === invalid === year ====" \ + + "\n\n" + ("🎂" * 40) + "\n\n" \ + + SgStrangeCalendar::Presenter::HorizonalPresenter.new.present( + table, + today) \ + + "\n\n" + ("🎂" * 40) \ + + "\n\n" + "しげるの誕生日は 1996年3月2日 です!\n" + end + + def valid? + 1000 <= @year && @year <= 9999 + end end From c192d477f1784b49c2d749999d6ef3ef543d0556 Mon Sep 17 00:00:00 2001 From: 4geru Date: Tue, 29 Oct 2024 20:23:16 +0900 Subject: [PATCH 12/13] change today as instance variable --- lib/sg_strange_calendar.rb | 8 +++--- .../presenter/base_presenter.rb | 26 +++++++++++-------- .../presenter/horizonal_presenter.rb | 4 +-- .../presenter/vertical_presenter.rb | 4 +-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index 252086b..88a0cb3 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -18,14 +18,12 @@ def generate(vertical: false) table = SgStrangeCalendar::Table.new(@year, @today).generate if vertical - SgStrangeCalendar::Presenter::VerticalPresenter.new.present( + SgStrangeCalendar::Presenter::VerticalPresenter.new(@today).present( table.transpose, - @today ) else - SgStrangeCalendar::Presenter::HorizonalPresenter.new.present( + SgStrangeCalendar::Presenter::HorizonalPresenter.new(@today).present( table, - @today ) end end @@ -38,7 +36,7 @@ def invalid_message table = SgStrangeCalendar::Table.new(year, today).generate "\n==== error === invalid === year ====" \ + "\n\n" + ("🎂" * 40) + "\n\n" \ - + SgStrangeCalendar::Presenter::HorizonalPresenter.new.present( + + SgStrangeCalendar::Presenter::HorizonalPresenter.new(today).present( table, today) \ + "\n\n" + ("🎂" * 40) \ diff --git a/lib/sg_strange_calendar/presenter/base_presenter.rb b/lib/sg_strange_calendar/presenter/base_presenter.rb index 385ceab..3039b41 100644 --- a/lib/sg_strange_calendar/presenter/base_presenter.rb +++ b/lib/sg_strange_calendar/presenter/base_presenter.rb @@ -1,48 +1,52 @@ class BasePresenter - def present(array, today) - (header(array) + content(array, today)).join("\n") + def initialize(today) + @today = today + end + + def present(array) + (header(array) + content(array)).join("\n") end def header(array) [(array[0]).join(' ')] end - def content(array, today) + def content(array) array[1..].map do |row| - str_row = normal_row(row, today) + str_row = normal_row(row) # [xx] を含む row は今日を含んでいると判断する if is_contain_today?(str_row) - str_row = update_today_str(str_row, today) + str_row = update_today_str(str_row) end # rsrip is to remove trailing spaces str_row.rstrip end end - def normal_row(row, today) + def normal_row(row) # row[0] is left-justified # row[1..] is right-justified [ row[0].ljust(4), # 4 equal years length row[1..].map do |cell| - display_value(cell, today) + display_value(cell) end ].join(' ') end - def display_value(value, today) + def display_value(value) # for debug # return '-+' if(value.nil?) return ' '.rjust(space_size) if(value.nil?) - return "[#{value.day}]".rjust(space_size) if(is_today?(value, today)) + return "[#{value.day}]".rjust(space_size) if(is_today?(value)) return value.day.to_s.rjust(space_size) if(value.is_a?(Date)) value.to_s.rjust(space_size) end - def is_today?(value, today) + def is_today?(value) false if value.is_a?(Date) - value == today + value == @today end def is_contain_today?(str_row) diff --git a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb index 3de0e71..d537f77 100644 --- a/lib/sg_strange_calendar/presenter/horizonal_presenter.rb +++ b/lib/sg_strange_calendar/presenter/horizonal_presenter.rb @@ -9,9 +9,9 @@ def space_size SPACE_SIZE end - def update_today_str(str_row, today) + def update_today_str(str_row) # 2桁以上の場合は、左側へのはみ出しを削る - str_row = str_row.gsub(/ \[/, '[') if today.day >= 10 + str_row = str_row.gsub(/ \[/, '[') if @today.day >= 10 # 右側へのはみ出しを削る str_row.gsub(/\] /, ']') diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb index b0dfed8..cdb8e43 100644 --- a/lib/sg_strange_calendar/presenter/vertical_presenter.rb +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -9,10 +9,10 @@ def space_size SPACE_SIZE end - def update_today_str(str_row, today) + def update_today_str(str_row) # 10 文字より小さかったら、日付の前にスペースを追加 # 1 文字ずつずらす - return str_row.gsub(/\] /, ']').gsub(/\[/, ' [') if today.day < 10 + return str_row.gsub(/\] /, ']').gsub(/\[/, ' [') if @today.day < 10 str_row end From 176e6e5ff12464bbeb5b3ed0a4f8c4784e4d051b Mon Sep 17 00:00:00 2001 From: 4geru Date: Sun, 8 Dec 2024 00:44:10 +0900 Subject: [PATCH 13/13] update the latest version --- lib/sg_strange_calendar/presenter/vertical_presenter.rb | 8 ++++---- test/sg_strange_calendar_test.rb | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/sg_strange_calendar/presenter/vertical_presenter.rb b/lib/sg_strange_calendar/presenter/vertical_presenter.rb index cdb8e43..33f31a7 100644 --- a/lib/sg_strange_calendar/presenter/vertical_presenter.rb +++ b/lib/sg_strange_calendar/presenter/vertical_presenter.rb @@ -10,11 +10,11 @@ def space_size end def update_today_str(str_row) - # 10 文字より小さかったら、日付の前にスペースを追加 - # 1 文字ずつずらす - return str_row.gsub(/\] /, ']').gsub(/\[/, ' [') if @today.day < 10 + # 2桁以上は、右側へのはみ出しを削る + str_row = str_row.gsub(/\[/, ' [') if @today.day < 10 - str_row + # 右側へのはみ出しを削る + str_row.gsub(/\] /, ']') end end end diff --git a/test/sg_strange_calendar_test.rb b/test/sg_strange_calendar_test.rb index b7c510a..4494cd0 100644 --- a/test/sg_strange_calendar_test.rb +++ b/test/sg_strange_calendar_test.rb @@ -107,7 +107,6 @@ def test_level_2_for_2025_03_31 end def test_level_2_all - skip "レベル2にチャレンジする人はこの行を削除してください" file_path = File.expand_path('level2.txt', File.dirname(__FILE__)) calendars = File.read(file_path).lines.each_slice(13).map(&:join).map(&:chomp) from_date = Date.new(2025, 1, 1) @@ -303,7 +302,6 @@ def test_level_3_for_2025_03_31 end def test_level_3_all - skip "レベル2およびレベル3にチャレンジする人はこの行を削除してください" file_path = File.expand_path('level3.txt', File.dirname(__FILE__)) calendars = File.read(file_path).lines.each_slice(38).map(&:join).map(&:chomp) from_date = Date.new(2025, 1, 1)