Last active
December 10, 2015 00:19
-
-
Save jp/4350536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'json' | |
require 'active_support/all' | |
require 'pry' | |
def input | |
[ | |
{lastmodificationdate:"2012-11-16",userproject:'FCBIZ',totalcost:10}, | |
{lastmodificationdate:"2012-11-16",userproject:'LMS',totalcost:20}, | |
{lastmodificationdate:"2012-11-16",userproject:'NetEngine',totalcost:52}, | |
{lastmodificationdate:"2012-11-17",userproject:'FCBIZ',totalcost:11}, | |
{lastmodificationdate:"2012-11-17",userproject:'LMS',totalcost:22}, | |
{lastmodificationdate:"2012-11-17",userproject:'NetEngine',totalcost:53}, | |
{lastmodificationdate:"2012-11-18",userproject:'FCBIZ',totalcost:34}, | |
{lastmodificationdate:"2012-11-18",userproject:'LMS',totalcost:32}, | |
{lastmodificationdate:"2012-11-18",userproject:'NetEngine',totalcost:55} | |
] | |
end | |
def desired_output | |
[ | |
{lastmodificationdate:"2012-12-16",FCBIZ:10,LMS:20,NetEngine:52}, | |
{lastmodificationdate:"2012-12-17",FCBIZ:11,LMS:22,NetEngine:53}, | |
{lastmodificationdate:"2012-12-18",FCBIZ:34,LMS:32,NetEngine:55} | |
] | |
end | |
class NetEngineGolf | |
def jim_1(input) | |
input.group_by { |a| a[:date] }.map do |date, values| | |
values.inject({}) do |hash, value| | |
hash.merge! value | |
end | |
end | |
end | |
def jim_2(input) | |
input.group_by { |a| a[:date] }.map do |date, values| | |
merged = { date: date } | |
values.each { |value| merged.merge! value.except(:date) } | |
merged | |
end | |
end | |
end | |
describe NetEngineGolf do | |
describe "#jim_1" do | |
it "turns the input into the output" do | |
output = NetEngineGolf.new.jim_1(input) | |
output.should == desired_output | |
end | |
end | |
describe "#jim_2" do | |
it "turns the input into the output" do | |
output = NetEngineGolf.new.jim_2(input) | |
output.should == desired_output | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment