Skip to content

Instantly share code, notes, and snippets.

@jonoliver
Created August 22, 2016 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonoliver/c3589979ddc1de6104a194cdd77fcb28 to your computer and use it in GitHub Desktop.
Save jonoliver/c3589979ddc1de6104a194cdd77fcb28 to your computer and use it in GitHub Desktop.
Gilded Rose Kata
require_relative 'item'
class GildedRose
def initialize items
@items = items
end
def update_quality
@items.each{ |item| update_item item }
end
private
def update_item item
update_strategy = UpdateStrategyFactory.get_strategy_for(item)
update_strategy.update_item()
end
end
class DefaultUpdateStrategy
def initialize item
@item = item
end
def update_item
update_sell_in
update_quality
end
private
def update_sell_in
@item.sell_in -= 1
end
def update_quality
return if @item.quality <= 0
multiplier = sell_date_passed? ? 2 : 1
@item.quality -= multiplier
end
def sell_date_passed?
@item.sell_in < 0
end
end
class AgedBrieUpdateStrategy < DefaultUpdateStrategy
private
def update_quality
return if @item.quality >= 50
multiplier = sell_date_passed? ? 2 : 1
@item.quality += multiplier
end
end
class SulfurasUpdateStrategy < DefaultUpdateStrategy
private
def update_sell_in
end
end
class BackstagePassUpdateStrategy < DefaultUpdateStrategy
private
def update_quality
@item.quality = 0 and return if @item.sell_in < 0
increment_item_quality if @item.sell_in < 10
increment_item_quality if @item.sell_in < 5
increment_item_quality
end
def increment_item_quality
return if @item.quality >= 50
@item.quality += 1
end
end
class ConjuredManaCakeUpdateStrategy < DefaultUpdateStrategy
private
def update_quality
@item.quality -= 2 unless @item.quality <= 0
end
end
class UpdateStrategyFactory
MAP = {
"Aged Brie" => AgedBrieUpdateStrategy,
"Sulfuras, Hand of Ragnaros" => SulfurasUpdateStrategy,
"Backstage passes to a TAFKAL80ETC concert" => BackstagePassUpdateStrategy,
"Conjured Mana Cake" => ConjuredManaCakeUpdateStrategy,
}
def self.get_strategy_for item
strategy = MAP[item.name] || DefaultUpdateStrategy
strategy.new(item)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment