Skip to content

Commit

Permalink
Merge pull request #18 from SOFware/feature/extend-cycle
Browse files Browse the repository at this point in the history
Feature/extend cycle
  • Loading branch information
jdowd authored Sep 4, 2024
2 parents 5e90cf9 + 6854d17 commit fe162d1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sof/cycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ def ==(other) = to_h == other.to_h
# Return the most recent completion date from the supplied array of dates
def last_completed(dates) = dates.compact.map(&:to_date).max

def extend_period(_ = nil) = self

# From the supplied anchor date, are there enough in-window completions to
# satisfy the cycle?
#
Expand Down
8 changes: 8 additions & 0 deletions lib/sof/cycles/within.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def self.recurring? = false

def to_s = "#{volume}x within #{date_range}"

def extend_period(count)
Cycle.for(
Parser.load(
parser.to_h.merge(period_count: period_count + count)
).to_s
)
end

def date_range
return humanized_span unless active?

Expand Down
1 change: 1 addition & 0 deletions spec/sof/cycles/calendar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module SOF
it_behaves_like "it computes #final_date(given)",
given: "1971-01-01", returns: "1971-12-31".to_date
it_behaves_like "last_completed is", :recent_date
it_behaves_like "it cannot be extended"

describe "#recurring?" do
it "repeats" do
Expand Down
1 change: 1 addition & 0 deletions spec/sof/cycles/end_of_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module SOF
it_behaves_like "#as_json returns the notation"
it_behaves_like "it computes #final_date(given)",
given: nil, returns: ("2020-01-01".to_date + 17.months).end_of_month
it_behaves_like "it cannot be extended"

describe "#last_completed" do
context "with an activated cycle" do
Expand Down
1 change: 1 addition & 0 deletions spec/sof/cycles/lookback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module SOF
it_behaves_like "it computes #final_date(given)",
given: "2003-03-08", returns: ("2003-03-08".to_date + 180.days)
it_behaves_like "last_completed is", :recent_date
it_behaves_like "it cannot be extended"

describe "#recurring?" do
it "repeats" do
Expand Down
6 changes: 6 additions & 0 deletions spec/sof/cycles/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@
expect(subject.last_completed(dates)).to eq(expected)
end
end

shared_examples_for "it cannot be extended" do
it "returns itself" do
expect(subject.extend_period(999)).to eq(subject)
end
end
1 change: 1 addition & 0 deletions spec/sof/cycles/volume_only_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module SOF
it_behaves_like "it computes #final_date(given)",
given: "2003-03-08", returns: nil
it_behaves_like "last_completed is", :recent_date
it_behaves_like "it cannot be extended"

describe "#recurring?" do
it "does not repeat" do
Expand Down
11 changes: 11 additions & 0 deletions spec/sof/cycles/within_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ module SOF
given: "_", returns: ("2020-08-01".to_date + 180.days)
it_behaves_like "last_completed is", :too_late

describe "#extend_period" do
it "increases the period by the given amount" do
aggregate_failures do
expect(cycle.notation).to eq("V2W180DF2020-08-01")

new_cycle = cycle.extend_period(30)
expect(new_cycle.notation).to eq("V2W210DF2020-08-01")
end
end
end

describe "#recurring?" do
it "does not repeat" do
expect(cycle).not_to be_recurring
Expand Down

0 comments on commit fe162d1

Please sign in to comment.