-
-
Save thedarkone/599605 to your computer and use it in GitHub Desktop.
This file contains 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
## Original | |
def reverse_sql_order(order_query) | |
order_query.join(', ').split(',').collect { |s| | |
if s.match(/\s(asc|ASC)$/) | |
s.gsub(/\s(asc|ASC)$/, ' DESC') | |
elsif s.match(/\s(desc|DESC)$/) | |
s.gsub(/\s(desc|DESC)$/, ' ASC') | |
else | |
s + ' DESC' | |
end | |
} | |
end | |
## Modified | |
def reverse_sql_order(order_query) | |
order_query.join(', ').split(',').collect { |s| | |
if s.match(/\s(asc|ASC)$/) | |
s.gsub!(/\s(asc|ASC)$/, ' DESC') | |
elsif s.match(/\s(desc|DESC)$/) | |
s.gsub!(/\s(desc|DESC)$/, ' ASC') | |
else | |
s.concat(' DESC') | |
end | |
s | |
} | |
end | |
## Modified - don't create match object | |
def reverse_sql_order(order_query) | |
order_query.join(', ').split(',').collect { |s| | |
if s =~ /\s(asc|ASC)$/ | |
s.gsub!(/\s(asc|ASC)$/, ' DESC') | |
elsif s =~ /\s(desc|DESC)$/ | |
s.gsub!(/\s(desc|DESC)$/, ' ASC') | |
else | |
s.concat(' DESC') | |
end | |
s | |
} | |
end | |
## Modified - case? | |
def reverse_sql_order(order_query) | |
order_query.join(', ').split(',').collect { |s| | |
case s | |
when /\s(asc|ASC)$/ | |
s.gsub!(/\s(asc|ASC)$/, ' DESC') | |
when /\s(desc|DESC)$/ | |
s.gsub!(/\s(desc|DESC)$/, ' ASC') | |
else | |
s.concat(' DESC') | |
end | |
s | |
} | |
end | |
## Lazy gsub? | |
def reverse_sql_order(order_query) | |
order_query.join(', ').split(',').collect do |s| | |
s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment