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

new(options = {}) click to toggle source
# File lib/sixarm_ruby_blob/base.rb, line 17
def initialize(options = {})
  @name ||= options[:name]
end
upload(file_path, file_field) click to toggle source

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_file_field?(file_field) click to toggle source

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

==(other) click to toggle source
# File lib/sixarm_ruby_blob/base.rb, line 21
def ==(other)
  self.name == other.name
end
base() click to toggle source
# File lib/sixarm_ruby_blob/dir.rb, line 16
def base; @base;  end
base=(x) click to toggle source
# File lib/sixarm_ruby_blob/dir.rb, line 17
def base=(x); @base=x; end
dir() click to toggle source

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
dir=(x) click to toggle source
# File lib/sixarm_ruby_blob/dir.rb, line 14
def dir=(x); @dir=x; end
eql?(other) click to toggle source
# File lib/sixarm_ruby_blob/base.rb, line 25
def eql?(other)
  self.name.eql?(other.name)
end
exist?() click to toggle source

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
export_base() click to toggle source

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
export_dir() click to toggle source

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
export_path() click to toggle source

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
export_pathname() click to toggle source

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
ext() click to toggle source
# File lib/sixarm_ruby_blob/dir.rb, line 28
def ext
  base =~ /\.(\w+)$/ ? $1 : nil
end
file_base() click to toggle source

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
file_dir() click to toggle source

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
file_exist?() click to toggle source

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_path() click to toggle source

Get the blob file's path e.g. “/my/photos/photo.jpg”

This impl calls file_dir and file_base. Subclasses can likely use this as-is.

@return [Pathname] the blob file's path

# File lib/sixarm_ruby_blob/file.rb, line 44
def file_path
  file_pathname.to_s
end
file_pathname() click to toggle source

Get the blob file's pathname e.g. Pathname(“/my/photos/photo.jpg”)

This impl calls file_dir and file_base. Subclasses can likely use this as-is.

@return [Pathname] the blob file's pathname

# File lib/sixarm_ruby_blob/file.rb, line 55
def file_pathname
  Pathname(file_dir) + file_base
end
name() click to toggle source
# File lib/sixarm_ruby_blob/base.rb, line 14
def name; @name; end
name=(x;) click to toggle source
# File lib/sixarm_ruby_blob/base.rb, line 15
def name=x; @name=x; end
save(file_field) click to toggle source

Deprecated

# File lib/sixarm_ruby_blob/upload.rb, line 52
def save(file_field)
  raise "Deprecated: replace with #upload"
end
upload(file_field) click to toggle source

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
uri() click to toggle source

Get the blob's URI to access this blob from the web.

This impl calls uri_dir and uri_base. Override this e.g. for assets, CDNs, etc.

@return [String] the blob URI's path

# File lib/sixarm_ruby_blob/uri.rb, line 44
def uri
  return "/#{uri_dir}/#{uri_base}"
end
uri_base() click to toggle source

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
uri_cacheless() click to toggle source

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
uri_dir() click to toggle source

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
url() click to toggle source

Deprecated

# File lib/sixarm_ruby_blob/uri.rb, line 59
def url
  raise "Deprecated: replace with #uri"
end
url_cacheless() click to toggle source

Deprecated

# File lib/sixarm_ruby_blob/uri.rb, line 65
def url_cacheless
  raise "Deprecated: replace with #uri_cacheless"
end