Sha256: 1dbd57b910e717c8592ea4201ae98964a187dd361acbcdfd1fd6374b08829378

Contents?: true

Size: 571 Bytes

Versions: 12

Compression:

Stored size: 571 Bytes

Contents

# frozen_string_literal: true

require "date"

module Matheus
  # Usage:
  #    $ date-of-last monday
  #    2022-01-24
  class DateOfLast < Command
    def call(args)
      day_name = args[0].to_s

      target_wday = wday_for(day_name) or return Failure("invalid day name: #{day_name.inspect}")
      date = Enumerator.produce(Date.today - 1, &:prev_day).find { _1.wday == target_wday }

      puts date
    end

    private

    def wday_for(day_name)
      Date::DAYNAMES.index(day_name.capitalize) || Date::ABBR_DAYNAMES.index(day_name.capitalize)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
matheus-0.7.1 lib/matheus/date_of_last.rb
matheus-0.7.0 lib/matheus/date_of_last.rb
matheus-0.6.4 lib/matheus/date_of_last.rb
matheus-0.6.3 lib/matheus/date_of_last.rb
matheus-0.6.2 lib/matheus/date_of_last.rb
matheus-0.6.1 lib/matheus/date_of_last.rb
matheus-0.6.0 lib/matheus/date_of_last.rb
matheus-0.5.0 lib/matheus/date_of_last.rb
matheus-0.4.0 lib/matheus/date_of_last.rb
matheus-0.3.0 lib/matheus/date_of_last.rb
matheus-0.2.1 lib/matheus/date_of_last.rb
matheus-0.2.0 lib/matheus/date_of_last.rb