class Blob
Blob
abstract model suitable for our typical kinds of data files in our app.
We can store the blob on the local file system (see BlobFile), We can make the blob accessible via URI (see BlobURI). We can export the blob to an external system (see BlobExport).
Blob
methods for accessing a blob via a dir name and base name. Exclusively for the Blob
class.
Blob
methods for exporting.
Exclusively for the Blob
class.
Blob
file methods for accessing a blob via the filesystem.
Exclusively for the Blob
class.
Blob
method to upload from a web page.
Exclusively for the Blob
class.
Blob
URI methods for accessing a blob via the web.
Exclusively for the Blob
class.
Public Class Methods
# File lib/sixarm_ruby_blob/base.rb, line 17 def initialize(options = {}) @name ||= options[:name] end
Upload to a file_path
from a web form file_field.
TODO optimize this to move the temp file into place
@return [Boolean] true iff the upload succeeds
# File lib/sixarm_ruby_blob/upload.rb, line 27 def self.upload(file_path, file_field) if vet_file_field?(file_field) file_field.tempfile.binmode File.open(file_path, "wb") { |f| f.write(file_field.read) } return true end return false end
Vet the file field for all the methods that we expect from a web browser upload; we call this before we upload.
# File lib/sixarm_ruby_blob/upload.rb, line 39 def self.vet_file_field?(file_field) !!( file_field \ && file_field.respond_to?(:tempfile) \ && file_field.tempfile \ && file_field.tempfile.respond_to?(:path) \ && file_field.tempfile.respond_to?(:binmode) \ && file_field.tempfile.path ) end
Public Instance Methods
# File lib/sixarm_ruby_blob/base.rb, line 21 def ==(other) self.name == other.name end
# File lib/sixarm_ruby_blob/dir.rb, line 16 def base; @base; end
# File lib/sixarm_ruby_blob/dir.rb, line 17 def base=(x); @base=x; end
attr_accessor :dir # Dir name of this blob, e.g. “/my/photos” attr_accessor :base # Base name of this blob, e.g. “photo.jpg”
# File lib/sixarm_ruby_blob/dir.rb, line 13 def dir; @dir; end
# File lib/sixarm_ruby_blob/dir.rb, line 14 def dir=(x); @dir=x; end
# File lib/sixarm_ruby_blob/base.rb, line 25 def eql?(other) self.name.eql?(other.name) end
Return true iff the image exists the way we expect. Currently, this is simply calling file_exist.
@deprecated @return [boolean] iff the file exists
# File lib/sixarm_ruby_blob/file.rb, line 73 def exist? Rails.logger.warn "DEPRECATED" FileTest.exists? file_path end
Get the blob export's base name, e.g. “photo.jpg”
The implementation simply calls file_base
. Override this when an export needs a custom base name.
@return [String] the blob export's base name
# File lib/sixarm_ruby_blob/export.rb, line 29 def export_base file_base end
Get the blob export's directory name, e.g. “/my/photos”
The implementation simply calls file_dir
. Override this when an export needs a custom directory base.
@return [String] the blob export's directory name
# File lib/sixarm_ruby_blob/export.rb, line 18 def export_dir file_dir end
Get the blob export's path, e.g. “/my/photos/photo.jpg”
The implementation simply calls file_path
. Override this when an export needs a custom path.
@return [String] the blob export's path
# File lib/sixarm_ruby_blob/export.rb, line 40 def export_path export_pathname.to_s end
Get the blob export's pathname, e.g. Pathname(“/my/photos/photo.jpg”)
The implementation simply calls file_pathname
. Override this when an export needs a custom path.
@return [String] the file's path suitable for export
# File lib/sixarm_ruby_blob/export.rb, line 51 def export_pathname Pathname(export_dir) + export_base end
# File lib/sixarm_ruby_blob/dir.rb, line 28 def ext base =~ /\.(\w+)$/ ? $1 : nil end
Get the blob file's base name e.g. “photo.jpg”.
This impl calls base
which is typically fine.
Override this when the local storage file base name is different than the generic base name.
@return [String] the blob file's base name
# File lib/sixarm_ruby_blob/file.rb, line 33 def file_base base end
Get the blob file's dir name e.g. “/my/photos”.
This impl calls dir
which is typically fine.
Override this if the local storage file dir name is different than the generic base name.
@return [String] the blob file's dir name
# File lib/sixarm_ruby_blob/file.rb, line 20 def file_dir dir end
Does the file exist on the local filesystem?
@return [boolean] iff the file exists
# File lib/sixarm_ruby_blob/file.rb, line 63 def file_exist? FileTest.exists? file_path end
# File lib/sixarm_ruby_blob/base.rb, line 14 def name; @name; end
# File lib/sixarm_ruby_blob/base.rb, line 15 def name=x; @name=x; end
Deprecated
# File lib/sixarm_ruby_blob/upload.rb, line 52 def save(file_field) raise "Deprecated: replace with #upload" end
Upload to this blob's file_path
from a web form file_field.
TODO optimize this to move the temp file into place
@return [Boolean] true iff the upload succeeds
# File lib/sixarm_ruby_blob/upload.rb, line 17 def upload(file_field) self.class.upload(file_path, file_field) end
Get the blob's URI base name e.g. “photo.jpg”.
This impl calls base
which is typically fine.
Override this when the URI base name is different than the generic base name.
@return [String] the blob URI's base name
# File lib/sixarm_ruby_blob/uri.rb, line 33 def uri_base base end
Get the blob's URI to access this blob from the web, with a random chaff query appended as a cache buster.
@return [String] the blob URI's path?uuid=chaff
# File lib/sixarm_ruby_blob/uri.rb, line 53 def uri_cacheless return "#{uri}?cacheless=#{SecureRandom.uuid}" end
Get the blob URI's dir name e.g. “/my/photos”.
This impl calls dir
which is typically fine.
Override this if the local storage file dir name is different than the generic base name.
@return [String] the blob URI's dir name
# File lib/sixarm_ruby_blob/uri.rb, line 20 def uri_dir dir end
Deprecated
# File lib/sixarm_ruby_blob/uri.rb, line 59 def url raise "Deprecated: replace with #uri" end
Deprecated
# File lib/sixarm_ruby_blob/uri.rb, line 65 def url_cacheless raise "Deprecated: replace with #uri_cacheless" end