mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-19 08:21:37 +00:00
Switch ItemFactory to ItemWrapperFactory
This commit is contained in:
parent
abd03d24c6
commit
899821440c
@ -1,5 +1,5 @@
|
||||
require File.join(File.dirname(__FILE__), "gilded_rose/store" )
|
||||
require File.join(File.dirname(__FILE__), "gilded_rose/item_factory" )
|
||||
require File.join(File.dirname(__FILE__), "gilded_rose/item_wrapper_factory" )
|
||||
|
||||
module GildedRose
|
||||
end
|
||||
@ -1,17 +0,0 @@
|
||||
module GildedRose
|
||||
class ItemFactory
|
||||
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:)
|
||||
klass = SPECIAL_ITEM_KLASSES.fetch(name, GENERIC_ITEM_KLASS)
|
||||
klass.new(name, sell_in, quality)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
18
ruby/lib/gilded_rose/item_wrapper_factory.rb
Normal file
18
ruby/lib/gilded_rose/item_wrapper_factory.rb
Normal file
@ -0,0 +1,18 @@
|
||||
module GildedRose
|
||||
class ItemWrapperFactory
|
||||
|
||||
SPECIAL_ITEM_WRAPPER_KLASSES = {
|
||||
"Aged Brie" => AgedBrieItemWrapper,
|
||||
"Backstage passes to a TAFKAL80ETC concert" => BackstagePassesItemWrapper,
|
||||
"Sulfuras, Hand of Ragnaros" => SulfurasItemWrapper,
|
||||
}
|
||||
|
||||
GENERIC_ITEM_WRAPPER_KLASS = GenericItemWrapper
|
||||
|
||||
def self.wrap(item: )
|
||||
klass = SPECIAL_ITEM_WRAPPER_KLASSES.fetch(item.name, GENERIC_ITEM_WRAPPER_KLASS)
|
||||
klass.new(item: item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -55,7 +55,7 @@ module GildedRose
|
||||
|
||||
def items
|
||||
@items ||= @raw_items.map do |item|
|
||||
ItemFactory.create_item(name: item.name, sell_in: item.sell_in, quality: item.quality)
|
||||
ItemWrapperFactory.wrap(item: item)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -74,52 +74,32 @@ module GildedRose
|
||||
end
|
||||
end
|
||||
|
||||
class GenericItem < Item
|
||||
class AbstractItemWrapper
|
||||
def initialize(item: )
|
||||
@item = item
|
||||
end
|
||||
|
||||
def update_quality
|
||||
if @quality > 0
|
||||
@quality -= 1
|
||||
if item.quality > 0
|
||||
item.quality -= 1
|
||||
end
|
||||
end
|
||||
|
||||
def update_sell_in
|
||||
@sell_in -= 1
|
||||
item.sell_in -= 1
|
||||
end
|
||||
|
||||
def method_missing(method_name, *args)
|
||||
item.send(method_name, *args)
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :item
|
||||
end
|
||||
|
||||
class AgedBrieItem < Item
|
||||
def update_quality
|
||||
if @quality > 0
|
||||
@quality -= 1
|
||||
end
|
||||
end
|
||||
|
||||
def update_sell_in
|
||||
@sell_in -= 1
|
||||
end
|
||||
end
|
||||
|
||||
class BackstagePassesItem < Item
|
||||
def update_quality
|
||||
if @quality > 0
|
||||
@quality -= 1
|
||||
end
|
||||
end
|
||||
|
||||
def update_sell_in
|
||||
@sell_in -= 1
|
||||
end
|
||||
end
|
||||
|
||||
class SulfurasItem < Item
|
||||
def update_quality
|
||||
if @quality > 0
|
||||
@quality -= 1
|
||||
end
|
||||
end
|
||||
|
||||
def update_sell_in
|
||||
@sell_in -= 1
|
||||
end
|
||||
end
|
||||
class GenericItemWrapper < AbstractItemWrapper; end
|
||||
class AgedBrieItemWrapper < AbstractItemWrapper; end
|
||||
class BackstagePassesItemWrapper < AbstractItemWrapper; end
|
||||
class SulfurasItemWrapper < AbstractItemWrapper; end
|
||||
end
|
||||
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
require_relative '../../lib/gilded_rose'
|
||||
require 'test/unit'
|
||||
|
||||
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 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 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
|
||||
|
||||
42
ruby/tests/gilded_rose/item_wrapper_factory_tests.rb
Normal file
42
ruby/tests/gilded_rose/item_wrapper_factory_tests.rb
Normal file
@ -0,0 +1,42 @@
|
||||
require_relative '../../lib/gilded_rose'
|
||||
require 'test/unit'
|
||||
|
||||
module GildedRose
|
||||
class ItemWrapperFactoryTests < Test::Unit::TestCase
|
||||
test ".create_item returns GenericItem instance if it is not a special item" do
|
||||
item = make_item(name: "random_item", sell_in: 0, quality: 0)
|
||||
|
||||
created_item = ItemWrapperFactory.wrap(item: item)
|
||||
assert_instance_of GenericItemWrapper, created_item
|
||||
end
|
||||
|
||||
test ".create_item returns AgedBrieItem instance if Aged Brie item is given" do
|
||||
item = make_item(name: "Aged Brie", sell_in: 0, quality: 0)
|
||||
|
||||
created_item = ItemWrapperFactory.wrap(item: item)
|
||||
assert_instance_of AgedBrieItemWrapper, created_item
|
||||
end
|
||||
|
||||
test ".create_item returns BackstagePassesItem instance if Backstage passes to a TAFKAL80ETC concert item is given" do
|
||||
item = make_item(name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 0, quality: 0)
|
||||
|
||||
created_item = ItemWrapperFactory.wrap(item: item)
|
||||
assert_instance_of BackstagePassesItemWrapper, created_item
|
||||
end
|
||||
|
||||
test ".create_item returns SulfurasItem instance if Sulfuras, Hand of Ragnaros item is given" do
|
||||
item = make_item(name: "Sulfuras, Hand of Ragnaros", sell_in: 0, quality: 0)
|
||||
|
||||
created_item = ItemWrapperFactory.wrap(item: item)
|
||||
assert_instance_of SulfurasItemWrapper, created_item
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def make_item(name:, sell_in:, quality:)
|
||||
Item.new(name, sell_in, quality)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user