mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
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
102 lines
2.3 KiB
Ruby
102 lines
2.3 KiB
Ruby
class GildedRose
|
|
QUALITY_LOWER_LIMIT = 0
|
|
QUALITY_UPPER_LIMIT = 50
|
|
|
|
def initialize(items)
|
|
@items = items
|
|
end
|
|
|
|
def update_quality
|
|
@items.each do |item|
|
|
case item.name
|
|
when 'Aged Brie'
|
|
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
|
|
update_normal_quality(item)
|
|
end
|
|
end
|
|
end
|
|
|
|
def update_aged_brie_quality(item)
|
|
decrease_sell_in_day(item)
|
|
|
|
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)
|
|
decrease_sell_in_day(item)
|
|
return item.quality = 0 if item.sell_in.negative?
|
|
|
|
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)
|
|
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
|
|
attr_accessor :name, :sell_in, :quality
|
|
|
|
def initialize(name, sell_in, quality)
|
|
@name = name
|
|
@sell_in = sell_in
|
|
@quality = quality
|
|
end
|
|
|
|
def to_s()
|
|
"#{@name}, #{@sell_in}, #{@quality}"
|
|
end
|
|
end
|