Class: Bunto::StaticFile
- Inherits:
-
Object
- Object
- Bunto::StaticFile
- Defined in:
- lib/bunto/static_file.rb
Instance Attribute Summary collapse
-
#extname ⇒ Object
readonly
Returns the value of attribute extname.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#relative_path ⇒ Object
readonly
Returns the value of attribute relative_path.
Class Method Summary collapse
-
.mtimes ⇒ Object
The cache of last modification times [path] -> mtime.
- .reset_cache ⇒ Object
Instance Method Summary collapse
- #basename ⇒ Object
- #data ⇒ Object
-
#defaults ⇒ Object
Returns the front matter defaults defined for the file’s URL and/or type as defined in _config.yml.
-
#destination(dest) ⇒ Object
Obtain destination path.
- #destination_rel_dir ⇒ Object
-
#initialize(site, base, dir, name, collection = nil) ⇒ StaticFile
constructor
Initialize a new StaticFile.
-
#modified? ⇒ Boolean
Is source path modified?.
- #modified_time ⇒ Object
-
#mtime ⇒ Object
Returns last modification time for this file.
-
#path ⇒ Object
Returns source file path.
- #placeholders ⇒ Object
- #to_liquid ⇒ Object
-
#type ⇒ Object
Returns the type of the collection if present, nil otherwise.
-
#url ⇒ Object
Applies a similar URL-building technique as Bunto::Document that takes the collection’s URL template into account.
-
#write(dest) ⇒ Object
Write the static file to the destination directory (if modified).
-
#write? ⇒ Boolean
Whether to write the file to the filesystem.
Constructor Details
#initialize(site, base, dir, name, collection = nil) ⇒ 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. rubocop: disable ParameterLists
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bunto/static_file.rb', line 23 def initialize(site, base, dir, name, collection = nil) @site = site @base = base @dir = dir @name = name @collection = collection @relative_path = File.join(*[@dir, @name].compact) @extname = File.extname(@name) data.default_proc = proc do |_, key| site.frontmatter_defaults.find(relative_path, type, key) end end |
Instance Attribute Details
#extname ⇒ Object (readonly)
Returns the value of attribute extname.
3 4 5 |
# File 'lib/bunto/static_file.rb', line 3 def extname @extname end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/bunto/static_file.rb', line 3 def name @name end |
#relative_path ⇒ Object (readonly)
Returns the value of attribute relative_path.
3 4 5 |
# File 'lib/bunto/static_file.rb', line 3 def relative_path @relative_path end |
Class Method Details
.mtimes ⇒ Object
The cache of last modification times [path] -> mtime.
7 8 9 |
# File 'lib/bunto/static_file.rb', line 7 def mtimes @mtimes ||= {} end |
.reset_cache ⇒ Object
11 12 13 |
# File 'lib/bunto/static_file.rb', line 11 def reset_cache @mtimes = nil end |
Instance Method Details
#basename ⇒ Object
110 111 112 |
# File 'lib/bunto/static_file.rb', line 110 def basename File.basename(name, extname) end |
#data ⇒ Object
106 107 108 |
# File 'lib/bunto/static_file.rb', line 106 def data @data ||= {} end |
#defaults ⇒ Object
Returns the front matter defaults defined for the file’s URL and/or type as defined in _config.yml.
146 147 148 |
# File 'lib/bunto/static_file.rb', line 146 def defaults @defaults ||= @site.frontmatter_defaults.all url, type end |
#destination(dest) ⇒ Object
Obtain destination path.
dest - The String path to the destination dir.
Returns destination file path.
48 49 50 |
# File 'lib/bunto/static_file.rb', line 48 def destination(dest) @site.in_dest_dir(*[dest, destination_rel_dir, @name].compact) end |
#destination_rel_dir ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/bunto/static_file.rb', line 52 def destination_rel_dir if @collection File.dirname(url) else @dir end end |
#modified? ⇒ Boolean
Is source path modified?
Returns true if modified since last write.
72 73 74 |
# File 'lib/bunto/static_file.rb', line 72 def modified? self.class.mtimes[path] != mtime end |
#modified_time ⇒ Object
60 61 62 |
# File 'lib/bunto/static_file.rb', line 60 def modified_time @modified_time ||= File.stat(path).mtime end |
#mtime ⇒ Object
Returns last modification time for this file.
65 66 67 |
# File 'lib/bunto/static_file.rb', line 65 def mtime modified_time.to_i end |
#path ⇒ Object
Returns source file path.
39 40 41 |
# File 'lib/bunto/static_file.rb', line 39 def path File.join(*[@base, @dir, @name].compact) end |
#placeholders ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bunto/static_file.rb', line 114 def placeholders { :collection => @collection.label, :path => relative_path[ @collection.relative_directory.size..relative_path.size], :output_ext => "", :name => "", :title => "", } end |
#to_liquid ⇒ Object
102 103 104 |
# File 'lib/bunto/static_file.rb', line 102 def to_liquid @to_liquid ||= Drops::StaticFileDrop.new(self) end |
#type ⇒ Object
Returns the type of the collection if present, nil otherwise.
140 141 142 |
# File 'lib/bunto/static_file.rb', line 140 def type @type ||= @collection.nil? ? nil : @collection.label.to_sym end |
#url ⇒ Object
Applies a similar URL-building technique as Bunto::Document that takes the collection’s URL template into account. The default URL template can be overriden in the collection’s configuration in _config.yml.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bunto/static_file.rb', line 128 def url @url ||= if @collection.nil? relative_path else ::Bunto::URL.new({ :template => @collection.url_template, :placeholders => placeholders, }) end.to_s.gsub(%r!/$!, "") 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).
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bunto/static_file.rb', line 89 def write(dest) dest_path = destination(dest) return false if File.exist?(dest_path) && !modified? self.class.mtimes[path] = mtime FileUtils.mkdir_p(File.dirname(dest_path)) FileUtils.rm(dest_path) if File.exist?(dest_path) copy_file(dest_path) true end |
#write? ⇒ Boolean
Whether to write the file to the filesystem
Returns true unless the defaults for the destination path from _config.yml contain ‘published: false`.
80 81 82 |
# File 'lib/bunto/static_file.rb', line 80 def write? defaults.fetch("published", true) end |