Sha256: 32fd75ee33b088e76602a39a092f0a6c1bb047a1c227fad5eae84594f2440774

Contents?: true

Size: 1.06 KB

Versions: 124

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

class ReeDatetime::PrevOccurring
  include Ree::FnDSL

  fn :prev_occurring do
    link :now
    link :change
    link :prev_occurring, from: :ree_date
  end

  doc(<<~DOC)
    Returns a new date/time representing the previous occurrence of the specified day of week.
    
      now                              # => Tue, 24 May 2022 13:15:20
      date_time (optional)                  # => Wed, 6  Apr 2022 13:15:20
      prev_occurring(date_time, :monday)    # => Mon, 28 Mar 2022 13:15:20
      prev_occurring(date_time, :thursday)  # => Thu, 31 Mar 2022 13:15:20
      prev_occurring(:thursday)        # => Thu, 19 May 2022 13:15:20
  DOC
  contract(
    Nilor[DateTime], 
    Or[
      :monday,
      :tuesday, 
      :wednesday, 
      :thursday, 
      :friday, 
      :saturday, 
      :sunday
    ] => DateTime
  )
  def call(date_time = nil, week_day)
    date_time = date_time || now
    
    change(
      prev_occurring(date_time, week_day),
      hour: date_time.hour,
      min: date_time.min,
      sec: date_time.sec
    )
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/prev_occurring.rb