Class: RailsUploads::Types::File
- Inherits:
-
Object
- Object
- RailsUploads::Types::File
- Defined in:
- lib/rails_uploads/types/file.rb
Direct Known Subclasses
Instance Method Summary collapse
- #delete ⇒ Object
- #exists?(*args) ⇒ Boolean
- #extname ⇒ Object
- #filename ⇒ Object
-
#initialize(source, options = {}) ⇒ File
constructor
A new instance of File.
- #is_default? ⇒ Boolean
- #path(*args) ⇒ Object
- #size(*args) ⇒ Object
- #store ⇒ Object
- #url(*args) ⇒ Object
Constructor Details
#initialize(source, options = {}) ⇒ File
Returns a new instance of File.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rails_uploads/types/file.rb', line 5 def initialize(source, ={}) if source.is_a? ActionDispatch::Http::UploadedFile or source.is_a? Rack::Test::UploadedFile @upload = source @stored = false @default = false @storage = build_storage(:local) elsif source.is_a? String @upload = false @filename = source @stored = true @default = false @storage = build_storage elsif .has_key? :default @upload = false @filename = [:default] @stored = true @default = true @storage = build_storage end @deleted = false = end |
Instance Method Details
#delete ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/rails_uploads/types/file.rb', line 72 def delete if not is_default? and is_stored? and exists? storage.delete path yield if block_given? @storage = build_storage(:local) if @upload.present? @stored = false @deleted = true end end |
#exists?(*args) ⇒ Boolean
32 33 34 35 |
# File 'lib/rails_uploads/types/file.rb', line 32 def exists?(*args) return false if is_deleted? storage.exists? path(*args) end |
#extname ⇒ Object
42 43 44 45 |
# File 'lib/rails_uploads/types/file.rb', line 42 def extname return nil if is_deleted? @extname ||= ::File.extname(filename) end |
#filename ⇒ Object
47 48 49 50 |
# File 'lib/rails_uploads/types/file.rb', line 47 def filename return nil if is_deleted? @filename ||= "#{(Time.now.to_f * 10000000).to_i}#{::File.extname @upload.original_filename}".downcase end |
#is_default? ⇒ Boolean
28 29 30 |
# File 'lib/rails_uploads/types/file.rb', line 28 def is_default? @default end |
#path(*args) ⇒ Object
52 53 54 55 |
# File 'lib/rails_uploads/types/file.rb', line 52 def path(*args) return nil if is_deleted? is_stored? ? destination_path(*args) : @upload.path end |
#size(*args) ⇒ Object
37 38 39 40 |
# File 'lib/rails_uploads/types/file.rb', line 37 def size(*args) return 0 if is_deleted? storage.size path(*args) end |
#store ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/rails_uploads/types/file.rb', line 62 def store if not is_stored? and is_upload? @storage = build_storage storage.store @upload, destination_path yield if block_given? @stored = true @deleted = false end end |
#url(*args) ⇒ Object
57 58 59 60 |
# File 'lib/rails_uploads/types/file.rb', line 57 def url(*args) return nil if is_deleted? or not is_stored? storage.url path(*args) end |