Skip to content

@thedarkone /gist:599605 forked from carlosantoniodasilva/gist:599566
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
## 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
Something went wrong with that request. Please try again.