Skip to content

Instantly share code, notes, and snippets.

@markmcspadden
Created December 4, 2012 23:59
Show Gist options
  • Save markmcspadden/4210444 to your computer and use it in GitHub Desktop.
Save markmcspadden/4210444 to your computer and use it in GitHub Desktop.
My Best and Worst of 2012
# Best/Worst 2012
# WORST
# Recreated as best as I can remember it
class ResourcesController < ApiController
# GET /api/:resource_type
# GET /api/opportunities
# GET /api/offer_performance_objects?query=opportunity_id%3dAF12JFHVN291
def index
@resources = params[:resource_type].constantize.find_by_query(params[:query])
sleep 10
render @resource.to_json
end
def show
# ...
end
# ...
end
# BEST
# https://github.com/rails/rails/pull/6259
# https://github.com/rails/rails/commit/6ff887321b930197a8d62ddd0fc1dcb965ccda29
# activesupport/lib/active_support/core_ext/hash/keys.rb
class Hash
# Return a new hash with all keys converted using the block operation.
#
# { :name => 'Rob', :years => '28' }.transform_keys{ |key| key.to_s.upcase }
# # => { "NAME" => "Rob", "YEARS" => "28" }
def transform_keys
result = {}
keys.each do |key|
result[yield(key)] = self[key]
end
result
end
# Destructively convert all keys using the block operations.
# Same as transform_keys but modifies +self+
def transform_keys!
keys.each do |key|
self[yield(key)] = delete(key)
end
self
end
# Return a new hash with all keys converted to strings.
#
# { :name => 'Rob', :years => '28' }.stringify_keys
# #=> { "name" => "Rob", "years" => "28" }
def stringify_keys
transform_keys{ |key| key.to_s }
end
# Destructively convert all keys to strings. Same as
# +stringify_keys+, but modifies +self+.
def stringify_keys!
transform_keys!{ |key| key.to_s }
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment