Class: Availability::Renderer::View
- Inherits:
-
Object
- Object
- Availability::Renderer::View
- 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
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#today ⇒ Object
readonly
Returns the value of attribute today.
Instance Method Summary collapse
- #format_compact_date(date) ⇒ Object
- #format_date(date) ⇒ Object
- #format_time(time) ⇒ Object
- #format_week_label(week) ⇒ Object
- #h(value) ⇒ Object
-
#initialize(days:, generated_at:, timezone:, enabled:, today:, days_to_show:, first_day_of_week:) ⇒ View
constructor
A new instance of View.
- #period_description ⇒ Object
- #template_binding ⇒ Object
- #timezone_name ⇒ Object
- #updated_at ⇒ Object
- #weekdays ⇒ Object
- #weeks ⇒ Object
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
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
33 34 35 |
# File 'lib/availability/renderer.rb', line 33 def enabled @enabled end |
#today ⇒ Object (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_description ⇒ Object
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_binding ⇒ Object
45 46 47 |
# File 'lib/availability/renderer.rb', line 45 def template_binding binding end |
#timezone_name ⇒ Object
87 88 89 |
# File 'lib/availability/renderer.rb', line 87 def timezone_name @timezone.identifier end |
#updated_at ⇒ Object
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 |
#weekdays ⇒ Object
79 80 81 |
# File 'lib/availability/renderer.rb', line 79 def weekdays WEEKDAYS.rotate(WEEKDAYS.index(@first_day_of_week)) end |
#weeks ⇒ Object
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 |