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
- #is_deleted? ⇒ 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 @options = end |
Instance Method Details
#delete ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/rails_uploads/types/file.rb', line 76 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
46 47 48 49 |
# File 'lib/rails_uploads/types/file.rb', line 46 def extname return nil if is_deleted? @extname ||= ::File.extname(filename) end |
#filename ⇒ Object
51 52 53 54 |
# File 'lib/rails_uploads/types/file.rb', line 51 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 |
#is_deleted? ⇒ Boolean
37 38 39 |
# File 'lib/rails_uploads/types/file.rb', line 37 def is_deleted? @deleted end |
#path(*args) ⇒ Object
56 57 58 59 |
# File 'lib/rails_uploads/types/file.rb', line 56 def path(*args) return nil if is_deleted? is_stored? ? destination_path(*args) : @upload.path end |
#size(*args) ⇒ Object
41 42 43 44 |
# File 'lib/rails_uploads/types/file.rb', line 41 def size(*args) return 0 if is_deleted? storage.size path(*args) end |
#store ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/rails_uploads/types/file.rb', line 66 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
61 62 63 64 |
# File 'lib/rails_uploads/types/file.rb', line 61 def url(*args) return nil if is_deleted? or not is_stored? storage.url path(*args) end |