Sha256: b23d2649db6cc485538a20b5facaf2e761eca847967caa2ba888ca1a3673765c

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Dsu
  module Support
    module TimesSortable
      module_function

      def times_sort(times:, entries_display_order: nil)
        entries_display_order ||= 'asc'
        unless %w[asc desc].include? entries_display_order
          raise "Invalid entries_display_order: #{entries_display_order}"
        end

        if entries_display_order == 'asc'
          times.sort # sort ascending
        elsif entries_display_order == 'desc'
          times.sort.reverse # sort descending
        end
      end

      def times_for(times:)
        start_date = times.max
        return times unless start_date.monday? || start_date.on_weekend?

        # (0..3).map { |num| start_date - num.days }
        # (start_date..-start_date.friday?).map { |time| time }
        # (0..3).map { |num| start_date - num.days if start_date.on_weekend? || start_date.monday? }
        # If the start_date is a weekend or a Monday, then we need to include
        # start_date along with all the dates up to and including the previous
        # Monday.
        (0..3).filter_map do |num|
          time = start_date - num.days
          next unless time == start_date || time.on_weekend? || time.friday?

          time
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dsu-1.0.0 lib/dsu/support/times_sortable.rb
dsu-0.1.0.alpha.5 lib/dsu/support/times_sortable.rb
dsu-0.1.0.alpha.4 lib/dsu/support/times_sortable.rb
dsu-0.1.0.alpha.3 lib/dsu/support/times_sortable.rb
dsu-0.1.0.alpha.2 lib/dsu/support/times_sortable.rb