diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index 3b299af0..1a611f34 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -5,54 +5,12 @@ class GildedRose end def update_quality() - @items.each do |item| - if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert" - if item.quality > 0 - if item.name != "Sulfuras, Hand of Ragnaros" - item.quality = item.quality - 1 - end - end - else - if item.quality < 50 - item.quality = item.quality + 1 - if item.name == "Backstage passes to a TAFKAL80ETC concert" - if item.sell_in < 11 - if item.quality < 50 - item.quality = item.quality + 1 - end - end - if item.sell_in < 6 - if item.quality < 50 - item.quality = item.quality + 1 - end - end - end - end - end - if item.name != "Sulfuras, Hand of Ragnaros" - item.sell_in = 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 = item.quality - 1 - end - end - else - item.quality = item.quality - item.quality - end - else - if item.quality < 50 - item.quality = item.quality + 1 - end - end - end - end + @items.each { |item| ItemUpdater.new(item) } end end + + class Item attr_accessor :name, :sell_in, :quality @@ -66,3 +24,40 @@ class Item "#{@name}, #{@sell_in}, #{@quality}" end end + +class ItemUpdater + attr_accessor :item, :type, :quality_modifier + + def initialize(item) + @item = item + set_attributes + update_quality + end + + def set_attributes + if /.*(^|\b)conjured(\b|$).*/i.match?(@item.name) + @type = 'conjured' + @quality_modifier = -2 + elsif /.*(^|\b)sulfuras(\b|$).*/i.match?(@item.name) + @type = 'legendary' + elsif /.*(^|\b)concert(\b|$).*/i.match?(@item.name) + @type = 'concert_ticket' + @quality_modifier = item.sell_in > 10 ? 1 : item.sell_in > 5 ? 2 : item.sell_in > 0 ? 3 : 1 - Float::INFINITY + elsif /aged brie/i.match?(@item.name) + @type = 'aged brie' + @quality_modifier = 1 + else + @type = 'basic' + @quality_modifier = -1 + end + end + + def update_quality + return if @type == 'legendary' + + @item.sell_in = @item.sell_in - 1 + @item.quality = @item.sell_in < 0 ? @item.quality + (@quality_modifier * 2) : @item.quality + @quality_modifier + @item.quality = 0 if @item.quality.negative? + @item.quality = 50 if @item.quality > 50 + end +end