Skip to content

Instantly share code, notes, and snippets.

@DonSchado
Created December 20, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonSchado/5becd1717e0135a360007d693b3d6ed8 to your computer and use it in GitHub Desktop.
Save DonSchado/5becd1717e0135a360007d693b3d6ed8 to your computer and use it in GitHub Desktop.
Railslove Ruby programming exercise A3
# Railslove Ruby programming exercise A3
#
# To run the following exercise you need to have rspec installed.
# Then inspect the failing specs by running `rspec mission.rb`
#
# How well did the avengers manage their time on their missions?
# Your task is to group all time entries by mission and then sum the minutes in total per avenger.
# After grouping the entries the expected response looks something like:
#
# {
# :missions => {
# 'Hydra' => {
# :total => 5.25,
# :avengers => {
# 'Captain America' => 300.0,
# 'Nick Fury' => 15.0
# }
# },
# 'S.H.I.E.L.D' => {
# :total => 8.0,
# :avengers => {
# 'Captain America' => 15.0,
# 'Nick Fury' => 465.0
# }
# },
# # ...
# }
# }
#
# Read the specs for more detailed expected response. :)
#
# Also watch out for the BONUS POINTS! \o/
#
# Write code in the body of the group_by_mission method to make the tests pass.
#
# Refactor until you're happy with your solution, by asking the follwing questions:
# * Is the code understandable/maintainable?
# * Do I really need all these if/else/elsif statements?
#
require 'ostruct'
class AvangersTimeSheet < OpenStruct
def group_by_mission
{} # your code goes here
end
private
#
# maybe add some helper methods here?
#
end
# don't touch me
require 'rspec'
RSpec.describe 'AvangersTimeSheet' do
let(:entries) do
[
OpenStruct.new(avenger: 'Captain America', mission: 'Hydra', minutes: 240),
OpenStruct.new(avenger: 'Captain America', mission: 'Hydra', minutes: 60),
OpenStruct.new(avenger: 'Captain America', mission: 'S.H.I.E.L.D', minutes: 15),
OpenStruct.new(avenger: 'Hulk', mission: 'Loki', minutes: 45),
OpenStruct.new(avenger: 'Hulk', mission: 'Nuclear Missile', minutes: 180),
OpenStruct.new(avenger: 'Iron Man', mission: 'Loki', minutes: 45),
OpenStruct.new(avenger: 'Iron Man', mission: 'Loki', minutes: 90),
OpenStruct.new(avenger: 'Iron Man', mission: 'Nuclear Missile', minutes: 60),
OpenStruct.new(avenger: 'Iron Man', mission: 'Schawarma', minutes: 30),
OpenStruct.new(avenger: 'Nick Fury', mission: 'Hydra', minutes: 15),
OpenStruct.new(avenger: 'Nick Fury', mission: 'S.H.I.E.L.D', minutes: 465),
OpenStruct.new(avenger: 'Nick Fury', mission: 'Loki', minutes: 15),
OpenStruct.new(avenger: 'Thor', mission: 'Loki', minutes: 120),
OpenStruct.new(avenger: 'Thor', mission: 'Nuclear Missile', minutes: 90),
OpenStruct.new(avenger: 'Thor', mission: 'Schawarma', minutes: 30),
]
end
subject(:grouping) { AvangersTimeSheet.new(entries: entries).group_by_mission }
describe '#group_by_mission' do
it 'returns a key missions' do
expect(grouping).to match(hash_including(:missions))
end
it 'includes the nested mission' do
expect(grouping.fetch(:missions, {}).keys).to match_array(
['Loki', 'Nuclear Missile', 'Hydra', 'S.H.I.E.L.D', 'Schawarma']
)
end
it 'returns totals per mission' do
expect(grouping.fetch(:missions, {}).fetch('Loki', {})).to have_key(:total)
end
it 'the totals are translated to hours' do
expect(grouping.fetch(:missions, {}).fetch('Loki', {})[:total]).to eq(5.25)
expect(grouping.fetch(:missions, {}).fetch('S.H.I.E.L.D', {})[:total]).to eq(8.0)
end
it 'sums the minutes per avenger per mission' do
expect(grouping.fetch(:missions, {}).fetch('Loki', {}).fetch(:avengers, {})['Iron Man']).to eq(135.0)
expect(grouping.fetch(:missions, {}).fetch('Hydra', {}).fetch(:avengers, {})['Captain America']).to eq(300.0)
end
it 'WOW: Iron Man and Thor had time to have Schawarma' do
expect(grouping.fetch(:missions, {}).fetch('Schawarma', {})).to match(hash_including(
{ total: 1.0, avengers: { 'Iron Man' => 30.0, 'Thor' => 30.0 } }
))
end
it 'orders the projects by total (descending, from high to low)' do
skip "BONUS POINTS! BONUS POINTS! BONUS POINTS!"
# write a spec to also implement the ordering of the missions
end
end
describe '#top_performer' do
skip "BONUS POINTS! BONUS POINTS! BONUS POINTS!"
# write a spec to also implement the top_performer method which returns the avengers summary with the highest total
# {
# avenger: 'Nick Fury',
# total: 8.25,
# missions: [ 'Hydra', 'S.H.I.E.L.D', 'Loki' ]
# }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment