mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
Start refactoring GildedRose class
This commit is contained in:
parent
687f36e40b
commit
fc8eae7652
@ -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
|
||||
|
||||
|
||||
|
||||
16
ruby/item_factory_tests.rb
Normal file
16
ruby/item_factory_tests.rb
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user