Class: Forward::Static::TemplateContext

Inherits:
Object
  • Object
show all
Defined in:
lib/forward/static/app.rb

Constant Summary collapse

FILESIZE_FORMAT =
[
  ['%.1fT', 1 << 40],
  ['%.1fG', 1 << 30],
  ['%.1fM', 1 << 20],
  ['%.1fK', 1 << 10],
]

Instance Method Summary collapse

Constructor Details

#initialize(root, path_info) ⇒ TemplateContext

Returns a new instance of TemplateContext.



15
16
17
18
19
20
# 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

Instance Method Details

#filesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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_bindingObject



56
57
58
# File 'lib/forward/static/app.rb', line 56

def get_binding
  binding
end

#path_componentsObject



22
23
24
# File 'lib/forward/static/app.rb', line 22

def path_components
  @path_info.split('/')[1..-1]
end