class VueCK::FileManager

Public Class Methods

new(filename) click to toggle source
# File lib/filemanager.rb, line 3
def initialize(filename)
    @filename  = filename
    @js_cache  = "#{DIRS[:cache]}#{FILES[:javascript]}"
    @css_cache = "#{DIRS[:cache]}#{FILES[:style]}"
end

Public Instance Methods

serve() click to toggle source
# File lib/filemanager.rb, line 9
def serve
    return render unless cache_valid?
    read_file
end

Private Instance Methods

cache_valid?() click to toggle source
# File lib/filemanager.rb, line 16
def cache_valid?
    cachedir_exists? and cachefiles_exist? and cachefiles_fresh?
end
cachedir_exists?() click to toggle source
# File lib/filemanager.rb, line 33
def cachedir_exists?
    return true if File.exists? DIRS[:cache]
    Dir.mkdir DIRS[:cache]
    false
end
cachefiles_exist?() click to toggle source
# File lib/filemanager.rb, line 39
def cachefiles_exist?
    return File.exists?(@js_cache) && File.exists?(@css_cache)
end
cachefiles_fresh?() click to toggle source
# File lib/filemanager.rb, line 43
def cachefiles_fresh?
    cache_mtime  = newest_file_in_dir(DIRS[:cache])
    source_mtime = newest_file_in_dir(DIRS[:components])
    return cache_mtime > source_mtime
end
newest_file_in_dir(path) click to toggle source
# File lib/filemanager.rb, line 49
def newest_file_in_dir(path)
    Dir.glob(File.join(path, '*.*')).max { |a,b| File.mtime(a) <=> File.mtime(b) }
end
read_file() click to toggle source
# File lib/filemanager.rb, line 27
def read_file
    cache_file = @js_cache if @filename == FILES[:javascript]
    cache_file = @css_cache if @filename == FILES[:style]
    File.read(cache_file)
end
render() click to toggle source
# File lib/filemanager.rb, line 20
def render
    renders = BatchRenderer.new.render
    File.write(@js_cache,  renders[:components])
    File.write(@css_cache, renders[:style])
    read_file
end