Created
December 21, 2012 03:57
-
-
Save jvnill/4350578 to your computer and use it in GitHub Desktop.
This file contains 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-12-20T23:26:34Z", | |
:totalcost=>30.827698, | |
:userproject=>"FCBIZ"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>92.440566, | |
:userproject=>"FCTA"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>9.481353, | |
:userproject=>"LMS"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>2.700484, | |
:userproject=>"adenbrook"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>16.837905, | |
:userproject=>"healthwise"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>3.615383, | |
:userproject=>"latronics"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>1171.492842, | |
:userproject=>"scout"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>0.002458, | |
:userproject=>"triggerapp"}, | |
{:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>34.834151, | |
:userproject=>nil}] | |
end | |
def desired_output | |
[{:date=>nil, | |
:lastmodificationdate=>"2012-12-20T23:26:34Z", | |
:totalcost=>34.834151, | |
:userproject=>nil}] | |
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