Sha256: 204486ed821ed6f1d8ac5faf9e5b69c4457c0ca35e20d680c0b70b8200212e62

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

class Servel::PathnameDecorator < SimpleDelegator
  def initialize(pathname:, parent: false, top: false)
    super(pathname)
    @parent = parent
    @top = top
  end

  def image?
    file? && extname && %w(.jpg .jpeg .png .gif).include?(extname.downcase)
  end

  def video?
    file? && extname && %w(.webm .mp4).include?(extname.downcase)
  end

  def audio?
    file? && extname && %w(.mp3 .m4a .wav).include?(extname.downcase)
  end

  def media?
    image? || video? || audio?
  end

  def type
    if directory?
      "Dir"
    elsif file?
      extname.sub(/^\./, "")
    else
      ""
    end
  end

  def media_type
    return "video" if video?
    return "image" if image?
    return "audio" if audio?
    "unknown"
  end

  def listing_classes
    klasses = []
    klasses << "media" if media?
    klasses << "image" if image?
    klasses << "video" if video?
    klasses << "audio" if audio?
    klasses << "top" if top?
    klasses << "parent" if parent?
    klasses << "file" if file?
    klasses << "directory" if directory?
    klasses.join(" ")
  end

  def listing_attrs
    {
      class: listing_classes,
      data: {
        type: media_type
      }
    }
  end

  def top?
    @top
  end

  def parent?
    @parent
  end

  def virtual?
    top? || parent?
  end

  def icon
    if @top
      "🔝"
    elsif @parent
      "⬆️"
    elsif directory?
      "📁"
    elsif video?
      "🎞️"
    elsif image?
      "🖼️"
    elsif audio?
      "🔊"
    else
      "📝"
    end
  end

  def href
    if @top || @parent
      to_s
    else
      basename
    end
  end

  def name
    if @top
      "Top Directory"
    elsif @parent
      "Parent Directory"
    else
      basename
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
servel-0.5.0 lib/servel/pathname_decorator.rb