Last active
July 17, 2018 21:37
-
-
Save jordanfowler/c767811f5bc34f3b97162b51728c59f8 to your computer and use it in GitHub Desktop.
Gilded Rose Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Delegates | |
class ConjuredManaCake < DefaultItem | |
def tick | |
unless quality == 0 | |
if days_remaining > 0 | |
@quality -= 2 | |
else | |
@quality -= 4 | |
end | |
end | |
@days_remaining -= 1 | |
end | |
end | |
end | |
Delegates.register('Conjured Mana Cake', Delegates::ConjuredManaCake) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Delegates | |
class DefaultItem | |
attr_reader :name, :days_remaining, :quality, :tick | |
def initialize(name:, days_remaining:, quality:) | |
@name = name | |
@days_remaining = days_remaining | |
@quality = quality | |
end | |
def tick | |
if @name != 'Aged Brie' && @name != 'Backstage passes to a TAFKAL80ETC concert' | |
if @quality > 0 | |
if @name != 'Sulfuras, Hand of Ragnaros' | |
@quality = @quality - 1 | |
end | |
end | |
else | |
if @quality < 50 | |
@quality = @quality + 1 | |
if @name == 'Backstage passes to a TAFKAL80ETC concert' | |
if @days_remaining < 11 | |
if @quality < 50 | |
@quality = @quality + 1 | |
end | |
end | |
if @days_remaining < 6 | |
if @quality < 50 | |
@quality = @quality + 1 | |
end | |
end | |
end | |
end | |
end | |
if @name != 'Sulfuras, Hand of Ragnaros' | |
@days_remaining = @days_remaining - 1 | |
end | |
if @days_remaining < 0 | |
if @name != 'Aged Brie' | |
if @name != 'Backstage passes to a TAFKAL80ETC concert' | |
if @quality > 0 | |
if @name != 'Sulfuras, Hand of Ragnaros' | |
@quality = @quality - 1 | |
end | |
end | |
else | |
@quality = @quality - @quality | |
end | |
else | |
if @quality < 50 | |
@quality = @quality + 1 | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dir[File.expand_path('delegates/*.rb', File.dirname(__FILE__))].each {|file| require_relative file } | |
module Delegates | |
@@delegates = Hash.new | |
def self.get(name) | |
@@delegates[name] || DefaultItem | |
end | |
def self.register(name, item_class) | |
@@delegates[name] = item_class | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'forwardable' | |
require_relative 'delegates' | |
class GildedRose | |
extend Forwardable | |
def_delegators :@delegate, :name, :days_remaining, :quality, :tick | |
def initialize(name:, days_remaining:, quality:) | |
@delegate = Delegates.get(name).new(name: name, days_remaining: days_remaining, quality: quality) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment