Module: Dsu::Support::TimesSortable

Extended by:
TimesSortable
Included in:
BaseCLI, Dsu::Services::EntryGroup::BrowseService, TimesSortable
Defined in:
lib/dsu/support/times_sortable.rb

Instance Method Summary collapse

Instance Method Details

#sorted_dsu_times_for(times:) ⇒ Object



6
7
8
9
10
# File 'lib/dsu/support/times_sortable.rb', line 6

def sorted_dsu_times_for(times:)
  configuration = Models::Configuration.new unless defined?(configuration) && configuration
  entries_display_order = configuration.entries_display_order
  times_sort(times: times_for(times: times), entries_display_order: entries_display_order)
end

#times_for(times:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dsu/support/times_sortable.rb', line 31

def times_for(times:)
  times = times.dup
  validate_times_argument!(times: times)

  start_date = times.max
  return times unless start_date.monday? || start_date.on_weekend?

  # If the start date is a weekend or a Monday then we need to look back
  # to include the preceeding Friday upto and including the start date.
  (0..3).filter_map do |num|
    time = start_date - num.days
    next unless time == start_date || time.on_weekend? || time.friday?

    time
  end
end

#times_sort(times:, entries_display_order: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dsu/support/times_sortable.rb', line 12

def times_sort(times:, entries_display_order: nil)
  times = times.dup
  entries_display_order ||= :asc

  validate_times_argument!(times: times)
  validate_entries_display_order_argument!(entries_display_order: entries_display_order)

  return times if times.one?

  # NOTE: The times array needs to be sorted unconditionally because if
  # the sort is ascending, then the times array needs to be returned
  # in ascending order. If the sort is descending, then in order to
  # properly reverse the times array, it needs to first be sorted in
  # ascending order before being reversed.
  return times.sort if entries_display_order == :asc

  times.sort_by { |time| -time.to_i }
end