Skip to content

@ahoward /non-sucky.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
methods can convey meaning
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
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
@ubermajestix

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

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.