Class: Jekyll::StaticFile

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/static_file.rb

Constant Summary collapse

@@mtimes =

The cache of last modification times [path] -> mtime.

Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, base, dir, name) ⇒ StaticFile

Initialize a new StaticFile.

site - The Site. base - The String path to the <source>. dir - The String path between <source> and the file. name - The String filename of the file.



13
14
15
16
17
18
# File 'lib/jekyll/static_file.rb', line 13

def initialize(site, base, dir, name)
  @site = site
  @base = base
  @dir  = dir
  @name = name
end

Class Method Details

.reset_cacheObject

Reset the mtimes cache (for testing purposes).

Returns nothing.



71
72
73
74
# File 'lib/jekyll/static_file.rb', line 71

def self.reset_cache
  @@mtimes = Hash.new
  nil
end

Instance Method Details

#destination(dest) ⇒ Object

Obtain destination path.

dest - The String path to the destination dir.

Returns destination file path.



35
36
37
# File 'lib/jekyll/static_file.rb', line 35

def destination(dest)
  File.join(dest, @dir, @name)
end

#inspectObject



26
27
28
# File 'lib/jekyll/static_file.rb', line 26

def inspect
  "<StaticFile: #{self.path}>"
end

#modified?Boolean

Is source path modified?

Returns true if modified since last write.

Returns:

  • (Boolean)


47
48
49
# File 'lib/jekyll/static_file.rb', line 47

def modified?
  @@mtimes[path] != mtime
end

#mtimeObject

Returns last modification time for this file.



40
41
42
# File 'lib/jekyll/static_file.rb', line 40

def mtime
  File.stat(path).mtime.to_i
end

#pathObject Also known as: filename

Returns source file path.



21
22
23
# File 'lib/jekyll/static_file.rb', line 21

def path
  File.join(@base, @dir, @name)
end

#write(dest) ⇒ Object

Write the static file to the destination directory (if modified).

dest - The String path to the destination dir.

Returns false if the file was not modified since last time (no-op).



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll/static_file.rb', line 56

def write(dest)
  dest_path = destination(dest)

  return false if File.exist?(dest_path) and !modified?
  @@mtimes[path] = mtime

  FileUtils.mkdir_p(File.dirname(dest_path))
  FileUtils.cp(path, dest_path)

  true
end