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
- #is_stored? ⇒ 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
80 81 82 83 84 85 86 87 88 |
# File 'lib/rails_uploads/types/file.rb', line 80 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
40 41 42 43 |
# File 'lib/rails_uploads/types/file.rb', line 40 def exists?(*args) return false if is_deleted? storage.exists? path(*args) end |
#extname ⇒ Object
50 51 52 53 |
# File 'lib/rails_uploads/types/file.rb', line 50 def extname return nil if is_deleted? @extname ||= ::File.extname(filename) end |
#filename ⇒ Object
55 56 57 58 |
# File 'lib/rails_uploads/types/file.rb', line 55 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
36 37 38 |
# File 'lib/rails_uploads/types/file.rb', line 36 def is_deleted? @deleted end |
#is_stored? ⇒ Boolean
32 33 34 |
# File 'lib/rails_uploads/types/file.rb', line 32 def is_stored? @stored end |
#path(*args) ⇒ Object
60 61 62 63 |
# File 'lib/rails_uploads/types/file.rb', line 60 def path(*args) return nil if is_deleted? is_stored? ? destination_path(*args) : @upload.path end |
#size(*args) ⇒ Object
45 46 47 48 |
# File 'lib/rails_uploads/types/file.rb', line 45 def size(*args) return 0 if is_deleted? storage.size path(*args) end |
#store ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/rails_uploads/types/file.rb', line 70 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
65 66 67 68 |
# File 'lib/rails_uploads/types/file.rb', line 65 def url(*args) return nil if is_deleted? or not is_stored? storage.url path(*args) end |