Permalink
Browse files

Define item, watermelon and pack objects.

  • Loading branch information...
SelenaSmall committed Sep 2, 2017
1 parent d16526d commit f2812ece9a4d347417e0651a794e5d972708801d
Showing with 122 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +4 −0 Gemfile.lock
  3. +12 −0 lib/item.rb
  4. +9 −0 lib/pack.rb
  5. +28 −0 lib/watermelon.rb
  6. +14 −0 spec/item_spec.rb
  7. +20 −0 spec/pack_spec.rb
  8. +34 −0 spec/watermelon_spec.rb
@@ -1,6 +1,7 @@
source 'https://rubygems.org'
gem 'bundler'
gem 'money'
gem 'rspec'
group :test do
@@ -5,7 +5,10 @@ GEM
simplecov
diff-lcs (1.3)
docile (1.1.5)
i18n (0.8.6)
json (2.1.0)
money (6.9.0)
i18n (>= 0.6.4, < 0.9)
rake (12.0.0)
rspec (3.6.0)
rspec-core (~> 3.6.0)
@@ -32,6 +35,7 @@ PLATFORMS
DEPENDENCIES
bundler
codeclimate-test-reporter (~> 1.0.0)
money
rake
rspec
simplecov
@@ -0,0 +1,12 @@
require 'money'
require_relative 'pack'
I18n.enforce_available_locales = false
# Item class
class Item
attr_reader :name
def initialize(name)
@name = name
end
end
@@ -0,0 +1,9 @@
# Pack class
class Pack
attr_reader :qty, :price
def initialize(qty, price)
@qty = qty
@price = price
end
end
@@ -0,0 +1,28 @@
require_relative 'item'
# Watermelon class
class Watermelon < Item
attr_reader :pack
OPTIONS = [3, 5].freeze
def initialize(name)
super(name)
@pack = nil
end
def pack_size(selection)
return unless OPTIONS.include?(selection)
return @pack = Pack.new(3, Money.new(699, 'NZD')) if selection == 3
@pack = Pack.new(5, Money.new(899, 'NZD')) if selection == 5
end
end
# watermelon = Watermelon.new('watermelon')
# puts watermelon
# size = watermelon.pack_size(3)
# puts size.qty
# puts size.price
@@ -0,0 +1,14 @@
require 'rspec'
require 'spec_helper'
require './lib/item'
describe Item do
describe '#initialize' do
it 'should have a name attribute which is a String' do
instance = Item.new('Watermelons')
expect(instance.name).to be_a String
expect(instance.name).to eq 'Watermelons'
end
end
end
@@ -0,0 +1,20 @@
require 'rspec'
require 'spec_helper'
require './lib/pack'
describe Pack do
describe '#initialize' do
it 'should have a qty attribute which is an Integer' do
instance = Pack.new(3, 6.99)
expect(instance.qty).to be_a Integer
expect(instance.qty).to eq 3
end
it 'should have a price attribute which is an instance of Money' do
instance = Pack.new(3, Money.new(699, 'NZD'))
expect(instance.price).to be_a Money
end
end
end
@@ -0,0 +1,34 @@
require 'rspec'
require 'spec_helper'
require './lib/watermelon'
describe Watermelon do
describe '#initialize' do
it 'should have a name attribute which is an String' do
instance = Watermelon.new('watermelon')
expect(instance.name).to be_a String
expect(instance.name).to eq 'watermelon'
end
it 'should have a pack attribute which is nil' do
instance = Watermelon.new('watermelon')
expect(instance.pack).to be_nil
end
end
describe '#pack_size' do
it 'should return nil if selection is not present in OPTIONS array' do
instance = Watermelon.new('watermelon')
expect(instance.pack_size(1)).to be_nil
end
it 'should return an instance of Pack if selection is present in OPTIONS array' do
instance = Watermelon.new('watermelon')
expect(instance.pack_size(3)).to be_a Pack
end
end
end

0 comments on commit f2812ec

Please sign in to comment.