Extract item update quality logic into its own method

This commit is contained in:
Feken Baboyan 2021-03-18 00:17:01 -04:00
parent 899821440c
commit 47d4825933
2 changed files with 70 additions and 47 deletions

View File

@ -7,49 +7,8 @@ module GildedRose
def update_quality() def update_quality()
items.each do |item| items.each do |item|
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" item.update_quality
if item.quality > 0 item.update_sell_in
if item.name != "Sulfuras, Hand of Ragnaros"
item.quality = item.quality - 1
end
end
else
if item.quality < 50
item.quality = item.quality + 1
if item.name == "Backstage passes to a TAFKAL80ETC concert"
if item.sell_in < 11
if item.quality < 50
item.quality = item.quality + 1
end
end
if item.sell_in < 6
if item.quality < 50
item.quality = item.quality + 1
end
end
end
end
end
if item.name != "Sulfuras, Hand of Ragnaros"
item.sell_in = 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 = item.quality - 1
end
end
else
item.quality = item.quality - item.quality
end
else
if item.quality < 50
item.quality = item.quality + 1
end
end
end
end end
end end
@ -80,7 +39,9 @@ module GildedRose
end end
def update_quality def update_quality
if item.quality > 0 if item.sell_in <= 0 && item.quality > 0
item.quality -= 2
elsif item.quality > 0
item.quality -= 1 item.quality -= 1
end end
end end
@ -98,8 +59,44 @@ module GildedRose
end end
class GenericItemWrapper < AbstractItemWrapper; end class GenericItemWrapper < AbstractItemWrapper; end
class AgedBrieItemWrapper < AbstractItemWrapper; end class AgedBrieItemWrapper < AbstractItemWrapper;
class BackstagePassesItemWrapper < AbstractItemWrapper; end def update_quality
class SulfurasItemWrapper < AbstractItemWrapper; end if item.quality < 50
item.quality = item.quality + 1
end
end
end
class BackstagePassesItemWrapper < AbstractItemWrapper;
# if sell_in is 0, then quality becomes 0
# if quality is already 50, then quality stays at 50
# if sell in is 5 or below, quality increases by 3
# if sell in is 10 or below, quality increases by 2
# if sell in is 10 or greater, quality increases by 1
def update_quality
if item.sell_in == 0
item.quality = 0
elsif reached_max_quality?
# noop
elsif item.sell_in < 6
item.quality += 3
elsif item.sell_in < 11
item.quality += 2
else
item.quality += 1
end
end
def reached_max_quality?
item.quality >= 50
end
end
class SulfurasItemWrapper < AbstractItemWrapper;
def update_quality
end
def update_sell_in
end
end
end end

View File

@ -89,6 +89,27 @@ module GildedRose
assert_equal initial_item_quality, item.quality assert_equal initial_item_quality, item.quality
end end
test "Backstage passes quality increases by 1 when the item has more than 10 days to expire" do
initial_item_quality = 10
item = make_item(name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 11, quality: initial_item_quality)
gilded_rose = Store.new([item])
gilded_rose.update_quality
assert_equal initial_item_quality + 1, item.quality
end
test "Backstage passes quality never goes above 50" do
initial_item_quality = 50
item = make_item(name: "Backstage passes to a TAFKAL80ETC concert", quality: initial_item_quality)
gilded_rose = Store.new([item])
gilded_rose.update_quality
assert_equal initial_item_quality, item.quality
end
test "Backstage passes quality increases by 2 when the item has 10 days or less to expire" do test "Backstage passes quality increases by 2 when the item has 10 days or less to expire" do
initial_item_quality = 10 initial_item_quality = 10
@ -123,6 +144,11 @@ module GildedRose
assert_equal 0, item.quality assert_equal 0, item.quality
end end
private
def make_item(name:, sell_in: 1, quality:)
Item.new(name, sell_in, quality)
end
end end
end end