Skip to content

Instantly share code, notes, and snippets.

@jvnill
Created December 21, 2012 03:57
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 jvnill/4350578 to your computer and use it in GitHub Desktop.
Save jvnill/4350578 to your computer and use it in GitHub Desktop.
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