class Gakubuchi::Template

Attributes

extname[R]
source_path[R]

Public Class Methods

all() click to toggle source
# File lib/gakubuchi/template.rb, line 22
def self.all
  ::Dir.glob(root.join("**/*.html*")).map { |source_path| new(source_path) }
end
new(source_path) click to toggle source
# File lib/gakubuchi/template.rb, line 30
def initialize(source_path)
  path = ::Pathname.new(source_path)
  root = self.class.root

  @extname = extract_extname(path)
  @source_path = path.absolute? ? path : root.join(path)

  case
  when !@extname.include?("html")
    raise ::Gakubuchi::Error::InvalidTemplate, "source path must refer to a template file"
  when !@source_path.fnmatch?(root.join("*").to_s)
    raise ::Gakubuchi::Error::InvalidTemplate, "template must exist in #{root}"
  end
end
root() click to toggle source
# File lib/gakubuchi/template.rb, line 26
def self.root
  ::Rails.root.join("app/assets", ::Gakubuchi.configuration.template_directory)
end

Public Instance Methods

destination_path() click to toggle source
# File lib/gakubuchi/template.rb, line 45
def destination_path
  ::Rails.public_path.join(logical_path)
end
digest_path() click to toggle source
# File lib/gakubuchi/template.rb, line 49
def digest_path
  # NOTE: Call #to_s for Sprockets 4 or later
  asset = assets.find_asset(logical_path.to_s)
  return if asset.nil?

  ::Pathname.new(::File.join(::Rails.public_path, app.config.assets.prefix, asset.digest_path))
end
logical_path() click to toggle source
# File lib/gakubuchi/template.rb, line 57
def logical_path
  dirname = source_path.relative_path_from(self.class.root).dirname
  ::Pathname.new(dirname).join("#{basename(extname)}.html")
end

Private Instance Methods

app() click to toggle source
# File lib/gakubuchi/template.rb, line 64
def app
  ::Rails.application
end
assets() click to toggle source

TODO: Cache @assets by Gakubuchi::Task instance because to call find_asset for the first time takes much time and would cause performance problem.

# File lib/gakubuchi/template.rb, line 70
def assets
  return @assets if @assets

  @assets = app.assets || ::Sprockets::Railtie.build_environment(app)
  @assets = @assets.cached if @assets.respond_to?(:cached)
  @assets
end
extract_extname(path) click to toggle source
# File lib/gakubuchi/template.rb, line 78
def extract_extname(path)
  extname = path.extname
  extname.empty? || extname == ".html" ?
    extname : "#{extract_extname(path.basename(extname))}#{extname}"
end