mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Refactored to class methods
This commit is contained in:
parent
329effa522
commit
b94a1948de
@ -8,41 +8,15 @@ class GildedRose
|
|||||||
@items.each do |item|
|
@items.each do |item|
|
||||||
case item.name
|
case item.name
|
||||||
when "Aged Brie"
|
when "Aged Brie"
|
||||||
item.sell_in = item.sell_in - 1
|
return AgedBrieItem.update_quality(item)
|
||||||
item.quality = item.quality + 1 unless item.quality >= 50
|
|
||||||
return
|
|
||||||
when "Backstage passes to a TAFKAL80ETC concert"
|
when "Backstage passes to a TAFKAL80ETC concert"
|
||||||
item.sell_in = item.sell_in - 1
|
return BackstagePassItem.update_quality(item)
|
||||||
if item.sell_in < 0
|
|
||||||
item.quality = 0
|
|
||||||
elsif item.sell_in < 5
|
|
||||||
item.quality = item.quality + 3
|
|
||||||
elsif item.sell_in < 10
|
|
||||||
item.quality = item.quality + 2
|
|
||||||
else
|
|
||||||
item.quality = item.quality + 1
|
|
||||||
end
|
|
||||||
return
|
|
||||||
when "Conjured item"
|
when "Conjured item"
|
||||||
item.sell_in = item.sell_in - 1
|
return ConjuredItem.update_quality(item)
|
||||||
if item.sell_in < 0
|
|
||||||
item.quality = item.quality - 4
|
|
||||||
else
|
|
||||||
item.quality = item.quality - 2
|
|
||||||
end
|
|
||||||
item.quality = 0 if item.quality < 0
|
|
||||||
return
|
|
||||||
when "Sulfuras, Hand of Ragnaros"
|
when "Sulfuras, Hand of Ragnaros"
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
item.sell_in = item.sell_in - 1
|
return NormalItem.update_quality(item)
|
||||||
if item.sell_in < 0
|
|
||||||
item.quality = item.quality - 2
|
|
||||||
else
|
|
||||||
item.quality = item.quality - 1
|
|
||||||
end
|
|
||||||
item.quality = 0 if item.quality < 0
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -61,3 +35,57 @@ class Item
|
|||||||
"#{@name}, #{@sell_in}, #{@quality}"
|
"#{@name}, #{@sell_in}, #{@quality}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class MyItem
|
||||||
|
def self.update_sell_in(item)
|
||||||
|
item.sell_in = item.sell_in - 1
|
||||||
|
return item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class AgedBrieItem < MyItem
|
||||||
|
def self.update_quality(item)
|
||||||
|
item = update_sell_in(item)
|
||||||
|
item.quality = item.quality + 1 unless item.quality >= 50
|
||||||
|
return item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class BackstagePassItem < MyItem
|
||||||
|
def self.update_quality(item)
|
||||||
|
item = update_sell_in(item)
|
||||||
|
if item.sell_in < 0
|
||||||
|
item.quality = 0
|
||||||
|
elsif item.sell_in < 5
|
||||||
|
item.quality = item.quality + 3
|
||||||
|
elsif item.sell_in < 10
|
||||||
|
item.quality = item.quality + 2
|
||||||
|
else
|
||||||
|
item.quality = item.quality + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ConjuredItem < MyItem
|
||||||
|
def self.update_quality(item)
|
||||||
|
item = update_sell_in(item)
|
||||||
|
if item.sell_in < 0
|
||||||
|
item.quality = item.quality - 4
|
||||||
|
else
|
||||||
|
item.quality = item.quality - 2
|
||||||
|
end
|
||||||
|
item.quality = 0 if item.quality < 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class NormalItem < MyItem
|
||||||
|
def self.update_quality(item)
|
||||||
|
item = update_sell_in(item)
|
||||||
|
if item.sell_in < 0
|
||||||
|
item.quality = item.quality - 2
|
||||||
|
else
|
||||||
|
item.quality = item.quality - 1
|
||||||
|
end
|
||||||
|
item.quality = 0 if item.quality < 0
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user