Skip to content

A Procedural Solution #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b2422b2
Flatten first conditional block to clarify what item type is in scope.
plainprogrammer Oct 20, 2021
82d71d3
Replace negative condition with positive condition to reveal what ite…
plainprogrammer Oct 20, 2021
7ec8972
Convert second top-level condition to unless for clarity of check.
plainprogrammer Oct 20, 2021
262663d
Reverse conditional structure to state positive case for backstage pa…
plainprogrammer Oct 20, 2021
3548bed
Pulled nested conditional to higher level to reduce nesting in third …
plainprogrammer Oct 20, 2021
5a4aa41
Inlined all quality guard clauses to further flatten conditionals.
plainprogrammer Oct 20, 2021
5fc9d48
Extract decrement quality method to reduce duplication.
plainprogrammer Oct 20, 2021
27f9b34
Extract increment quality method to reduce duplication.
plainprogrammer Oct 20, 2021
b4076af
Changed first conditional section to a case statement to clarify item…
plainprogrammer Oct 20, 2021
b0e6242
Introduce ability to adjust quality increment via argument.
plainprogrammer Oct 20, 2021
f98b49c
Use ability to specify quality increment amount to clarify how backst…
plainprogrammer Oct 20, 2021
93f2801
Flatten nested conditional in third conditional section to clarify it…
plainprogrammer Oct 20, 2021
c7e74a2
Change third conditional section to use case to clarify item type beh…
plainprogrammer Oct 20, 2021
8ff3fb5
Extracted conditional for expiration detection into method to clarify…
plainprogrammer Oct 20, 2021
a27f6d8
Consolidate all item behaviors into singular case statement within up…
plainprogrammer Oct 20, 2021
d88e08d
Extract item type behaviors into discreet update methods.
plainprogrammer Oct 20, 2021
ca13f0b
Reordered methods to clarify their relationship to each other.
plainprogrammer Oct 20, 2021
5386e02
Changed expiration behavior for updating backstage pass for increased…
plainprogrammer Oct 20, 2021
e2ea8b4
Rearranged code for updating aged brie to adjust sell in before adjus…
plainprogrammer Oct 20, 2021
ce0c8f4
Rearranged code for updating backstage passes to adjust sell in befor…
plainprogrammer Oct 20, 2021
c95a9aa
Rearranged code for updating normal items to adjust sell in before ad…
plainprogrammer Oct 20, 2021
58de357
Add support for the Conjured Mana Cake item.
plainprogrammer Oct 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 68 additions & 42 deletions lib/gilded_rose.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
def update_quality(items)
items.each do |item|
if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert'
if item.quality > 0
if item.name != 'Sulfuras, Hand of Ragnaros'
item.quality -= 1
end
end
else
if item.quality < 50
item.quality += 1
if item.name == 'Backstage passes to a TAFKAL80ETC concert'
if item.sell_in < 11
if item.quality < 50
item.quality += 1
end
end
if item.sell_in < 6
if item.quality < 50
item.quality += 1
end
end
end
end
end
if item.name != 'Sulfuras, Hand of Ragnaros'
item.sell_in -= 1
end
if item.sell_in < 0
if item.name != "Aged Brie"
if item.name != 'Backstage passes to a TAFKAL80ETC concert'
if item.quality > 0
if item.name != 'Sulfuras, Hand of Ragnaros'
item.quality -= 1
end
end
else
item.quality = item.quality - item.quality
end
else
if item.quality < 50
item.quality += 1
end
end
case item.name
when 'NORMAL ITEM'
update_normal_item(item)
when 'Backstage passes to a TAFKAL80ETC concert'
update_backstage_pass(item)
when 'Aged Brie'
update_aged_brie(item)
when 'Conjured Mana Cake'
update_conjured_item(item)
else # Sulfuras
#No-Op
end
end
end

def update_normal_item(item)
item.sell_in -= 1

if expired?(item)
decrement_quality(item, 2)
else
decrement_quality(item)
end
end

def update_backstage_pass(item)
item.sell_in -= 1

if expired?(item)
item.quality = 0
elsif item.sell_in < 5
increment_quality(item, 3)
elsif item.sell_in < 10
increment_quality(item, 2)
else
increment_quality(item)
end
end

def update_aged_brie(item)
item.sell_in -= 1

if expired?(item)
increment_quality(item, 2)
else
increment_quality(item)
end
end

def update_conjured_item(item)
item.sell_in -= 1

if expired?(item)
decrement_quality(item, 4)
else
decrement_quality(item, 2)
end
end

def decrement_quality(item, amount = 1)
item.quality -= amount if item.quality > 0
end

def increment_quality(item, amount = 1)
item.quality += amount
item.quality = 50 if item.quality > 50
end

def expired?(item)
item.sell_in < 0
end

#----------------------------
# DO NOT CHANGE THINGS BELOW
#----------------------------
Expand Down
12 changes: 6 additions & 6 deletions spec/gilded_rose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,36 +191,36 @@
context 'before the sell date' do
let(:initial_sell_in) { 5 }

xit { expect(item.quality).to eq(initial_quality - 2) }
it { expect(item.quality).to eq(initial_quality - 2) }

context 'at zero quality' do
let(:initial_quality) { 0 }

xit { expect(item.quality).to eq(initial_quality) }
it { expect(item.quality).to eq(initial_quality) }
end
end

context 'on sell date' do
let(:initial_sell_in) { 0 }

xit { expect(item.quality).to eq(initial_quality - 4) }
it { expect(item.quality).to eq(initial_quality - 4) }

context 'at zero quality' do
let(:initial_quality) { 0 }

xit { expect(item.quality).to eq(initial_quality) }
it { expect(item.quality).to eq(initial_quality) }
end
end

context 'after sell date' do
let(:initial_sell_in) { -10 }

xit { expect(item.quality).to eq(initial_quality - 4) }
it { expect(item.quality).to eq(initial_quality - 4) }

context 'at zero quality' do
let(:initial_quality) { 0 }

xit { expect(item.quality).to eq(initial_quality) }
it { expect(item.quality).to eq(initial_quality) }
end
end
end
Expand Down