Update ItemFactory to use the new SpecialItem classes

This commit is contained in:
Feken Baboyan 2021-03-03 22:15:48 -05:00
parent cb68b26059
commit abd03d24c6
2 changed files with 23 additions and 11 deletions

View File

@ -1,12 +1,16 @@
module GildedRose module GildedRose
class ItemFactory class ItemFactory
SPECIAL_ITEMS = ["Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros"] SPECIAL_ITEM_KLASSES = {
"Aged Brie" => AgedBrieItem,
"Backstage passes to a TAFKAL80ETC concert" => BackstagePassesItem,
"Sulfuras, Hand of Ragnaros" => SulfurasItem,
}
GENERIC_ITEM_KLASS = GenericItem
def self.create_item(name:, sell_in:, quality:) def self.create_item(name:, sell_in:, quality:)
if SPECIAL_ITEMS.include?(name) klass = SPECIAL_ITEM_KLASSES.fetch(name, GENERIC_ITEM_KLASS)
return Item.new(name, sell_in, quality) klass.new(name, sell_in, quality)
end
GenericItem.new(name, sell_in, quality)
end end
end end
end end

View File

@ -5,14 +5,22 @@ module GildedRose
class ItemFactoryTests < Test::Unit::TestCase class ItemFactoryTests < Test::Unit::TestCase
test ".create_item returns GenericItem instance if it is not a special item" do test ".create_item returns GenericItem instance if it is not a special item" do
created_item = ItemFactory.create_item(name: "random_item", sell_in: 0, quality: 0) created_item = ItemFactory.create_item(name: "random_item", sell_in: 0, quality: 0)
assert_instance_of GenericItem, created_item assert_instance_of GenericItem, created_item
end end
test ".create_item returns Item instance if Aged Brie item if given" do test ".create_item returns AgedBrieItem instance if Aged Brie item is given" do
created_item = ItemFactory.create_item(name: "Aged Brie", sell_in: 0, quality: 0) created_item = ItemFactory.create_item(name: "Aged Brie", sell_in: 0, quality: 0)
assert_instance_of AgedBrieItem, created_item
assert_instance_of Item, created_item end
test ".create_item returns BackstagePassesItem instance if Backstage passes to a TAFKAL80ETC concert item is given" do
created_item = ItemFactory.create_item(name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 0, quality: 0)
assert_instance_of BackstagePassesItem, created_item
end
test ".create_item returns SulfurasItem instance if Sulfuras, Hand of Ragnaros item is given" do
created_item = ItemFactory.create_item(name: "Sulfuras, Hand of Ragnaros", sell_in: 0, quality: 0)
assert_instance_of SulfurasItem, created_item
end end
end end
end end