Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created August 3, 2012 12:30
Show Gist options
  • Save jcasimir/3247167 to your computer and use it in GitHub Desktop.
Save jcasimir/3247167 to your computer and use it in GitHub Desktop.
LOOKUP_CHAIN = [:params, :user, :session, :http, :default]
def self.set_by(inputs)
locale = nil
LOOKUP_CHAIN.each do |lookup|
locale = send("by_#{lookup}", inputs[lookup])
break if locale
end
return locale
end
@roryokane
Copy link

A test harness of sorts, so that we can verify that our changed versions match the original:

class LocaleChooser


    LOOKUP_CHAIN = [:params, :user, :session, :http, :default]

    def self.set_by(inputs)
        locale = nil
        LOOKUP_CHAIN.each do |lookup|
            locale = send("by_#{lookup}", inputs[lookup])
            break if locale
        end
        return locale
    end


    # define self.by_params(input), self.by_user(input), etc.
    class << self
        returning_lookups = [:user, :http] # edit me for testing

        LOOKUP_CHAIN.each do |lookup|
            define_method("by_#{lookup}") do |input|
                puts "evaluated by_#{lookup}"

                if returning_lookups.include?(lookup)
                    return [lookup, input]
                else
                    return nil
                end
            end
        end
    end
end

p LocaleChooser.set_by({})
puts '---'
p LocaleChooser.set_by(:user => :custom_user) 
puts '---'
p LocaleChooser.set_by(:default => :custom_default)
puts '---'
p LocaleChooser.set_by(:params => :custom_params)

This harness is based on the code in ernie’s comment. The main part of the code, separated with whitespace, is straight from this Gist.

If you run this as shown above (on Ruby 1.9), you get this output:

evaluated by_params
evaluated by_user
[:user, nil]

---
evaluated by_params
evaluated by_user
[:user, :custom_user]

---
evaluated by_params
evaluated by_user
[:user, nil]

---
evaluated by_params
evaluated by_user
[:user, nil]

Change the calls at the bottom and the contents of returning_lookups to test different behaviors.

@roryokane
Copy link

Here’s a refactoring:

module Enumerable
  def find_mapped
    result = nil
    self.each do |lookup|
      result = yield(lookup)
      break if result
    end
    return result
  end
end
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

It produces the same output in the test harness I posted previously.

Oh, and sorry for the 4-space indentation in that previous test-harness code. I forgot to convert from tabs to spaces, and GitHub won’t let me edit that comment.

@roryokane
Copy link

@roryokane
Copy link

I just found out that you can make more than one fork of a Gist. So here’s a Gist fork for the test harness I posted earlier.

@roryokane
Copy link

This Gist was posted alongside a tweet by the author:

Any ideas to refactor this little ruby collection operation? https://gist.github.com/3247167

@Yardboy
Copy link

Yardboy commented Aug 6, 2012

Thought about this the other day but just got around to trying it this afternoon. I believe #any? stops when it reaches a truthy result, so how about this:

def self.set_by(inputs)
  locale = nil
  LOOKUP_CHAIN.any? { |lookup| locale = send("by_#{lookup}", inputs[lookup]) }
  return locale
end

Passes the test harness.

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