Introduce ItemFactory in GildedRose::Store class

This commit is contained in:
Feken Baboyan 2021-03-03 22:14:32 -05:00
parent dc9e8be57d
commit f196a3ab9b

View File

@ -1,12 +1,12 @@
module GildedRose module GildedRose
class Store class Store
def initialize(items) def initialize(raw_items)
@items = items @raw_items = raw_items
end end
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" if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
if item.quality > 0 if item.quality > 0
if item.name != "Sulfuras, Hand of Ragnaros" if item.name != "Sulfuras, Hand of Ragnaros"
@ -52,32 +52,38 @@ module GildedRose
end end
end end
end end
def items
@items ||= @raw_items.map do |item|
ItemFactory.create_item(name: item.name, sell_in: item.sell_in, quality: item.quality)
end
end
end end
class Item class Item
attr_accessor :name, :sell_in, :quality attr_accessor :name, :sell_in, :quality
def initialize(name, sell_in, quality) def initialize(name, sell_in, quality)
@name = name @name = name
@sell_in = sell_in @sell_in = sell_in
@quality = quality @quality = quality
end end
def to_s() def to_s()
"#{@name}, #{@sell_in}, #{@quality}" "#{@name}, #{@sell_in}, #{@quality}"
end end
end end
class GenericItem < Item class GenericItem < Item
def update_quality def update_quality
if @quality > 0 if @quality > 0
@quality -= 1 @quality -= 1
end end
end end
def update_sell_in def update_sell_in
@sell_in -= 1 @sell_in -= 1
end end
end end
end end