Class: Timet::WeekInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/timet/week_info.rb

Overview

The WeekInfo class encapsulates the date string and weeks array and provides methods for formatting and determining week information.

It is instantiated for each date entry in the TimeBlockChart and helps decide how the week number is displayed and whether a separator line is needed before the entry.

Constant Summary collapse

WEEKEND_DAYS =

Initializes a new WeekInfo instance.

%w[Sat Sun].freeze

Instance Method Summary collapse

Constructor Details

#initialize(date_object, date_string_for_display, weeks_array_ref) ⇒ WeekInfo

Returns a new instance of WeekInfo.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/timet/week_info.rb', line 21

def initialize(date_object, date_string_for_display, weeks_array_ref)
  @date_string = date_string_for_display # Use the passed string for display
  @current_cweek = date_object.cweek

  is_first_entry = weeks_array_ref.empty?
  is_new_week = is_first_entry || @current_cweek != weeks_array_ref.last

  # A separator is needed if this entry starts a new week group, but it's not the very first one.
  @print_separator_before_this = is_new_week && !is_first_entry

  # The week number is underlined if it's the first time this week appears.
  @week_display_string = if is_new_week
                           format('%02d', @current_cweek).underline
                         else
                           '  '
                         end

  weeks_array_ref << @current_cweek # Record this week as processed
end

Instance Method Details

#format_and_print_date_info(day) ⇒ void

This method returns an undefined value.

Formats and prints the date information

Parameters:

  • day (String)

    The day of the week



52
53
54
55
56
57
58
59
# File 'lib/timet/week_info.rb', line 52

def format_and_print_date_info(day)
  weekend_str = @date_string # Use the original date string for display
  is_weekend_day = WEEKEND_DAYS.include?(day)
  day_str = is_weekend_day ? day.red : day
  weekend_str = weekend_str.red if is_weekend_day

  print ''.gray + "#{@week_display_string} #{weekend_str} #{day_str}" + '┆- '.gray
end

#needs_inter_week_separator?Boolean

Indicates whether an inter-week separator line should be printed before this date’s entry.

Returns:

  • (Boolean)

    True if a separator is needed, false otherwise.



44
45
46
# File 'lib/timet/week_info.rb', line 44

def needs_inter_week_separator?
  @print_separator_before_this
end