diff --git a/ruby/lib/gilded_rose/item_factory.rb b/ruby/lib/gilded_rose/item_factory.rb index fa3d567e..c6747988 100644 --- a/ruby/lib/gilded_rose/item_factory.rb +++ b/ruby/lib/gilded_rose/item_factory.rb @@ -1,12 +1,16 @@ module GildedRose 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:) - if SPECIAL_ITEMS.include?(name) - return Item.new(name, sell_in, quality) - end - - GenericItem.new(name, sell_in, quality) + klass = SPECIAL_ITEM_KLASSES.fetch(name, GENERIC_ITEM_KLASS) + klass.new(name, sell_in, quality) end end end diff --git a/ruby/tests/gilded_rose/item_factory_tests.rb b/ruby/tests/gilded_rose/item_factory_tests.rb index 0437544a..05022f1f 100644 --- a/ruby/tests/gilded_rose/item_factory_tests.rb +++ b/ruby/tests/gilded_rose/item_factory_tests.rb @@ -5,14 +5,22 @@ module GildedRose class ItemFactoryTests < Test::Unit::TestCase 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) - assert_instance_of GenericItem, created_item 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) - - assert_instance_of Item, created_item + assert_instance_of AgedBrieItem, 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