Created
December 6, 2011 05:54
-
-
Save ahoward/1436931 to your computer and use it in GitHub Desktop.
methods can convey meaning
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
def for(params, *args) | |
params = Map.for(params) | |
options = Map.options_for!(args) | |
key = Array(options[:key] || args).flatten.compact | |
key = [:upload] if key.empty? | |
return( | |
currently_uploaded_file(params, key, options) or | |
previously_uploaded_file(params, key, options) or | |
default_uploaded_file(params, key, options) | |
) | |
end | |
def currently_uploaded_file(params, key, options) | |
upload = params.get(key) | |
if upload.respond_to?(:read) | |
tmpdir do |tmp| | |
original_basename = | |
[:original_path, :original_filename, :path, :filename]. | |
map{|msg| upload.send(msg) if upload.respond_to?(msg)}.compact.first | |
basename = cleanname(original_basename) | |
path = File.join(tmp, basename) | |
open(path, 'wb'){|fd| fd.write(upload.read)} | |
upload_cache = UploadCache.new(key, path, options) | |
params.set(key, upload_cache.io) | |
return upload_cache | |
end | |
end | |
false | |
end | |
def previously_uploaded_file(params, key, options) | |
cache_key = cache_key_for(key) | |
upload_cache = params.get(cache_key) | |
if upload_cache | |
dirname, basename = File.split(File.expand_path(upload_cache)) | |
relative_dirname = File.basename(dirname) | |
relative_basename = File.join(relative_dirname, basename) | |
path = root + '/' + relative_basename | |
upload_cache = UploadCache.new(key, path, options) | |
params.set(key, upload_cache.io) | |
return upload_cache | |
end | |
false | |
end | |
def default_uploaded_file(params, key, options) | |
upload_cache = UploadCache.new(key, options) | |
params.set(key, upload_cache.io) if upload_cache.io | |
return upload_cache | |
end |
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
def for(params, *args) | |
params = Map.for(params) | |
options = Map.options_for!(args) | |
key = Array(options[:key] || args).flatten.compact | |
key = [:upload] if key.empty? | |
upload = params.get(key) | |
if upload.respond_to?(:read) | |
tmpdir do |tmp| | |
original_basename = | |
[:path, :filename, :original_path, :original_filename]. | |
map{|msg| upload.send(msg) if upload.respond_to?(msg)}.compact.first | |
basename = cleanname(original_basename) | |
path = File.join(tmp, basename) | |
open(path, 'wb'){|fd| fd.write(upload.read)} | |
upload_cache = UploadCache.new(key, path, options) | |
params.set(key, upload_cache.io) | |
return upload_cache | |
end | |
end | |
cache_key = cache_key_for(key) | |
upload_cache = params.get(cache_key) | |
if upload_cache | |
dirname, basename = File.split(upload_cache) | |
relative_dirname = File.expand_path(File.dirname(dirname)) | |
relative_basename = File.join(relative_dirname, basename) | |
path = root + '/' + relative_basename | |
upload_cache = UploadCache.new(key, path, options) | |
params.set(key, upload_cache.io) | |
return upload_cache | |
end | |
upload_cache = UploadCache.new(key, options) | |
params.set(key, upload_cache.io) if upload_cache.io | |
return upload_cache | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love little well named methods that get called as part of a larger process! Makes shitty stuff like this much easier to figure out, and its easy to test the edge cases. Took me a while to start writing code like non-sucky.rb