From fc8eae7652cc14c7885467dcf96f344ef37d599a Mon Sep 17 00:00:00 2001 From: Feken Baboyan Date: Tue, 23 Feb 2021 22:23:06 -0500 Subject: [PATCH] Start refactoring GildedRose class --- ruby/gilded_rose.rb | 25 +++++++++++++++++++++++++ ruby/item_factory_tests.rb | 16 ++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 ruby/item_factory_tests.rb diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a497..11d061b6 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -53,6 +53,17 @@ class GildedRose end end +class ItemFactory + SPECIAL_ITEMS = ["Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros"] + 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) + end +end + class Item attr_accessor :name, :sell_in, :quality @@ -66,3 +77,17 @@ class Item "#{@name}, #{@sell_in}, #{@quality}" end end + +class GenericItem < Item + def update_quality + if @quality > 0 + @quality -= 1 + end + end + + def update_sell_in + @sell_in -= 1 + end +end + + diff --git a/ruby/item_factory_tests.rb b/ruby/item_factory_tests.rb new file mode 100644 index 00000000..87eadbc2 --- /dev/null +++ b/ruby/item_factory_tests.rb @@ -0,0 +1,16 @@ +require File.join(File.dirname(__FILE__), 'gilded_rose') +require 'test/unit' + +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 + created_item = ItemFactory.create_item(name: "Aged Brie", sell_in: 0, quality: 0) + + assert_instance_of Item, created_item + end +end \ No newline at end of file