Reuse some methods make methods more verbose

I was doubting if instead of decreasing the quality in 2 steps, adding a parameter to the decrease_quality method and decrease it in 1 go was a better option.
I didnt go for it in the end as I like the verbosity at the moment and it's easier to follow later on why we decrease multiple times instead of e.g. decrease_quality(n) method
This commit is contained in:
Maarten Parmentier 2023-06-27 21:10:03 +02:00
parent cc494bf27e
commit 815ba7ce24
3 changed files with 55 additions and 17 deletions

View File

@ -1,4 +1,6 @@
class GildedRose
QUALITY_LOWER_LIMIT = 0
QUALITY_UPPER_LIMIT = 50
def initialize(items)
@items = items
@ -11,6 +13,8 @@ class GildedRose
update_aged_brie_quality(item)
when 'Backstage passes to a TAFKAL80ETC concert'
update_backstage_passes_quality(item)
when 'Conjured Mana Cake'
update_conjured_quality(item)
when 'Sulfuras, Hand of Ragnaros'
update_sulfuras_quality(item)
else
@ -20,31 +24,66 @@ class GildedRose
end
def update_aged_brie_quality(item)
item.sell_in -= 1
decrease_sell_in_day(item)
item.quality += 1
item.quality += 1 if item.sell_in.negative?
item.quality = 50 if item.quality > 50
increase_item_quality(item)
increase_item_quality(item) if item.sell_in.negative?
keep_quality_upper_limit_in_bounds(item)
end
def update_backstage_passes_quality(item)
item.sell_in -= 1
decrease_sell_in_day(item)
return item.quality = 0 if item.sell_in.negative?
item.quality += 1
item.quality += 1 if item.sell_in < 10
item.quality += 1 if item.sell_in < 5
item.quality = 50 if item.quality > 50
increase_item_quality(item)
increase_item_quality(item) if item.sell_in < 10
increase_item_quality(item) if item.sell_in < 5
keep_quality_upper_limit_in_bounds(item)
end
def update_conjured_quality(item)
decrease_sell_in_day(item)
decrease_item_quality(item, 2)
decrease_item_quality(item, 2) if item.sell_in.negative?
keep_quality_lower_limit_in_bounds(item)
end
def update_normal_quality(item)
item.sell_in -= 1
item.quality -= 1
item.quality -= 1 if item.sell_in.negative?
item.quality = 0 if item.quality.negative?
decrease_sell_in_day(item)
decrease_item_quality(item)
decrease_item_quality(item) if item.sell_in.negative?
keep_quality_lower_limit_in_bounds(item)
end
def update_sulfuras_quality(item); end
private
def decrease_sell_in_day(item)
item.sell_in -= 1
end
def increase_item_quality(item)
item.quality += 1
end
def decrease_item_quality(item, by = 1)
item.quality -= by
end
def keep_quality_upper_limit_in_bounds(item)
item.quality = QUALITY_UPPER_LIMIT if item.quality > QUALITY_UPPER_LIMIT
end
def keep_quality_lower_limit_in_bounds(item)
item.quality = QUALITY_LOWER_LIMIT if item.quality < QUALITY_LOWER_LIMIT
end
end
class Item

View File

@ -220,7 +220,7 @@ describe GildedRose do
end
end
xcontext "when item name is 'Conjured Mana Cake'" do
context "when item name is 'Conjured Mana Cake'" do
let(:item_name) { 'Conjured Mana Cake' }
context 'when sell by date has not passed' do
@ -245,7 +245,7 @@ describe GildedRose do
it 'lowers sell_in by 1' do
subject.update_quality
expect(items[0].sell_in).to eq 2
expect(items[0].sell_in).to eq 0
end
end

View File

@ -12,8 +12,7 @@ items = [
Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20),
Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49),
Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49),
# This Conjured item does not work properly yet
Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O
Item.new(name="Conjured Mana Cake", sell_in=3, quality=6),
]
days = 2