Sha256: 6c002e5a8feec86629fc3b2d2744101988005a6850506aa6cc2b7d7889642b27
Contents?: true
Size: 1.03 KB
Versions: 126
Compression:
Stored size: 1.03 KB
Contents
# frozen_string_literal: true class ReeDate::PrevOccurring include Ree::FnDSL fn :prev_occurring do link :today link :advance link :monday link :days_since link 'ree_date/functions/constants', -> { DAYS_INTO_WEEK } end doc(<<~DOC) Returns a new date representing the previous occurrence of the specified day of week. today # => Tue, 24 May 2022 date (optional) # => Wed, 6 Apr 2022 prev_occurring(date, :monday) # => Mon, 28 Mar 2022 prev_occurring(date, :thursday) # => Thu, 31 Mar 2022 prev_occurring(:thursday) # => Thu, 19 May 2022 DOC contract(Nilor[Date], Or[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday] => Date) def call(date = nil, week_day) date = date || today days = nil if week_day == :sunday days = 6 else days = DAYS_INTO_WEEK.fetch(week_day) - 1 end advance(date, days: -7) .then { monday(_1) } .then { days_since(_1, days) } end end
Version data entries
126 entries across 126 versions & 1 rubygems