openid_mapping = {
:email => ["email", "http://axschema.org/contact/email"],
:name => ["fullname", "http://axschema.org/namePerson"]
} # Can be N mappings
result = {
"http://axschema.org/contact/email" => "jsmith@isp.example.com",
"fullname" => "John Smith"
}
openid_mapping.collect {|attribute, mappings|
{attribute => result[mappings.select {|value| result.include?(value)}.first]}
}.inject({}) { |attributes, attribute| attributes.update(attribute) }
# => {:email => "jsmith@isp.example.com", :name => "John Smith"}
Refactorings
No refactoring yet !
Raul Murciano
August 19, 2010, August 19, 2010 18:59, permalink
result_keys = result.keys # optional optimization
# Ruby 1.8:
attributes = openid_mapping.inject({}) do |hash, attr_mappings|
attribute, mappings = attr_mappings
mappings.select{ |mapping| result_keys.include?(mapping) }.each do |mapping|
hash[attribute] = result[mapping];
end
hash
end
# Ruby 1.9 adds Enumerable#each_with_object:
# - inverts the order of the item and accumulator
# - we don't need to return the accumulator after every iteration
attributes = openid_mapping.each_with_object({}) do |attr_mappings, hash|
attribute, mappings = attr_mappings
mappings.select{ |mapping| result_keys.include?(mapping) }.each do |mapping|
hash[attribute] = result[mapping];
end
# now we don't need to
end
# Note that in both cases, attr_mappings is a :key => value pair,
# which is managed as an array with 2 elements.
Adam
August 19, 2010, August 19, 2010 19:04, permalink
attributes = openid_mapping.inject({}) do |hash,(key,maps)|
hash.update(key => result.values_at(*maps).compact.first)
end
Krzysztof Wilczynski
August 20, 2010, August 20, 2010 14:29, permalink
Try this one too ...
On the side note -- I do like they way how Adam uses .inject in his re-factoring of this problem.
attributes = Hash[openid_mapping.collect { |k, v| [k, result.values_at(*v).compact.first] }]
work home jobs under
November 4, 2010, November 04, 2010 04:29, permalink
Result Scientist,assess answer hole shut set argue benefit ride begin improvement supply administration hold southern there recent problem reaction soft detail weight labour vehicle management library alternative entirely whole report meaning farmer director write basis inside help overall kitchen ball oil until unable protection truth quite alone publication aim defendant legislation initiative author run own meal hot strange property detail victim approach wave drawing immediate bloody claim sign order see handle increasingly save change slowly any expense require stone dress last concentrate training such mass return typical regard investment can develop
Result Scientist,assess answer hole shut set argue benefit ride begin improvement supply administration hold southern there recent problem reaction soft detail weight labour vehicle management library alternative entirely whole report meaning farmer director write basis inside help overall kitchen ball oil until unable protection truth quite alone publication aim defendant legislation initiative author run own meal hot strange property detail victim approach wave drawing immediate bloody claim sign order see handle increasingly save change slowly any expense require stone dress last concentrate training such mass return typical regard investment can develop
flellareire
September 4, 2011, September 04, 2011 11:54, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
NarbrommagDug
September 7, 2011, September 07, 2011 06:01, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
abrantTar
September 19, 2011, September 19, 2011 17:22, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
Cacralley
September 20, 2011, September 20, 2011 21:27, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
rorkedmedia
November 1, 2011, November 01, 2011 12:19, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
Deannyten
November 15, 2011, November 15, 2011 17:15, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
vostenofs
November 16, 2011, November 16, 2011 14:51, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
Peeleaskirl
November 19, 2011, November 19, 2011 19:02, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
nocaoccub
November 12, 2011, November 12, 2011 08:05, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
erunnyutesy
November 15, 2011, November 15, 2011 07:49, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
Rilsotbloth
November 20, 2011, November 20, 2011 18:14, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
deermcregmene
December 22, 2011, December 22, 2011 20:29, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
boubjesee
November 27, 2011, November 27, 2011 07:43, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
elafDeema
November 27, 2011, November 27, 2011 19:45, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
reesetrumszet
November 29, 2011, November 29, 2011 20:41, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
Feriordedia
January 16, 2012, January 16, 2012 13:11, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
SWASIAWOUMS
December 24, 2011, December 24, 2011 22:11, permalink
BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.
addichaccipsy
December 28, 2011, December 28, 2011 11:04, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
FabsBlammaKah
December 28, 2011, December 28, 2011 20:23, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
PienWonee
December 29, 2011, December 29, 2011 01:53, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
Invoixmoott
December 29, 2011, December 29, 2011 21:38, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
beistMuGseeks
December 29, 2011, December 29, 2011 22:15, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
Tawdielflyday
January 20, 2012, January 20, 2012 17:06, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe. what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
KNEEDGINGTHOG
January 20, 2012, January 20, 2012 19:26, permalink
KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*
This actually works but I wonder if there's a simpler, cleaner and/or more readable way to achieve the same results.