Permalink
Browse files

Here's my solution to the kata.

  • Loading branch information...
1 parent b1e1d3a commit 28eae6977e8cde373a6d6ca35f1fc80fc92a3f6a @JEG2 committed Dec 8, 2011
Showing with 75 additions and 44 deletions.
  1. +75 −43 gilded_rose.rb
  2. +0 −1 gilded_rose_spec.rb
View
118 gilded_rose.rb
@@ -1,51 +1,83 @@
-def update_quality(items)
- items.each do |item|
- if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert'
- if item.quality > 0
- if item.name != 'Sulfuras, Hand of Ragnaros'
- item.quality -= 1
- end
- end
- else
- if item.quality < 50
- item.quality += 1
- if item.name == 'Backstage passes to a TAFKAL80ETC concert'
- if item.sell_in < 11
- if item.quality < 50
- item.quality += 1
- end
- end
- if item.sell_in < 6
- if item.quality < 50
- item.quality += 1
- end
- end
- end
- end
- end
- if item.name != 'Sulfuras, Hand of Ragnaros'
- item.sell_in -= 1
- end
- if item.sell_in < 0
- if item.name != "Aged Brie"
- if item.name != 'Backstage passes to a TAFKAL80ETC concert'
- if item.quality > 0
- if item.name != 'Sulfuras, Hand of Ragnaros'
- item.quality -= 1
- end
- end
- else
- item.quality = item.quality - item.quality
- end
- else
- if item.quality < 50
- item.quality += 1
- end
+module Aging
+ def age
+ update_quality
+ update_sell_in
+ end
+
+ def update_sell_in
+ self.sell_in -= 1
+ end
+
+ def update_quality
+ reduce_quality(sell_in <= 0 ? 2 : 1)
+ end
+
+ def reduce_quality(amount)
+ self.quality = [quality - amount, 0].max
+ end
+
+ def increase_quality(amount)
+ self.quality = [quality + amount, 50].min
+ end
+end
+
+module BetterWithAge
+ def update_quality
+ increase_quality(sell_in <= 0 ? 2 : 1)
+ end
+end
+
+module Popular
+ def update_quality
+ increase_quality(
+ case sell_in
+ when -Float::INFINITY..0 then -quality
+ when 1..5 then 3
+ when 6..10 then 2
+ else 1
end
+ )
+ end
+end
+
+module Legendary
+ def update_sell_in
+ # do nothing: never has to be sold
+ end
+
+ def update_quality
+ # do nothing: does not degrade
+ end
+end
+
+module Conjured
+ def update_quality
+ 2.times do
+ super
end
end
end
+def prepare_for_aging(item)
+ unless item.respond_to? :age
+ item.extend(Aging)
+ type = case item.name
+ when /Aged Brie/i then BetterWithAge
+ when /Backstage pass/i then Popular
+ when /Sulfuras/i then Legendary
+ when /Conjured/i then Conjured
+ end
+ item.extend(type) if type
+ end
+ item
+end
+
+def update_quality(items)
+ items.each do |item|
+ prepare_for_aging(item).age
+ end
+end
+
# DO NOT CHANGE THINGS BELOW -----------------------------------------
Item = Struct.new(:name, :sell_in, :quality)
View
1 gilded_rose_spec.rb
@@ -169,7 +169,6 @@
end
context "conjured item" do
- before { pending }
Given(:name) { "Conjured Mana Cake" }
context "before the sell date" do

0 comments on commit 28eae69

Please sign in to comment.