Class: Availability::Renderer::View

Inherits:
Object
  • Object
show all
Defined in:
lib/availability/renderer.rb

Overview

Exposes only display-safe availability values and formatting helpers to ERB.

Constant Summary collapse

WEEKDAYS =
%w[monday tuesday wednesday thursday friday saturday sunday].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(days:, generated_at:, timezone:, enabled:, today:, days_to_show:, first_day_of_week:) ⇒ View

Returns a new instance of View.



35
36
37
38
39
40
41
42
43
# File 'lib/availability/renderer.rb', line 35

def initialize(days:, generated_at:, timezone:, enabled:, today:, days_to_show:, first_day_of_week:)
  @days = days
  @generated_at = generated_at
  @timezone = timezone
  @enabled = enabled
  @today = today
  @days_to_show = days_to_show
  @first_day_of_week = first_day_of_week
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



33
34
35
# File 'lib/availability/renderer.rb', line 33

def enabled
  @enabled
end

#todayObject (readonly)

Returns the value of attribute today.



33
34
35
# File 'lib/availability/renderer.rb', line 33

def today
  @today
end

Instance Method Details

#format_compact_date(date) ⇒ Object



57
58
59
# File 'lib/availability/renderer.rb', line 57

def format_compact_date(date)
  date.strftime('%a %-d %b')
end

#format_date(date) ⇒ Object



53
54
55
# File 'lib/availability/renderer.rb', line 53

def format_date(date)
  "#{date.strftime('%A')}, #{date.day} #{date.strftime('%B %Y')}"
end

#format_time(time) ⇒ Object



67
68
69
# File 'lib/availability/renderer.rb', line 67

def format_time(time)
  @timezone.to_local(time.getutc).strftime('%H:%M')
end

#format_week_label(week) ⇒ Object



61
62
63
64
65
# File 'lib/availability/renderer.rb', line 61

def format_week_label(week)
  first_date = week.compact.first.date
  week_start = first_date - weekday_offset(first_date)
  "Week of #{week_start.day} #{week_start.strftime('%B %Y')}"
end

#h(value) ⇒ Object



49
50
51
# File 'lib/availability/renderer.rb', line 49

def h(value)
  CGI.escapeHTML(value.to_s)
end

#period_descriptionObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/availability/renderer.rb', line 91

def period_description
  final_date = @today + @days_to_show - 1
  return @today.strftime('%-d %b %Y') if final_date == @today

  start_with_year = @today.strftime('%-d %b %Y')
  final_with_year = final_date.strftime('%-d %b %Y')
  return "#{start_with_year}#{final_with_year}" if @today.year != final_date.year

  return "#{@today.day}#{final_with_year}" if @today.month == final_date.month

  "#{@today.strftime('%-d %b')}#{final_with_year}"
end

#template_bindingObject



45
46
47
# File 'lib/availability/renderer.rb', line 45

def template_binding
  binding
end

#timezone_nameObject



87
88
89
# File 'lib/availability/renderer.rb', line 87

def timezone_name
  @timezone.identifier
end

#updated_atObject



83
84
85
# File 'lib/availability/renderer.rb', line 83

def updated_at
  @timezone.to_local(@generated_at.getutc).strftime('%-d %b %Y, %H:%M %Z')
end

#weekdaysObject



79
80
81
# File 'lib/availability/renderer.rb', line 79

def weekdays
  WEEKDAYS.rotate(WEEKDAYS.index(@first_day_of_week))
end

#weeksObject



71
72
73
74
75
76
77
# File 'lib/availability/renderer.rb', line 71

def weeks
  return [] if @days.empty?

  padded_days = Array.new(weekday_offset(@days.first.date)) + @days
  padded_days.concat(Array.new((7 - padded_days.length) % 7))
  padded_days.each_slice(7).to_a
end