Skip to content

Instantly share code, notes, and snippets.

@weapp
Last active February 20, 2017 23:27
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 weapp/4d9806e5b54133063f5f31aca9daaaa0 to your computer and use it in GitHub Desktop.
Save weapp/4d9806e5b54133063f5f31aca9daaaa0 to your computer and use it in GitHub Desktop.
GildedRose

based on: https://github.com/jimweirich/gilded_rose_kata

The Gilded Rose Code Kata

This is a Elixir version of the Gilded Rose Kata, found here.

This is a refactorying kata, so you will be starting with a legacy code base. To work the Kata, clone this git repository and checkout the tag 'start-here'. Read the description below for the "rules" involving this kata.

Changes from the original

This Elixir version follows the original code very closely, but has the some changes.

You can read the original kata article for more details.

Hope you enjoy this.

Original Description of the Gilded Rose

Hi and welcome to team Gilded Rose. As you know, we are a small inn with a prime location in a prominent city run by a friendly innkeeper named Allison. We also buy and sell only the finest goods. Unfortunately, our goods are constantly degrading in quality as they approach their sell by date. We have a system in place that updates our inventory for us. It was developed by a no-nonsense type named Leeroy, who has moved on to new adventures. Your task is to add the new feature to our system so that we can begin selling a new category of items. First an introduction to our system:

  • All items have a SellIn value which denotes the number of days we have to sell the item
  • All items have a Quality value which denotes how valuable the item is
  • At the end of each day our system lowers both values for every item

Pretty simple, right? Well this is where it gets interesting:

  • Once the sell by date has passed, Quality degrades twice as fast
  • The Quality of an item is never negative
  • "Aged Brie" actually increases in Quality the older it gets
  • The Quality of an item is never more than 50
  • "Sulfuras", being a legendary item, never has to be sold or decreases in Quality
  • "Backstage passes", like aged brie, increases in Quality as it's SellIn value approaches; Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but Quality drops to 0 after the concert

We have recently signed a supplier of conjured items. This requires an update to our system:

  • "Conjured" items degrade in Quality twice as fast as normal items

Feel free to make any changes to the UpdateQuality method and add any new code as long as everything still works correctly. However, do not alter the Item class or Items property as those belong to the goblin in the corner who will insta-rage and one-shot you as he doesn't believe in shared code ownership (you can make the UpdateQuality method and Items property static if you like, we'll cover for you). Your work needs to be completed by Friday, February 18, 2011 08:00:00 AM PST.

Just for clarification, an item can never have its Quality increase above 50, however "Sulfuras" is a legendary item and as such its Quality is 80 and it never alters.

Spanish Version: https://gonzaloquero.com/blog/2016/08/27/rehaciendo-la-rosa-dorada/

defmodule GildedRose do
def update_quality(items) do
items |> Enum.map(fn item ->
new_value =
if (item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert") do
if (item.quality > 0) do
if (item.name != "Sulfuras, Hand of Ragnaros") do
%{quality: item.quality - 1}
else
%{quality: item.quality}
end
else
%{quality: item.quality}
end
else
if (item.quality < 50) do
q = 1
q2 =
if (item.name == "Backstage passes to a TAFKAL80ETC concert") do
q3 =
if (item.sell_in < 11) do
if ((item.quality + q) < 50) do
1
else
0
end
else
0
end
q4 =
if (item.sell_in < 6) do
if ((item.quality + q + q3)< 50) do
1
else
0
end
else
0
end
q3 + q4
else
0
end
%{quality: item.quality + q + q2}
else
%{quality: item.quality}
end
end
sell_in =
if (item.name != "Sulfuras, Hand of Ragnaros") do
%{sell_in: item.sell_in - 1}
else
%{sell_in: item.sell_in}
end
new_value = Map.merge(new_value, sell_in)
new_value =
if (sell_in[:sell_in] < 0) do
if (item.name != "Aged Brie") do
if (item.name != "Backstage passes to a TAFKAL80ETC concert") do
if (item.quality > 0) do
if (item.name != "Sulfuras, Hand of Ragnaros") do
Map.put(new_value, :quality, new_value[:quality] - 1)
else
new_value
end
else
new_value
end
else
Map.put(new_value, :quality, new_value[:quality] - new_value[:quality])
end
else
if (item.quality < 50) do
Map.put(new_value, :quality, new_value[:quality] + 1)
else
new_value
end
end
else
new_value
end
Map.merge(item, new_value)
end)
end
end
items = [
%{
name: "+5 Dexterity Vest", # Normal Item
sell_in: 10,
quality: 20
},
%{
name: "Aged Brie",
sell_in: 2,
quality: 0
},
%{
name: "Elixir of the Mongoose",
sell_in: 5,
quality: 7
},
%{
name: "Sulfuras, Hand of Ragnaros",
sell_in: 0,
quality: 80
},
%{
name: "Backstage passes to a TAFKAL80ETC concert",
sell_in: 15,
quality: 20
},
%{
name: "Conjured Mana Cake",
sell_in: 3,
quality: 6
}
]
# DO NOT CHANGE THINGS BELOW -----------------------------------------
IO.puts "initial state"
items |> Enum.each(&IO.inspect(&1, width: 180))
IO.puts "first iteration"
items = GildedRose.update_quality(items)
items |> Enum.each(&IO.inspect(&1, width: 180))
IO.puts "second iteration"
items = GildedRose.update_quality(items)
items |> Enum.each(&IO.inspect(&1, width: 180))
IO.puts "third iteration"
items = GildedRose.update_quality(items)
items |> Enum.each(&IO.inspect(&1, width: 180))
IO.puts "fourth iteration"
items = GildedRose.update_quality(items)
items |> Enum.each(&IO.inspect(&1, width: 180))
IO.puts "fifth iteration"
items = GildedRose.update_quality(items)
items |> Enum.each(&IO.inspect(&1, width: 180))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment