class Servel::EntryFactory

Constants

AUDIO_EXTS
IMAGE_EXTS
TEXT_EXTS
VIDEO_EXTS

Public Class Methods

for(path) click to toggle source
# File lib/servel/entry_factory.rb, line 42
def self.for(path)
  new(path).entry
rescue Errno::EACCES
  nil
end
home(href) click to toggle source
# File lib/servel/entry_factory.rb, line 9
def self.home(href)
  Servel::Entry.new(
    ftype: :directory,
    type: "Dir",
    listing_classes: "home directory",
    icon: "🏠",
    href: href,
    name: "Listings Home"
  )
end
new(path) click to toggle source
# File lib/servel/entry_factory.rb, line 48
def initialize(path)
  @path_basename = path.basename.to_s
  @path_extname = path.extname.to_s
  stat = path.stat
  @path_mtime = stat.mtime
  @path_size = stat.size
  @path_ftype = stat.ftype.intern
  @path_directory = @path_ftype == :directory
  @path_file = @path_ftype == :file
end
parent(href) click to toggle source
# File lib/servel/entry_factory.rb, line 31
def self.parent(href)
  Servel::Entry.new(
    ftype: :directory,
    type: "Dir",
    listing_classes: "parent directory",
    icon: "⬆️",
    href: href,
    name: "Parent Directory"
  )
end
top(href) click to toggle source
# File lib/servel/entry_factory.rb, line 20
def self.top(href)
  Servel::Entry.new(
    ftype: :directory,
    type: "Dir",
    listing_classes: "top directory",
    icon: "🔝",
    href: href,
    name: "Top Directory"
  )
end

Public Instance Methods

entry() click to toggle source
# File lib/servel/entry_factory.rb, line 61
def entry
  Servel::Entry.new(
    ftype: @path_ftype,
    type: type,
    media_type: media_type,
    listing_classes: listing_classes,
    icon: icon,
    href: @path_basename,
    name: @path_basename,
    size: size,
    mtime: @path_mtime
  )
end
icon() click to toggle source
# File lib/servel/entry_factory.rb, line 111
def icon
  return "📁" if @path_directory
  case media_type
  when :video
    "🎞️"
  when :image
    "🖼️"
  when :audio
    "🔊"
  else
    "📝"
  end
end
listing_classes() click to toggle source
# File lib/servel/entry_factory.rb, line 102
def listing_classes
  klasses = []
  klasses << "file" if @path_file
  klasses << "directory" if @path_directory
  klasses << "media" if media_type
  klasses << media_type if media_type
  klasses.join(" ")
end
media_type() click to toggle source
# File lib/servel/entry_factory.rb, line 85
def media_type
  return nil unless @path_file && @path_extname

  case @path_extname.downcase
  when *IMAGE_EXTS
    :image
  when *VIDEO_EXTS
    :video
  when *AUDIO_EXTS
    :audio
  when *TEXT_EXTS
    :text
  else
    nil
  end
end
size() click to toggle source
# File lib/servel/entry_factory.rb, line 125
def size
  @path_directory ? nil : @path_size
end
type() click to toggle source
# File lib/servel/entry_factory.rb, line 75
def type
  if @path_directory
    "Dir"
  elsif @path_file
    @path_extname.sub(/^\./, "")
  else
    ""
  end
end