Class: Stic::Blob

Inherits:
Object
  • Object
show all
Includes:
SiteBase
Defined in:
lib/stic/blob.rb

Overview

Stic::Blob

A Blob represents a single output file. It does not assume any specific source or processing.

Blob provides the basic functionality Site expects for managing and writing blobs. Blob should not be used directly but sub-classed to provide an actual implementation of at least #url_template and #render.

See File for a simple blob implementation serving a static file or Page for a more future rich example including rendering.

Direct Known Subclasses

File

Instance Attribute Summary

Attributes included from SiteBase

#data, #site

Accessors collapse

Actions collapse

Methods included from SiteBase

#initialize

Instance Method Details

#mime_typeMIME::Type

Return the target file MIME type.

The MIME type is determined using the #relative_target_path but can be overridden by subclasses.

The MIME type is used for post-processing.

Returns:

  • (MIME::Type)

    MIME type.



78
79
80
81
# File 'lib/stic/blob.rb', line 78

def mime_type
  types = MIME::Types.of relative_target_path.to_s
  types.first
end

#relative_target_pathPath

Return the site relative target path.

The relative target path must be based on the URL.

The default implementation joins the relative URL with ‘index.html` if the relative URL does not have any file extension. Otherwise the relative URL will be returned.

Returns:

  • (Path)

    Target file path relative to site target path.



49
50
51
52
53
54
55
# File 'lib/stic/blob.rb', line 49

def relative_target_path
  if relative_url.extensions.empty?
    relative_url.join('index.html')
  else
    relative_url
  end
end

#relative_urlPath

Return a the relative URL the blob should have in generated site. The URL is based on the URL template where placeholders are replaced with values from SiteBase#data.

Example: Given the URL template ‘/blog/:year/:slug.html` and a blob data hash of `=> 2014, :slug => ’a-blog-post’‘ the resulting relative URL would be ’/blog/2014/a-blog-post.html’. Only ‘A-z0-9_` are allowed as a placeholder.

Returns:

  • (Path)

    URL relative to site root but as an absolute path.



32
33
34
35
36
37
# File 'lib/stic/blob.rb', line 32

def relative_url
  parts = url_template.each_component(empty: true).map do |fn|
    fn =~ /^:([A-z0-9_]+)$/ ? data[$1] : fn
  end
  Path '/', parts
end

#target_pathPathname

Return full target path.

The target path is based on the site target dir and the relative target path. You should not override this method. Instead provide a custom relative target path or URL.

Returns:

  • (Pathname)

    Fill blob file path.



65
66
67
# File 'lib/stic/blob.rb', line 65

def target_path
  site.target.join relative_target_path.as_relative
end

#to_sObject



102
103
104
# File 'lib/stic/blob.rb', line 102

def to_s
  "#<#{self.class.name}:#{object_id} #{relative_target_path}>"
end

#writeObject

Write rendered blob to file.

Path from #target_path will be used as the file path.

You should not override this method. Instead provide a custom #render method to customize written result.



92
93
94
95
96
97
98
99
100
# File 'lib/stic/blob.rb', line 92

def write
  unless ::File.directory?((dir = ::File.dirname(target_path)))
    FileUtils.mkdir_p dir
  end

  ::File.open target_path, 'w' do |file|
    file.write render
  end
end