Skip to content

Commit f2812ec

Browse files
committed
Define item, watermelon and pack objects.
1 parent d16526d commit f2812ec

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source 'https://rubygems.org'
22

33
gem 'bundler'
4+
gem 'money'
45
gem 'rspec'
56

67
group :test do

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ GEM
55
simplecov
66
diff-lcs (1.3)
77
docile (1.1.5)
8+
i18n (0.8.6)
89
json (2.1.0)
10+
money (6.9.0)
11+
i18n (>= 0.6.4, < 0.9)
912
rake (12.0.0)
1013
rspec (3.6.0)
1114
rspec-core (~> 3.6.0)
@@ -32,6 +35,7 @@ PLATFORMS
3235
DEPENDENCIES
3336
bundler
3437
codeclimate-test-reporter (~> 1.0.0)
38+
money
3539
rake
3640
rspec
3741
simplecov

lib/item.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'money'
2+
require_relative 'pack'
3+
I18n.enforce_available_locales = false
4+
5+
# Item class
6+
class Item
7+
attr_reader :name
8+
9+
def initialize(name)
10+
@name = name
11+
end
12+
end

lib/pack.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Pack class
2+
class Pack
3+
attr_reader :qty, :price
4+
5+
def initialize(qty, price)
6+
@qty = qty
7+
@price = price
8+
end
9+
end

lib/watermelon.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative 'item'
2+
3+
# Watermelon class
4+
class Watermelon < Item
5+
attr_reader :pack
6+
7+
OPTIONS = [3, 5].freeze
8+
9+
def initialize(name)
10+
super(name)
11+
@pack = nil
12+
end
13+
14+
def pack_size(selection)
15+
return unless OPTIONS.include?(selection)
16+
17+
return @pack = Pack.new(3, Money.new(699, 'NZD')) if selection == 3
18+
@pack = Pack.new(5, Money.new(899, 'NZD')) if selection == 5
19+
end
20+
end
21+
22+
# watermelon = Watermelon.new('watermelon')
23+
# puts watermelon
24+
25+
# size = watermelon.pack_size(3)
26+
27+
# puts size.qty
28+
# puts size.price

spec/item_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/item'
4+
5+
describe Item do
6+
describe '#initialize' do
7+
it 'should have a name attribute which is a String' do
8+
instance = Item.new('Watermelons')
9+
10+
expect(instance.name).to be_a String
11+
expect(instance.name).to eq 'Watermelons'
12+
end
13+
end
14+
end

spec/pack_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/pack'
4+
5+
describe Pack do
6+
describe '#initialize' do
7+
it 'should have a qty attribute which is an Integer' do
8+
instance = Pack.new(3, 6.99)
9+
10+
expect(instance.qty).to be_a Integer
11+
expect(instance.qty).to eq 3
12+
end
13+
14+
it 'should have a price attribute which is an instance of Money' do
15+
instance = Pack.new(3, Money.new(699, 'NZD'))
16+
17+
expect(instance.price).to be_a Money
18+
end
19+
end
20+
end

spec/watermelon_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/watermelon'
4+
5+
describe Watermelon do
6+
describe '#initialize' do
7+
it 'should have a name attribute which is an String' do
8+
instance = Watermelon.new('watermelon')
9+
10+
expect(instance.name).to be_a String
11+
expect(instance.name).to eq 'watermelon'
12+
end
13+
14+
it 'should have a pack attribute which is nil' do
15+
instance = Watermelon.new('watermelon')
16+
17+
expect(instance.pack).to be_nil
18+
end
19+
end
20+
21+
describe '#pack_size' do
22+
it 'should return nil if selection is not present in OPTIONS array' do
23+
instance = Watermelon.new('watermelon')
24+
25+
expect(instance.pack_size(1)).to be_nil
26+
end
27+
28+
it 'should return an instance of Pack if selection is present in OPTIONS array' do
29+
instance = Watermelon.new('watermelon')
30+
31+
expect(instance.pack_size(3)).to be_a Pack
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)