mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Move items to their own classes
This commit is contained in:
parent
815ba7ce24
commit
fbae17fd32
@ -1,3 +1,9 @@
|
||||
require_relative 'items/backstage_pass_item'
|
||||
require_relative 'items/brie_item'
|
||||
require_relative 'items/conjured_item'
|
||||
require_relative 'items/normal_item'
|
||||
require_relative 'items/sulfura_item'
|
||||
|
||||
class GildedRose
|
||||
QUALITY_LOWER_LIMIT = 0
|
||||
QUALITY_UPPER_LIMIT = 50
|
||||
@ -10,80 +16,18 @@ class GildedRose
|
||||
@items.each do |item|
|
||||
case item.name
|
||||
when 'Aged Brie'
|
||||
update_aged_brie_quality(item)
|
||||
BrieItem.new(item).spend_day_in_shop
|
||||
when 'Backstage passes to a TAFKAL80ETC concert'
|
||||
update_backstage_passes_quality(item)
|
||||
BackstagePassItem.new(item).spend_day_in_shop
|
||||
when 'Conjured Mana Cake'
|
||||
update_conjured_quality(item)
|
||||
ConjuredItem.new(item).spend_day_in_shop
|
||||
when 'Sulfuras, Hand of Ragnaros'
|
||||
update_sulfuras_quality(item)
|
||||
SulfuraItem.new(item).spend_day_in_shop
|
||||
else
|
||||
update_normal_quality(item)
|
||||
NormalItem.new(item).spend_day_in_shop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update_aged_brie_quality(item)
|
||||
decrease_sell_in_day(item)
|
||||
|
||||
increase_item_quality(item)
|
||||
increase_item_quality(item) if item.sell_in.negative?
|
||||
|
||||
keep_quality_upper_limit_in_bounds(item)
|
||||
end
|
||||
|
||||
def update_backstage_passes_quality(item)
|
||||
decrease_sell_in_day(item)
|
||||
return item.quality = 0 if item.sell_in.negative?
|
||||
|
||||
increase_item_quality(item)
|
||||
increase_item_quality(item) if item.sell_in < 10
|
||||
increase_item_quality(item) if item.sell_in < 5
|
||||
|
||||
keep_quality_upper_limit_in_bounds(item)
|
||||
end
|
||||
|
||||
def update_conjured_quality(item)
|
||||
decrease_sell_in_day(item)
|
||||
|
||||
decrease_item_quality(item, 2)
|
||||
decrease_item_quality(item, 2) if item.sell_in.negative?
|
||||
|
||||
keep_quality_lower_limit_in_bounds(item)
|
||||
end
|
||||
|
||||
def update_normal_quality(item)
|
||||
decrease_sell_in_day(item)
|
||||
|
||||
decrease_item_quality(item)
|
||||
decrease_item_quality(item) if item.sell_in.negative?
|
||||
|
||||
keep_quality_lower_limit_in_bounds(item)
|
||||
end
|
||||
|
||||
def update_sulfuras_quality(item); end
|
||||
|
||||
private
|
||||
|
||||
def decrease_sell_in_day(item)
|
||||
item.sell_in -= 1
|
||||
end
|
||||
|
||||
def increase_item_quality(item)
|
||||
item.quality += 1
|
||||
end
|
||||
|
||||
def decrease_item_quality(item, by = 1)
|
||||
item.quality -= by
|
||||
end
|
||||
|
||||
def keep_quality_upper_limit_in_bounds(item)
|
||||
item.quality = QUALITY_UPPER_LIMIT if item.quality > QUALITY_UPPER_LIMIT
|
||||
end
|
||||
|
||||
def keep_quality_lower_limit_in_bounds(item)
|
||||
item.quality = QUALITY_LOWER_LIMIT if item.quality < QUALITY_LOWER_LIMIT
|
||||
end
|
||||
end
|
||||
|
||||
class Item
|
||||
|
||||
16
ruby/items/backstage_pass_item.rb
Normal file
16
ruby/items/backstage_pass_item.rb
Normal file
@ -0,0 +1,16 @@
|
||||
require_relative 'normal_item'
|
||||
|
||||
class BackstagePassItem < NormalItem
|
||||
def spend_day_in_shop
|
||||
decrease_sell_in_day
|
||||
return item.quality = 0 if item.sell_in.negative?
|
||||
|
||||
increase_quality
|
||||
increase_quality if item.sell_in < 10
|
||||
increase_quality if item.sell_in < 5
|
||||
|
||||
keep_quality_upper_limit_in_bounds
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
14
ruby/items/brie_item.rb
Normal file
14
ruby/items/brie_item.rb
Normal file
@ -0,0 +1,14 @@
|
||||
require_relative 'normal_item'
|
||||
|
||||
class BrieItem < NormalItem
|
||||
def spend_day_in_shop
|
||||
decrease_sell_in_day
|
||||
|
||||
increase_quality
|
||||
increase_quality if item.sell_in.negative?
|
||||
|
||||
keep_quality_upper_limit_in_bounds
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
19
ruby/items/conjured_item.rb
Normal file
19
ruby/items/conjured_item.rb
Normal file
@ -0,0 +1,19 @@
|
||||
require_relative 'normal_item'
|
||||
|
||||
class ConjuredItem< NormalItem
|
||||
def spend_day_in_shop
|
||||
decrease_sell_in_day
|
||||
|
||||
decrease_quality
|
||||
decrease_quality
|
||||
|
||||
if item.sell_in.negative?
|
||||
decrease_quality
|
||||
decrease_quality
|
||||
end
|
||||
|
||||
keep_quality_lower_limit_in_bounds
|
||||
|
||||
self
|
||||
end
|
||||
end
|
||||
41
ruby/items/normal_item.rb
Normal file
41
ruby/items/normal_item.rb
Normal file
@ -0,0 +1,41 @@
|
||||
class NormalItem
|
||||
attr_reader :item
|
||||
|
||||
def initialize(item)
|
||||
@item = item
|
||||
end
|
||||
|
||||
def spend_day_in_shop
|
||||
decrease_sell_in_day
|
||||
|
||||
decrease_quality
|
||||
decrease_quality if item.sell_in.negative?
|
||||
|
||||
keep_quality_lower_limit_in_bounds
|
||||
keep_quality_upper_limit_in_bounds
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def decrease_sell_in_day
|
||||
item.sell_in -= 1
|
||||
end
|
||||
|
||||
def decrease_quality
|
||||
item.quality -= 1
|
||||
end
|
||||
|
||||
def increase_quality
|
||||
item.quality += 1
|
||||
end
|
||||
|
||||
def keep_quality_lower_limit_in_bounds
|
||||
item.quality = GildedRose::QUALITY_LOWER_LIMIT if item.quality < GildedRose::QUALITY_LOWER_LIMIT
|
||||
end
|
||||
|
||||
def keep_quality_upper_limit_in_bounds
|
||||
item.quality = GildedRose::QUALITY_UPPER_LIMIT if item.quality > GildedRose::QUALITY_UPPER_LIMIT
|
||||
end
|
||||
end
|
||||
5
ruby/items/sulfura_item.rb
Normal file
5
ruby/items/sulfura_item.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require_relative 'normal_item'
|
||||
|
||||
class SulfuraItem < NormalItem
|
||||
def spend_day_in_shop; end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user