class Sinatra::Backstage::StoredFile::StoredFile
Constants
- DIR
- URL_DIR
Public Class Methods
create(attrs, tempfile, extra = {})
click to toggle source
Methods
Calls superclass method
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 52 def self.create(attrs, tempfile, extra = {}) file = super( attrs.merge extra ) write_file(file, tempfile) file rescue DataMapper::SaveFailureError => e puts e.resource.errors.inspect end
stored_dir()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 34 def self.stored_dir "#{self::DIR + self::URL_DIR}" end
Protected Class Methods
write_file(stored_file, tempfile)
click to toggle source
Helpers
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 77 def self.write_file(stored_file, tempfile) FileUtils.mkdir_p ".#{stored_dir}" unless Dir.exist? ".#{stored_dir}" File.open(stored_file.stored_path, 'wb') do |f| f.write(tempfile.read) end end
Public Instance Methods
filesize_kb()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 26 def filesize_kb "#{filesize/1024}" end
stored_filename()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 38 def stored_filename extname = File.extname(filename) "#{File.basename(filename, extname)}_#{id}#{extname}" end
stored_path()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 43 def stored_path ".#{self.class.stored_dir}/#{stored_filename}" end
update(attrs, tempfile, extra = {})
click to toggle source
Calls superclass method
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 60 def update(attrs, tempfile, extra = {}) delete_file super( attrs.merge extra ) self.class.write_file(self, tempfile) rescue DataMapper::SaveFailureError => e puts e.resource.errors.inspect end
uploaded()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 30 def uploaded "#{created_at.strftime('%d %b')}" end
url()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 47 def url "#{self.class::URL_DIR}/#{stored_filename}" end
Protected Instance Methods
delete_file()
click to toggle source
# File lib/sinatra/backstage/stored_file/stored_file_model.rb, line 84 def delete_file File.delete(stored_path) if File.exist?(stored_path) end