Class: Saviour::File
- Inherits:
-
Object
- Object
- Saviour::File
- Defined in:
- lib/saviour/file.rb
Instance Attribute Summary collapse
-
#persisted_path ⇒ Object
readonly
Returns the value of attribute persisted_path.
Instance Method Summary collapse
- #assign(object) ⇒ Object
- #blank? ⇒ Boolean
- #changed? ⇒ Boolean
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #filename ⇒ Object
- #filename_to_be_assigned ⇒ Object
-
#initialize(uploader_klass, model, attached_as) ⇒ File
constructor
A new instance of File.
- #persisted? ⇒ Boolean
- #public_url ⇒ Object (also: #url)
- #read ⇒ Object
- #set_path!(path) ⇒ Object
- #source_data ⇒ Object
- #with_copy ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(uploader_klass, model, attached_as) ⇒ File
7 8 9 10 |
# File 'lib/saviour/file.rb', line 7 def initialize(uploader_klass, model, attached_as) @uploader_klass, @model, @attached_as = uploader_klass, model, attached_as @source_was = @source = nil end |
Instance Attribute Details
#persisted_path ⇒ Object (readonly)
Returns the value of attribute persisted_path.
5 6 7 |
# File 'lib/saviour/file.rb', line 5 def persisted_path @persisted_path end |
Instance Method Details
#assign(object) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/saviour/file.rb', line 34 def assign(object) raise(RuntimeError, "must respond to `read`") if object && !object.respond_to?(:read) followers = @model.class.attached_followers_per_leader[@attached_as] followers.each { |x| @model.send(x).assign(object) unless @model.send(x).changed? } if followers @source_data = nil @source = object @persisted_path = nil if object object end |
#blank? ⇒ Boolean
97 98 99 |
# File 'lib/saviour/file.rb', line 97 def blank? !@source && !persisted? end |
#changed? ⇒ Boolean
51 52 53 |
# File 'lib/saviour/file.rb', line 51 def changed? @source_was != @source end |
#delete ⇒ Object
24 25 26 |
# File 'lib/saviour/file.rb', line 24 def delete persisted? && exists? && Config.storage.delete(@persisted_path) end |
#exists? ⇒ Boolean
16 17 18 |
# File 'lib/saviour/file.rb', line 16 def exists? persisted? && Config.storage.exists?(@persisted_path) end |
#filename ⇒ Object
55 56 57 |
# File 'lib/saviour/file.rb', line 55 def filename ::File.basename(@persisted_path) if persisted? end |
#filename_to_be_assigned ⇒ Object
77 78 79 |
# File 'lib/saviour/file.rb', line 77 def filename_to_be_assigned changed? ? (SourceFilenameExtractor.new(@source).detected_filename || SecureRandom.hex) : nil end |
#persisted? ⇒ Boolean
47 48 49 |
# File 'lib/saviour/file.rb', line 47 def persisted? !!@persisted_path end |
#public_url ⇒ Object Also known as: url
28 29 30 |
# File 'lib/saviour/file.rb', line 28 def public_url persisted? && Config.storage.public_url(@persisted_path) end |
#read ⇒ Object
20 21 22 |
# File 'lib/saviour/file.rb', line 20 def read persisted? && exists? && Config.storage.read(@persisted_path) end |
#set_path!(path) ⇒ Object
12 13 14 |
# File 'lib/saviour/file.rb', line 12 def set_path!(path) @persisted_path = path end |
#source_data ⇒ Object
90 91 92 93 94 95 |
# File 'lib/saviour/file.rb', line 90 def source_data @source_data ||= begin @source.rewind @source.read end end |
#with_copy ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/saviour/file.rb', line 59 def with_copy raise "must be persisted" unless persisted? Tempfile.open([::File.basename(filename, ".*"), ::File.extname(filename)]) do |file| begin file.binmode file.write(read) file.flush file.rewind yield(file) ensure file.close file.delete end end end |
#write ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/saviour/file.rb', line 81 def write raise(RuntimeError, "You must provide a source to read from first") unless @source path = uploader.write(source_data, filename_to_be_assigned) @source_was = @source @persisted_path = path path end |