Class: Attachs::Types::File
- Inherits:
-
Object
- Object
- Attachs::Types::File
- Defined in:
- lib/attachs/types/file.rb
Direct Known Subclasses
Instance Method Summary collapse
- #default? ⇒ Boolean
- #delete ⇒ Object
- #deleted? ⇒ Boolean
- #exists?(*args) ⇒ Boolean
- #extname ⇒ Object
- #filename ⇒ Object
-
#initialize(source, options = {}) ⇒ File
constructor
A new instance of File.
- #original_filename ⇒ Object
- #path(*args) ⇒ Object
- #realpath(*args) ⇒ Object
- #size(*args) ⇒ Object
- #store ⇒ Object
- #stored? ⇒ Boolean
- #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/attachs/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
#default? ⇒ Boolean
28 29 30 |
# File 'lib/attachs/types/file.rb', line 28 def default? @default == true end |
#delete ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/attachs/types/file.rb', line 90 def delete if not default? and stored? and exists? storage.delete path yield if block_given? @storage = build_storage(:local) if upload? @stored = false @deleted = true end end |
#deleted? ⇒ Boolean
36 37 38 |
# File 'lib/attachs/types/file.rb', line 36 def deleted? @deleted == true end |
#exists?(*args) ⇒ Boolean
40 41 42 43 |
# File 'lib/attachs/types/file.rb', line 40 def exists?(*args) return false if deleted? storage.exists? path(*args) end |
#extname ⇒ Object
50 51 52 53 |
# File 'lib/attachs/types/file.rb', line 50 def extname return nil if deleted? @extname ||= ::File.extname(filename) end |
#filename ⇒ Object
55 56 57 58 |
# File 'lib/attachs/types/file.rb', line 55 def filename return nil if deleted? @filename ||= "#{(Time.now.to_f * 10000000).to_i}#{::File.extname upload.original_filename}".downcase end |
#original_filename ⇒ Object
60 61 62 63 |
# File 'lib/attachs/types/file.rb', line 60 def original_filename return nil unless upload.present? upload.original_filename end |
#path(*args) ⇒ Object
65 66 67 68 |
# File 'lib/attachs/types/file.rb', line 65 def path(*args) return nil if deleted? (stored? ? destination_path(*args) : upload.path).to_s end |
#realpath(*args) ⇒ Object
70 71 72 73 |
# File 'lib/attachs/types/file.rb', line 70 def realpath(*args) return nil if deleted? or Rails.application.config.attachs.storage == :s3 (stored? ? storage.realpath(path) : upload.path).to_s end |
#size(*args) ⇒ Object
45 46 47 48 |
# File 'lib/attachs/types/file.rb', line 45 def size(*args) return 0 if deleted? storage.size path(*args) end |
#store ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/attachs/types/file.rb', line 80 def store if not stored? and upload? @storage = build_storage storage.store upload, destination_path yield if block_given? @stored = true @deleted = false end end |
#stored? ⇒ Boolean
32 33 34 |
# File 'lib/attachs/types/file.rb', line 32 def stored? @stored == true end |
#url(*args) ⇒ Object
75 76 77 78 |
# File 'lib/attachs/types/file.rb', line 75 def url(*args) return nil if deleted? or not stored? storage.url(path(*args)).to_s end |