Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
AllCops:
NewCops: enable

Metrics/BlockLength:
Enabled: true
Exclude:
- spec/**/*

Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.1.2
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: ruby
rvm:
- 2.7
- 3.1.2
14 changes: 10 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.org'

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem "rspec"
gem 'guard'
gem 'guard-shell'
gem 'rake'
gem 'rake'
gem 'rspec'

group :development do
gem 'rubocop'
gem 'rubocop-shopify', require: false
gem 'ruby-lsp'
end
84 changes: 60 additions & 24 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
coderay (1.1.3)
diff-lcs (1.4.4)
ffi (1.13.1)
formatador (0.2.5)
guard (2.16.2)
diff-lcs (1.5.0)
ffi (1.15.5)
formatador (1.1.0)
guard (2.18.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (>= 1.0.12, < 2.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
pry (>= 0.13.0)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-shell (0.7.1)
guard-shell (0.7.2)
guard (>= 2.0.0)
guard-compat (~> 1.0)
listen (3.2.1)
json (2.6.2)
language_server-protocol (3.17.0.1)
listen (3.7.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lumberjack (1.2.8)
Expand All @@ -27,37 +30,70 @@ GEM
notiffany (0.1.3)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.13.1)
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
prettier_print (0.1.0)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.0.1)
rb-fsevent (0.10.4)
rainbow (3.1.1)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.3)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
regexp_parser (2.5.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.36.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.2.1)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.20.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-shopify (2.9.0)
rubocop (~> 1.33)
ruby-lsp (0.3.2)
language_server-protocol (~> 3.17.0)
sorbet-runtime
syntax_tree (>= 3.4)
ruby-progressbar (1.11.0)
shellany (0.0.1)
thor (1.0.1)
sorbet-runtime (0.5.10415)
syntax_tree (3.5.0)
prettier_print
thor (1.2.1)
unicode-display_width (2.2.0)

PLATFORMS
ruby
arm64-darwin-21

DEPENDENCIES
guard
guard-shell
rake
rspec
rubocop
rubocop-shopify
ruby-lsp

BUNDLED WITH
2.1.2
2.3.19
4 changes: 3 additions & 1 deletion Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

Expand All @@ -19,5 +21,5 @@
# watch(%r{file/path}) { `command(s)` }
#
guard :shell do
watch(%r{^*\.rb}) { `bundle exec rspec --force-color spec/` }
watch(/^*\.rb/) { `bundle exec rspec --force-color spec/` }
end
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

begin
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |t|
t.fail_on_error = false
end

task :default => :spec
task default: :spec
rescue LoadError
# no rspec available
end
end
2 changes: 1 addition & 1 deletion lib/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def scan(barcode)
def total
@cart.reduce(0) do |total, (item, quantity)|
rule = @rules.find(item)
total + rule.apply(quantity: quantity)
total + rule.apply(quantity:)
end.floor(2)
end
end
4 changes: 2 additions & 2 deletions lib/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class Product
attr_accessor :code, :price

def initialize(code, price = 0.00)
raise ArgumentError.new('product needs a valid code') if code.empty?
raise ArgumentError.new('price needs to be positive or 0') if price.negative?
raise ArgumentError, 'product needs a valid code' if code.empty?
raise ArgumentError, 'price needs to be positive or 0' if price.negative?

self.code = code
self.price = price
Expand Down
6 changes: 3 additions & 3 deletions lib/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def initialize(product, minimun: 0, per: 1, price: nil)
# the price of the rule should be the same of
# the product in case it wasn't informed
self.price = price || product.price
raise ArgumentError.new('price should be a positive number') if self.price.negative?
raise ArgumentError, 'price should be a positive number' if self.price.negative?

self.product = product
self.minimun = minimun
self.per = per
Expand All @@ -30,6 +30,6 @@ def apply(quantity: 0)
quantity = quantity % per
end

total + product.price * quantity
total + (product.price * quantity)
end
end
22 changes: 12 additions & 10 deletions lib/rules.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# frozen_string_literal: true

class Rules
def initialize(*rules)
attr_reader :rules

def initialize(*received_rules)
@rules = []
rules.each { |rule| add(rule) }
received_rules.each { |rule| add(rule) }
end

def count
@rules.count
rules.count
end

def each
@rules.each { |p| yield p }
def each(&)
rules.each(&)
end

def add(rule)
raise StandardError.new('only one rule per product') if self.exists?(rule)
@rules << rule
raise StandardError, 'only one rule per product' if exists?(rule)

rules << rule
end

def find(code)
@rules.find { |rule| rule.code == code }
rules.find { |rule| rule.code == code }
end

private

def exists?(rule)
@rules.map(&:code).include?(rule.code)
rules.map(&:code).include?(rule.code)
end
end
12 changes: 6 additions & 6 deletions spec/checkout_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require 'rule.rb'
require 'rules.rb'
require 'product.rb'
require 'errors.rb'
require 'checkout.rb'
require 'rule'
require 'rules'
require 'product'
require 'errors'
require 'checkout'

RSpec.describe Checkout do
before(:each) do
Expand All @@ -17,7 +17,7 @@
Rule.new(gr1, minimun: 2, per: 2, price: 3.11),
Rule.new(sr1, minimun: 3, per: 1, price: 4.50),
Rule.new(cf1, minimun: 3, per: 1, price: 11.23 / 3 * 2),
Rule.new(gr2),
Rule.new(gr2)
].each { |r| pricing_rules.add(r) }
@checkout = Checkout.new(pricing_rules)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/product_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'product.rb'
require 'product'

RSpec.describe Product do
describe '#new' do
Expand Down
4 changes: 2 additions & 2 deletions spec/rule_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'product.rb'
require 'rule.rb'
require 'product'
require 'rule'

RSpec.describe Rule do
before(:each) do
Expand Down
8 changes: 4 additions & 4 deletions spec/rules_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'product.rb'
require 'rule.rb'
require 'rules.rb'
require 'product'
require 'rule'
require 'rules'

RSpec.describe Rules do
before(:each) do
Expand Down Expand Up @@ -33,7 +33,7 @@
end
end

describe '#find' do
describe '#find' do
before(:each) do
@rules = Rules.new
@rules.add(@rule)
Expand Down
Loading