diff --git a/ruby/Dockerfile b/ruby/Dockerfile new file mode 100644 index 00000000..2af14cca --- /dev/null +++ b/ruby/Dockerfile @@ -0,0 +1,10 @@ +FROM ruby:2.6 + +RUN mkdir -p /app +WORKDIR /app + +RUN gem install bundler:2.1.4 +COPY Gemfile Gemfile.lock /app/ +RUN bundle install + +COPY . /app \ No newline at end of file diff --git a/ruby/Gemfile b/ruby/Gemfile index 199df8f6..764076a3 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -1,3 +1,7 @@ -source "https://rubygems.org" +# frozen_string_literal: true + +source 'https://rubygems.org' gem 'rspec', '~> 3.0' + +gem 'rubocop', '~> 1.15' diff --git a/ruby/Gemfile.lock b/ruby/Gemfile.lock index 8143957f..ebe44538 100644 --- a/ruby/Gemfile.lock +++ b/ruby/Gemfile.lock @@ -1,7 +1,14 @@ GEM remote: https://rubygems.org/ specs: + ast (2.4.2) diff-lcs (1.4.4) + parallel (1.20.1) + parser (3.0.1.1) + ast (~> 2.4.1) + rainbow (3.0.0) + regexp_parser (2.1.1) + rexml (3.2.5) rspec (3.10.0) rspec-core (~> 3.10.0) rspec-expectations (~> 3.10.0) @@ -15,12 +22,26 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) rspec-support (3.10.2) + rubocop (1.15.0) + parallel (~> 1.10) + parser (>= 3.0.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.5.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.6.0) + parser (>= 3.0.1.1) + ruby-progressbar (1.11.0) + unicode-display_width (2.0.0) PLATFORMS ruby DEPENDENCIES rspec (~> 3.0) + rubocop (~> 1.15) BUNDLED WITH 2.1.4 diff --git a/ruby/Makefile b/ruby/Makefile new file mode 100644 index 00000000..316829d1 --- /dev/null +++ b/ruby/Makefile @@ -0,0 +1,8 @@ +test: + docker-compose up --build test + +lint: + docker-compose up --build lint + +auto-correct: + docker-compose up --build auto-correct \ No newline at end of file diff --git a/ruby/README.md b/ruby/README.md new file mode 100644 index 00000000..7b6cadc0 --- /dev/null +++ b/ruby/README.md @@ -0,0 +1,13 @@ +## Guilded Rose +Guio and Lenise's implementation for [guilded rose refactoring](https://github.com/emilybache/GildedRose-Refactoring-Kata#gilded-rose-refactoring-kata) + +### Testing + +`make test` + +#### Linter - Rubocop + +`make lint` + +automatic fix detected offenses +`make auto-correct` \ No newline at end of file diff --git a/ruby/docker-compose.yml b/ruby/docker-compose.yml new file mode 100644 index 00000000..2b6484bf --- /dev/null +++ b/ruby/docker-compose.yml @@ -0,0 +1,22 @@ +version: '2' +services: + base: + build: + context: . + volumes: + - .:/app + + lint: + extends: + service: base + command: rubocop + + auto-correct: + extends: + service: base + command: bundle exec rubocop --auto-correct + + test: + extends: + service: base + command: bundle exec rspec gilded_rose_spec.rb \ No newline at end of file diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index f58cd6df..f7ca6733 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -1,45 +1,37 @@ -class GildedRose +# frozen_string_literal: true +class GildedRose def initialize(items) @items = items end - def update_quality() + def update_quality @items.each do |item| - case item.name - when "Sulfuras, Hand of Ragnaros" - when "Backstage passes to a TAFKAL80ETC concert" + if item.name.include?('Backstage passes') item.quality += 1 if item.sell_in > 10 - item.quality += 2 if item.sell_in <= 10 and item.sell_in > 5 + item.quality += 2 if (item.sell_in <= 10) && (item.sell_in > 5) item.quality += 3 if item.sell_in <= 5 item.sell_in -= 1 - item.quality = 0 if item.sell_in < 0 - when "Aged Brie" - item.sell_in -= 1 - if item.quality < 50 - if item.sell_in < 0 - item.quality += 2 - else - item.quality += 1 - end - end - when /Conjured/ - item.sell_in -= 1 - if item.quality > 0 - item.quality -= 2 - item.quality -= 2 if item.sell_in < 0 - end - else - item.sell_in -= 1 - if item.quality > 0 - item.quality -= 1 - item.quality -= 1 if item.sell_in < 0 + item.quality = 0 if item.sell_in.negative? + elsif !item.name.include?('Sulfuras') + factor = 0 + factor = case item.name + when 'Aged Brie' + 1 + when /Conjured/ + -2 + else + -1 + end + if (item.quality >= 0) && (item.quality <= 50) + item.sell_in -= 1 + item.quality += factor + item.quality += factor if item.sell_in.negative? end + item.quality = 50 if item.quality > 50 + item.quality = 0 if item.quality.negative? end - if item.quality < 0 - item.quality = 0 - end - end + end end end @@ -52,7 +44,7 @@ class Item @quality = quality end - def to_s() + def to_s "#{@name}, #{@sell_in}, #{@quality}" end end diff --git a/ruby/gilded_rose_spec.rb b/ruby/gilded_rose_spec.rb index c1b7aeba..1bf300a3 100644 --- a/ruby/gilded_rose_spec.rb +++ b/ruby/gilded_rose_spec.rb @@ -1,87 +1,87 @@ -require File.join(File.dirname(__FILE__), 'gilded_rose') +# frozen_string_literal: true + +require_relative File.join(File.dirname(__FILE__), 'gilded_rose') describe GildedRose do - - describe "#update_quality" do - it "does not change the name" do - items = [Item.new("foo", 0, 0)] - GildedRose.new(items).update_quality() - expect(items[0].name).to eq "foo" + describe '#update_quality' do + it 'does not change the name' do + items = [Item.new('foo', 0, 0)] + GildedRose.new(items).update_quality + expect(items[0].name).to eq 'foo' end - it "degrades quality at double speed when sell by date has passed" do - items = [Item.new("foo", 0, 10)] - GildedRose.new(items).update_quality() + it 'degrades quality at double speed when sell by date has passed' do + items = [Item.new('foo', 0, 10)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 8 end it "degrades quality at normal speed when sell by date hasn't passed" do - items = [Item.new("foo", 1, 10)] - GildedRose.new(items).update_quality() + items = [Item.new('foo', 1, 10)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 9 end it "does not decrease quality when it's zero" do - items = [Item.new("foo", 1, 0)] - GildedRose.new(items).update_quality() + items = [Item.new('foo', 1, 0)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 0 end it "increase quality by 1 when item is Aged Brie and sell by date hasn't passed" do - items = [Item.new("Aged Brie", 1, 0)] - GildedRose.new(items).update_quality() + items = [Item.new('Aged Brie', 1, 0)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 1 end - it "increase quality by 2 when item is Aged Brie and sell by date has passed" do - items = [Item.new("Aged Brie", 0, 0)] - GildedRose.new(items).update_quality() + it 'increase quality by 2 when item is Aged Brie and sell by date has passed' do + items = [Item.new('Aged Brie', 0, 0)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 2 end it "does not increase quality when it's already 50" do - items = [Item.new("Aged Brie", 0, 50)] - GildedRose.new(items).update_quality() + items = [Item.new('Aged Brie', 0, 50)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 50 end it "does not change quality and sell by date when it's a legendary item (Sulfuras)" do - items = [Item.new("Sulfuras, Hand of Ragnaros", 0, 80)] - GildedRose.new(items).update_quality() + items = [Item.new('Sulfuras, Hand of Ragnaros', 0, 80)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 80 expect(items[0].sell_in).to eq 0 end - it "increase quality by 1 when item is Backstage Passes and sell by date is bigger than 10" do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 11, 1)] - GildedRose.new(items).update_quality() + it 'increase quality by 1 when item is Backstage Passes and sell by date is bigger than 10' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 11, 1)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 2 end - - it "increase quality by 2 when item is Backstage Passes and sell by date is less or equal 10" do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 10, 1)] - GildedRose.new(items).update_quality() + + it 'increase quality by 2 when item is Backstage Passes and sell by date is less or equal 10' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 10, 1)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 3 end - it "increase quality by 3 when item is Backstage Passes and sell by date is less or equal 5" do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 5, 1)] - GildedRose.new(items).update_quality() + it 'increase quality by 3 when item is Backstage Passes and sell by date is less or equal 5' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 5, 1)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 4 end - it "sets quality to 0 when item is Backstage Passes and sell by date has passed" do - items = [Item.new("Backstage passes to a TAFKAL80ETC concert", 0, 50)] - GildedRose.new(items).update_quality() + it 'sets quality to 0 when item is Backstage Passes and sell by date has passed' do + items = [Item.new('Backstage passes to a TAFKAL80ETC concert', 0, 50)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 0 end - it "degrades quality twice as fast for Conjured items" do - items = [Item.new("Conjured sword", 10, 50), Item.new("Conjured axe", 0, 50)] - GildedRose.new(items).update_quality() + it 'degrades quality twice as fast for Conjured items' do + items = [Item.new('Conjured sword', 10, 50), Item.new('Conjured axe', 0, 50)] + GildedRose.new(items).update_quality expect(items[0].quality).to eq 48 expect(items[1].quality).to eq 46 end end - end