Class: CalendarFormatter::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/calendar_formatter/formatter.rb

Constant Summary collapse

DAYS_PER_MONTH =

42 - 6 weeks - max amount of weeks in month. Needs for padding.

42

Instance Method Summary collapse

Constructor Details

#initialize(start_wday: :monday) ⇒ Object

Create formatter.

Parameters:

  • start_wday (Symbol) (defaults to: :monday)

    available values from :monday to :sunday



16
17
18
19
20
21
# File 'lib/calendar_formatter/formatter.rb', line 16

def initialize(start_wday: :monday)
  unless ::DateAndTime::Calculations::DAYS_INTO_WEEK.has_key?(start_wday)
    raise ArgumentError, 'Argument start_wday is not correct'
  end
  @start_wday = start_wday
end

Instance Method Details

#month(date, padding: false) ⇒ Range<Date>

Get range of formatted dates for month.

Parameters:

  • date (Date)
  • padding (Boolean) (defaults to: false)

Returns:

  • (Range<Date>)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/calendar_formatter/formatter.rb', line 42

def month(date, padding: false)
  first_month_day = date.beginning_of_month
  last_month_day = first_month_day.end_of_month

  first_week_day = first_month_day.beginning_of_week(@start_wday)
  last_week_day = last_month_day.end_of_week(@start_wday)

  if padding
    while (last_week_day - first_week_day).to_i + 1 < DAYS_PER_MONTH
      last_week_day = last_week_day + 1.week
    end
  end

  first_week_day .. last_week_day
end

#week(date) ⇒ Range<Date>

Get range of formatted dates for week.

Parameters:

  • date (Date)

Returns:

  • (Range<Date>)


29
30
31
32
33
# File 'lib/calendar_formatter/formatter.rb', line 29

def week(date)
  first_week_day = date.beginning_of_week(@start_wday)
  last_week_day = date.end_of_week(@start_wday)
  first_week_day .. last_week_day
end