E4679be8d7d2484f9c0b8c4df71f4d2a

This actually works but I wonder if there's a simpler, cleaner and/or more readable way to achieve the same results.

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 !

348246701cfdb2130b842fd839751a18

Raul Murciano

August 19, 2010, August 19, 2010 18:59, permalink

No rating. Login to rate!
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.
A8d3f35baafdaea851914b17dae9e1fc

Adam

August 19, 2010, August 19, 2010 19:04, permalink

No rating. Login to rate!
attributes = openid_mapping.inject({}) do |hash,(key,maps)|
  hash.update(key => result.values_at(*maps).compact.first)
end
9dc2fe52cbc7a4f42b54d056e470efea

Krzysztof Wilczynski

August 20, 2010, August 20, 2010 14:29, permalink

No rating. Login to rate!

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] }]
C5f84c74d67f28a62fbadf44b2edbdb0

work home jobs under

November 4, 2010, November 04, 2010 04:29, permalink

No rating. Login to rate!

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
81ee518a1181fd0766c4ec89e5d7a079

flellareire

September 4, 2011, September 04, 2011 11:54, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

81ee518a1181fd0766c4ec89e5d7a079

NarbrommagDug

September 7, 2011, September 07, 2011 06:01, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

5e536dbc473469d0af1d44020a4b0bab

abrantTar

September 19, 2011, September 19, 2011 17:22, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

5e536dbc473469d0af1d44020a4b0bab

Cacralley

September 20, 2011, September 20, 2011 21:27, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

772ebcc4c3ef8a1dab8b677c0c45c588

rorkedmedia

November 1, 2011, November 01, 2011 12:19, permalink

No rating. Login to rate!

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*

9e67cf08593df723202d8a7083072f67

Deannyten

November 15, 2011, November 15, 2011 17:15, permalink

No rating. Login to rate!

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*

F0331c47e300bd43483bcce637523003

vostenofs

November 16, 2011, November 16, 2011 14:51, permalink

No rating. Login to rate!

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.

76ba81d8bf91ab0042fc3476b487a408

Peeleaskirl

November 19, 2011, November 19, 2011 19:02, permalink

No rating. Login to rate!

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.

1a295524b1ce0c250072963eead13184

nocaoccub

November 12, 2011, November 12, 2011 08:05, permalink

No rating. Login to rate!

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.

A00cef0bc511c9c4f6b8fa1c81e98917

erunnyutesy

November 15, 2011, November 15, 2011 07:49, permalink

No rating. Login to rate!

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.

9567bd4ee5ea6fdc6ff66c69e5c0b968

Rilsotbloth

November 20, 2011, November 20, 2011 18:14, permalink

No rating. Login to rate!

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.

21eb0f5dc6dd6cfa74971bb4bda87412

deermcregmene

December 22, 2011, December 22, 2011 20:29, permalink

No rating. Login to rate!

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.

8afb24cbb37771d3604cf1b175fa9c58

boubjesee

November 27, 2011, November 27, 2011 07:43, permalink

No rating. Login to rate!

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.

F8060a0ec00043c539a3f143e282cf4f

elafDeema

November 27, 2011, November 27, 2011 19:45, permalink

No rating. Login to rate!

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*

E28d075f9b3c4daf5d9bac6dee5adfdf

reesetrumszet

November 29, 2011, November 29, 2011 20:41, permalink

No rating. Login to rate!

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.

46772bca7a2249035cec92de277c0391

Feriordedia

January 16, 2012, January 16, 2012 13:11, permalink

No rating. Login to rate!

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*

339917717d728686e8dfa823242d13c8

SWASIAWOUMS

December 24, 2011, December 24, 2011 22:11, permalink

No rating. Login to rate!

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.

92e12544ec63b2bd1950b11f78628dff

addichaccipsy

December 28, 2011, December 28, 2011 11:04, permalink

No rating. Login to rate!

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*

945f35eccee794a84869465a3be349c5

FabsBlammaKah

December 28, 2011, December 28, 2011 20:23, permalink

No rating. Login to rate!

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*

Afe8e8f8d73ac17b6f156a28337a3495

PienWonee

December 29, 2011, December 29, 2011 01:53, permalink

No rating. Login to rate!

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*

0e18fa150b731e599d8dd32b5202ce82

Invoixmoott

December 29, 2011, December 29, 2011 21:38, permalink

No rating. Login to rate!

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*

56797e70a9cef3127328d0d7c5357efd

beistMuGseeks

December 29, 2011, December 29, 2011 22:15, permalink

No rating. Login to rate!

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*

Afd225dd756e748c8504b59df06cef8d

Tawdielflyday

January 20, 2012, January 20, 2012 17:06, permalink

No rating. Login to rate!

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*

A121c48e6c9eef8adec2838944359955

KNEEDGINGTHOG

January 20, 2012, January 20, 2012 19:26, permalink

No rating. Login to rate!

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*

E43737dac874c9f246c79e66d3308fd8

papydiple

January 21, 2012, January 21, 2012 09:37, permalink

No rating. Login to rate!

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*

Your refactoring





Format Copy from initial code

or Cancel