Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Here's my solution to the kata.

  • Loading branch information...
commit 28eae6977e8cde373a6d6ca35f1fc80fc92a3f6a 1 parent b1e1d3a
James Edward Gray II authored

Showing 2 changed files with 75 additions and 44 deletions. Show diff stats Hide diff stats

  1. +75 43 gilded_rose.rb
  2. +0 1  gilded_rose_spec.rb
118 gilded_rose.rb
... ... @@ -1,51 +1,83 @@
1   -def update_quality(items)
2   - items.each do |item|
3   - if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert'
4   - if item.quality > 0
5   - if item.name != 'Sulfuras, Hand of Ragnaros'
6   - item.quality -= 1
7   - end
8   - end
9   - else
10   - if item.quality < 50
11   - item.quality += 1
12   - if item.name == 'Backstage passes to a TAFKAL80ETC concert'
13   - if item.sell_in < 11
14   - if item.quality < 50
15   - item.quality += 1
16   - end
17   - end
18   - if item.sell_in < 6
19   - if item.quality < 50
20   - item.quality += 1
21   - end
22   - end
23   - end
24   - end
25   - end
26   - if item.name != 'Sulfuras, Hand of Ragnaros'
27   - item.sell_in -= 1
28   - end
29   - if item.sell_in < 0
30   - if item.name != "Aged Brie"
31   - if item.name != 'Backstage passes to a TAFKAL80ETC concert'
32   - if item.quality > 0
33   - if item.name != 'Sulfuras, Hand of Ragnaros'
34   - item.quality -= 1
35   - end
36   - end
37   - else
38   - item.quality = item.quality - item.quality
39   - end
40   - else
41   - if item.quality < 50
42   - item.quality += 1
43   - end
  1 +module Aging
  2 + def age
  3 + update_quality
  4 + update_sell_in
  5 + end
  6 +
  7 + def update_sell_in
  8 + self.sell_in -= 1
  9 + end
  10 +
  11 + def update_quality
  12 + reduce_quality(sell_in <= 0 ? 2 : 1)
  13 + end
  14 +
  15 + def reduce_quality(amount)
  16 + self.quality = [quality - amount, 0].max
  17 + end
  18 +
  19 + def increase_quality(amount)
  20 + self.quality = [quality + amount, 50].min
  21 + end
  22 +end
  23 +
  24 +module BetterWithAge
  25 + def update_quality
  26 + increase_quality(sell_in <= 0 ? 2 : 1)
  27 + end
  28 +end
  29 +
  30 +module Popular
  31 + def update_quality
  32 + increase_quality(
  33 + case sell_in
  34 + when -Float::INFINITY..0 then -quality
  35 + when 1..5 then 3
  36 + when 6..10 then 2
  37 + else 1
44 38 end
  39 + )
  40 + end
  41 +end
  42 +
  43 +module Legendary
  44 + def update_sell_in
  45 + # do nothing: never has to be sold
  46 + end
  47 +
  48 + def update_quality
  49 + # do nothing: does not degrade
  50 + end
  51 +end
  52 +
  53 +module Conjured
  54 + def update_quality
  55 + 2.times do
  56 + super
45 57 end
46 58 end
47 59 end
48 60
  61 +def prepare_for_aging(item)
  62 + unless item.respond_to? :age
  63 + item.extend(Aging)
  64 + type = case item.name
  65 + when /Aged Brie/i then BetterWithAge
  66 + when /Backstage pass/i then Popular
  67 + when /Sulfuras/i then Legendary
  68 + when /Conjured/i then Conjured
  69 + end
  70 + item.extend(type) if type
  71 + end
  72 + item
  73 +end
  74 +
  75 +def update_quality(items)
  76 + items.each do |item|
  77 + prepare_for_aging(item).age
  78 + end
  79 +end
  80 +
49 81 # DO NOT CHANGE THINGS BELOW -----------------------------------------
50 82
51 83 Item = Struct.new(:name, :sell_in, :quality)
1  gilded_rose_spec.rb
@@ -169,7 +169,6 @@
169 169 end
170 170
171 171 context "conjured item" do
172   - before { pending }
173 172 Given(:name) { "Conjured Mana Cake" }
174 173
175 174 context "before the sell date" do

0 comments on commit 28eae69

Please sign in to comment.
Something went wrong with that request. Please try again.