Skip to content

Instantly share code, notes, and snippets.

@lojic
Last active August 29, 2015 14:16
Show Gist options
  • Save lojic/eec2c68c30c82576fedf to your computer and use it in GitHub Desktop.
Save lojic/eec2c68c30c82576fedf to your computer and use it in GitHub Desktop.
Functional version of gilded rose kata. See https://github.com/jimweirich/gilded_rose_kata
module ItemAging
AGING_FUNCTIONS = {
'NORMAL ITEM' => ->(s,q) { normal(s,q) },
'Aged Brie' => ->(s,q) { brie(s,q) },
'Backstage passes to a TAFKAL80ETC concert' => ->(s,q) { backstage(s,q) },
'Conjured Mana Cake' => ->(s,q) { conjured(s,q) },
}
# A convenience function. The *only* function in the module that
# mutates non-local state.
def self.age_item item
item.quality, item.sell_in =
aging_func(item).call(item.sell_in, item.quality)
end
def self.aging_func item, funcs=AGING_FUNCTIONS
funcs[item.name] || ->(s,q) { [ q, s ] }
end
# -------------------------------------------------------------------------------------------
def self.backstage sell_in, quality
if quality < 50
if sell_in < 1
quality = 0
else
quality += 1
quality += 1 if sell_in < 11
quality += 1 if sell_in < 6
end
end
[ quality, sell_in - 1]
end
def self.brie sell_in, quality
if quality < 50
quality += 1
quality += 1 if sell_in <= 1 && quality < 50
end
[ quality, sell_in - 1 ]
end
def self.conjured sell_in, quality
if quality != 0
quality -= 2
quality -= 2 if sell_in <= 1
end
[ quality, sell_in - 1]
end
def self.normal sell_in, quality
if quality != 0
quality -= 1
quality -= 1 if sell_in <= 1
end
[ quality, sell_in - 1 ]
end
end
def update_quality(items)
items.each {|item| ItemAging.age_item(item) }
end
Item = Struct.new(:name, :sell_in, :quality)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment