Skip to content

Instantly share code, notes, and snippets.

@roryokane
Forked from jcasimir/gist:3247167
Created August 3, 2012 17:51
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 roryokane/3249962 to your computer and use it in GitHub Desktop.
Save roryokane/3249962 to your computer and use it in GitHub Desktop.
refactoring posted code by creating Enumerable#find_mapped
LOOKUP_CHAIN = [:params, :user, :session, :http, :default]
def self.set_by(inputs)
LOOKUP_CHAIN.find_mapped do |lookup|
locale = send("by_#{lookup}", inputs[lookup])
end
end
module Enumerable
def find_mapped
result = nil
self.each do |lookup|
result = yield(lookup)
break if result
end
return result
end
end
@roryokane
Copy link
Author

find_mapped could have an ifnone parameter added, like find has. See find’s documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment