Class: RestPackageCpt::Cells::OpeningHours

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/rest_package_cpt/cells/opening_hours.rb

Overview

Manage Restaurant Opening Hours

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpeningHours

Returns a new instance of OpeningHours.



10
11
12
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 10

def initialize
  @cache = Rails.cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 8

def cache
  @cache
end

#packageObject

Returns the value of attribute package.



7
8
9
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 7

def package
  @package
end

#restaurantObject

Returns the value of attribute restaurant.



7
8
9
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 7

def restaurant
  @restaurant
end

Class Method Details

.minutes_to_human(minutes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 39

def minutes_to_human(minutes)
  return '' if minutes.blank?

  # return unlimited if minutes is 1_000_000
  return 'Unlimited' if minutes.to_i == HhPackage::Pricing::MAX_SEAT

  result = {}

  hours = minutes / 60
  result[:hours] = hours
  result[:minutes] = minutes % 60

  if minutes % 60 != 0 && minutes > 60
    return I18n.t('views.d.minutes_to_human.hours_minutes',
                  hour: result[:hours], minute: result[:minutes])
  end
  return I18n.t('views.d.minutes_to_human.hours', count: result[:hours]) if (minutes % 60).zero?
  return I18n.t('views.d.minutes_to_human.minutes', count: minutes) if minutes < 60

  ''
end

.minutes_to_label(minutes) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 61

def minutes_to_label(minutes)
  return '' if minutes.blank?
  return 'Unlimited' if minutes == HhPackage::UNLIMITED

  result = {}

  hours = minutes / 60
  result[:hours] = hours
  result[:minutes] = minutes % 60

  if minutes % 60 != 0 && minutes > 60
    return I18n.t('views.d.minutes_to_label.hours_minutes',
                  hour: result[:hours], minute: result[:minutes])
  end
  return I18n.t('views.d.minutes_to_label.hours', count: result[:hours]) if (minutes % 60).zero?
  return I18n.t('views.d.minutes_to_label.minutes', count: minutes) if minutes < 60

  ''
end

Instance Method Details

#search_availability_of_next_days(restaurant_package) ⇒ Object

Get next X availability of given date argument

Parameters:

  • x

    Integer

  • mode

    Symbol if equal days, then max of x argument value is 7

Returns:

  • Hash

    monday: '',
    tuesday: '',
    wednesday: '',
    etc...
    



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/my_lib/rest_package_cpt/cells/opening_hours.rb', line 24

def search_availability_of_next_days(restaurant_package)
  cache_key = restaurant_package.opening_hours.cache_key

  Rails.cache.fetch("restaurant_package:oh:#{cache_key}:#{I18n.locale}") do
    dummy_date_to_find_date = Date.parse('10 Oct 2021') # Sunday
    restaurant_package.opening_hours.map do |oh|
      {
        day_name: I18n.t((dummy_date_to_find_date + oh.cwday.days).strftime('%A')),
        schedule: oh.attributes.slice('open', 'close'),
      }
    end
  end
end