-
-
Save roryokane/3249962 to your computer and use it in GitHub Desktop.
refactoring posted code by creating Enumerable#find_mapped
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| module Enumerable | |
| def find_mapped | |
| result = nil | |
| self.each do |lookup| | |
| result = yield(lookup) | |
| break if result | |
| end | |
| return result | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
find_mappedcould have anifnoneparameter added, likefindhas. Seefind’s documentation.