Skip to content

Commit

Permalink
Use Enumerator.produce to find last friday
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusRich committed Jul 16, 2024
1 parent 03f9344 commit f0fcef5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/gold_miner/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ def self.pretty_date(date)
end

def self.last_friday
date = Date.today
one_day_ago = (date - 1)
one_week_ago = date - 7
friday = one_day_ago.downto(one_week_ago).find { |date| date.friday? }

friday.to_s
Enumerator.produce(Date.today - 1, &:prev_day).find { |date| date.friday? }.to_s
end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/gold_miner/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
require "spec_helper"

RSpec.describe GoldMiner::Helpers do
describe GoldMiner::Helpers::Time do
describe ".last_friday" do
it "returns the last Friday when today is Saturday" do
travel_to Date.new(2024, 7, 13) do
result = described_class.last_friday

expect(result).to eq("2024-07-12")
end
end

it "returns the last Friday when today is Friday" do
travel_to Date.new(2024, 7, 12) do
result = described_class.last_friday

expect(result).to eq("2024-07-05")
end
end

it "returns the last Friday when today is Thursday" do
travel_to Date.new(2024, 7, 11) do
result = described_class.last_friday

expect(result).to eq("2024-07-05")
end
end
end
end

describe GoldMiner::Helpers::Sentence do
describe ".from" do
it "returns a sentence from a list of words" do
Expand Down

0 comments on commit f0fcef5

Please sign in to comment.