Skip to content

Instantly share code, notes, and snippets.

@depy
Created January 24, 2016 10:17
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 depy/9062161a450abde52d32 to your computer and use it in GitHub Desktop.
Save depy/9062161a450abde52d32 to your computer and use it in GitHub Desktop.
require 'forwardable'
Item = Struct.new(:name, :sell_in, :quality)
class NormalAging
def self.age(item)
item.quality -= 1 if item.sell_in > 0
item.quality -= 2 if item.sell_in <= 0
item.quality = 0 if item.quality < 0
item.quality = 50 if item.quality > 50
item.sell_in -= 1
end
end
class BrieAging
def self.age(item)
item.quality += 1 if item.sell_in > 0
item.quality += 2 if item.sell_in <= 0
item.quality = 0 if item.quality < 0
item.quality = 50 if item.quality > 50
item.sell_in -= 1
end
end
class TicketAging
def self.age(item)
item.quality += 1 if item.sell_in > 10
item.quality += 2 if item.sell_in > 5 && item.sell_in <= 10
item.quality += 3 if item.sell_in > 0 && item.sell_in <= 5
item.quality = 0 if item.sell_in <= 0 || item.quality < 0
item.quality = 50 if item.quality > 50
item.sell_in -= 1
end
end
class ManaCakeAging
def self.age(item)
item.quality -= 2 if item.sell_in <= 5 && item.sell_in > 0
item.quality -= 4 if item.sell_in <= 0
item.quality = 0 if item.quality < 0
item.quality = 50 if item.quality > 50
item.sell_in -= 1
end
end
class NoAging
def self.age(item)
end
end
class AgingItem
attr_reader :item, :aging_strategy
extend Forwardable
def_delegators :@item, :name, :name=, :sell_in, :sell_in=, :quality, :quality=
def initialize(item, aging_strategy)
@item = item
@aging_strategy = aging_strategy
end
def age
aging_strategy.age(item)
end
end
class GildedRose
AGING_STRATEGIES = {
'NORMAL ITEM' => NormalAging,
'Aged Brie' => BrieAging,
'Backstage passes to a TAFKAL80ETC concert' => TicketAging,
'Sulfuras, Hand of Ragnaros' => NoAging,
'Conjured Mana Cake' => ManaCakeAging
}
def self.update_quality(items)
items.each do |item|
aging_strategy = AGING_STRATEGIES[item.name]
AgingItem.new(item, aging_strategy).age
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment