Skip to content

Instantly share code, notes, and snippets.

@simonkro
Last active August 29, 2015 14:00
Show Gist options
  • Save simonkro/11287981 to your computer and use it in GitHub Desktop.
Save simonkro/11287981 to your computer and use it in GitHub Desktop.
gilded_rose_with_config
Normal:
:change: -1
:decay: [0]
Sulfuras, Hand of Ragnaros:
:eternal: true
Aged Brie:
:change: +1
:decay: [0]
Backstage passes to a TAFKAL80ETC concert:
:change: +1
:decay: [10, 5]
:expires: true
Conjured Mana Cake:
:change: -2
:decay: [1]
require 'yaml'
class GildedRose < Struct.new(:name, :days_remaining, :quality)
def tick(item = config[name] || config['Normal'])
return quality if item[:eternal]
self.days_remaining -= 1
self.quality = new_quality(item[:decay], item[:change], item[:expires])
end
protected
def new_quality(decay, change, expires)
return 0 if expires and days_remaining < 0
[[0, quality + decays(decay) * change].max, 50].min
end
def decays(decay)
1 + decay.count { |days| days_remaining < days }
end
def config
@config ||= YAML.load_file("#{__dir__}/config.yml")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment