class Refile::Attacher
@api private
Constants
- Presence
Attributes
definition[R]
errors[R]
record[R]
remove[RW]
Public Class Methods
new(definition, record)
click to toggle source
# File lib/refile/attacher.rb, line 9 def initialize(definition, record) @definition = definition @record = record @errors = [] @metadata = {} end
Public Instance Methods
basename()
click to toggle source
# File lib/refile/attacher.rb, line 48 def basename if filename and extension ::File.basename(filename, "." << extension) else filename end end
cache()
click to toggle source
# File lib/refile/attacher.rb, line 20 def cache @definition.cache end
cache!(uploadable)
click to toggle source
# File lib/refile/attacher.rb, line 93 def cache!(uploadable) @metadata = { size: uploadable.size, content_type: Refile.extract_content_type(uploadable), filename: Refile.extract_filename(uploadable) } if valid? @metadata[:id] = cache.upload(uploadable).id write_metadata elsif @definition.raise_errors? raise Refile::Invalid, @errors.join(", ") end end
cache_id()
click to toggle source
# File lib/refile/attacher.rb, line 44 def cache_id Presence[@metadata[:id]] end
content_type()
click to toggle source
# File lib/refile/attacher.rb, line 40 def content_type Presence[@metadata[:content_type] || read(:content_type)] end
data()
click to toggle source
# File lib/refile/attacher.rb, line 155 def data @metadata if valid? end
delete!()
click to toggle source
# File lib/refile/attacher.rb, line 141 def delete! cache.delete(cache_id) if cache_id store.delete(id) if id @metadata = {} end
download(url)
click to toggle source
# File lib/refile/attacher.rb, line 107 def download(url) unless url.to_s.empty? download = Refile::Download.new(url) @metadata = { size: download.size, filename: download.original_filename, content_type: download.content_type } if valid? @metadata[:id] = cache.upload(download.io).id write_metadata elsif @definition.raise_errors? raise Refile::Invalid, @errors.join(", ") end end rescue Refile::Error @errors = [:download_failed] raise if @definition.raise_errors? end
extension()
click to toggle source
# File lib/refile/attacher.rb, line 56 def extension if filename Presence[::File.extname(filename).sub(/^\./, "")] elsif content_type type = MIME::Types[content_type][0] type.extensions[0] if type end end
filename()
click to toggle source
# File lib/refile/attacher.rb, line 36 def filename Presence[@metadata[:filename] || read(:filename)] end
get()
click to toggle source
# File lib/refile/attacher.rb, line 65 def get if remove? nil elsif cache_id cache.get(cache_id) elsif id store.get(id) end end
id()
click to toggle source
# File lib/refile/attacher.rb, line 28 def id Presence[read(:id, true)] end
name()
click to toggle source
# File lib/refile/attacher.rb, line 16 def name @definition.name end
present?()
click to toggle source
# File lib/refile/attacher.rb, line 151 def present? not @metadata.empty? end
remove?()
click to toggle source
# File lib/refile/attacher.rb, line 147 def remove? remove and remove != "" and remove !~ /\A0|false$\z/ end
retrieve!(value)
click to toggle source
# File lib/refile/attacher.rb, line 84 def retrieve!(value) if value.is_a?(String) @metadata = Refile.parse_json(value, symbolize_names: true) || {} elsif value.is_a?(Hash) @metadata = value end write_metadata if cache_id end
set(value)
click to toggle source
# File lib/refile/attacher.rb, line 75 def set(value) self.remove = false case value when nil then self.remove = true when String, Hash then retrieve!(value) else cache!(value) end end
size()
click to toggle source
# File lib/refile/attacher.rb, line 32 def size Presence[@metadata[:size] || read(:size)] end
store()
click to toggle source
# File lib/refile/attacher.rb, line 24 def store @definition.store end
store!()
click to toggle source
# File lib/refile/attacher.rb, line 127 def store! if remove? delete! write(:id, nil, true) remove_metadata elsif cache_id file = store.upload(get) delete! write(:id, file.id, true) write_metadata end @metadata = {} end
valid?()
click to toggle source
# File lib/refile/attacher.rb, line 159 def valid? @errors = @definition.validate(self) @errors.empty? end
Private Instance Methods
read(column, strict = false)
click to toggle source
# File lib/refile/attacher.rb, line 166 def read(column, strict = false) m = "#{name}_#{column}" value ||= record.send(m) if strict or record.respond_to?(m) value end
remove_metadata()
click to toggle source
# File lib/refile/attacher.rb, line 184 def remove_metadata write(:size, nil) write(:content_type, nil) write(:filename, nil) end
write(column, value, strict = false)
click to toggle source
# File lib/refile/attacher.rb, line 172 def write(column, value, strict = false) return if record.frozen? m = "#{name}_#{column}=" record.send(m, value) if strict or record.respond_to?(m) end
write_metadata()
click to toggle source
# File lib/refile/attacher.rb, line 178 def write_metadata write(:size, size) write(:content_type, content_type) write(:filename, filename) end