Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Define item, watermelon and pack objects.
  • Loading branch information
SelenaSmall committed Sep 2, 2017
1 parent d16526d commit f2812ec
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
@@ -1,6 +1,7 @@
source 'https://rubygems.org'

gem 'bundler'
gem 'money'
gem 'rspec'

group :test do
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand All @@ -32,6 +35,7 @@ PLATFORMS
DEPENDENCIES
bundler
codeclimate-test-reporter (~> 1.0.0)
money
rake
rspec
simplecov
Expand Down
12 changes: 12 additions & 0 deletions lib/item.rb
@@ -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
9 changes: 9 additions & 0 deletions lib/pack.rb
@@ -0,0 +1,9 @@
# Pack class
class Pack
attr_reader :qty, :price

def initialize(qty, price)
@qty = qty
@price = price
end
end
28 changes: 28 additions & 0 deletions lib/watermelon.rb
@@ -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
14 changes: 14 additions & 0 deletions spec/item_spec.rb
@@ -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
20 changes: 20 additions & 0 deletions spec/pack_spec.rb
@@ -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
34 changes: 34 additions & 0 deletions spec/watermelon_spec.rb
@@ -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.