class Forward::Static::TemplateContext

Constants

FILESIZE_FORMAT

Public Class Methods

new(root, path_info) click to toggle source
# File lib/forward/static/app.rb, line 15
def initialize(root, path_info)
  @root         = root
  @project_name = File.basename(root)
  @path_info    = path_info
  @path         = File.join(root, path_info)
end

Public Instance Methods

files() click to toggle source
# File lib/forward/static/app.rb, line 26
def files
  _files = []

  Dir["#{@path}*"].sort.each do |path|
    _stat = stat(path)
    next if _stat.nil?

    basename = File.basename(path)
    url      = path.sub(@root, '')
    ext      = File.extname(path)
    size     = _stat.size
    type     = _stat.directory? ? 'directory' : Rack::Mime.mime_type(ext)
    size     = _stat.directory? ? '-' : filesize_format(size)

    if _stat.directory?
      url      << '/'
      basename << '/'
    end

    _files << {
      url: url,
      basename: basename,
      size: size,
      type: type,
    }
  end

  _files
end
get_binding() click to toggle source
# File lib/forward/static/app.rb, line 56
def get_binding
  binding
end
path_components() click to toggle source
# File lib/forward/static/app.rb, line 22
def path_components
  @path_info.split('/')[1..-1]
end

Private Instance Methods

filesize_format(int) click to toggle source
# File lib/forward/static/app.rb, line 68
def filesize_format(int)
  FILESIZE_FORMAT.each do |format, size|
    return format % (int.to_f / size) if int >= size
  end

  int.to_s + 'B'
end
stat(path) click to toggle source
# File lib/forward/static/app.rb, line 62
def stat(path)
  File.stat(path)
rescue Errno::ENOENT, Errno::ELOOP
  nil
end