class Hanmoto::Task

Constants

EXTENSIONS

Public Class Methods

new(view_dir:, layouts: {}) click to toggle source
# File lib/hanmoto/task.rb, line 13
def initialize(view_dir:, layouts: {})
  @view_dir = view_dir
  @layouts = layouts
end
run(**args) click to toggle source
# File lib/hanmoto/task.rb, line 9
def self.run(**args)
  new(**args).run
end

Public Instance Methods

run() click to toggle source
# File lib/hanmoto/task.rb, line 18
def run
  Dir.foreach(Rails.root.join('app', 'views', @view_dir)) do |file|
    next if file.start_with?('.')
    name, format = file.split('.', 3)
    layout = @layouts[format.to_sym]
    body = ApplicationController.renderer.render("#{@view_dir}/#{name}", layout: layout)
    path = output_path(name, format)
    File.write(path, body)
    ::Logger.new(::STDOUT).info("Writing #{path}")
  end
end

Private Instance Methods

ext_by_format(format) click to toggle source
# File lib/hanmoto/task.rb, line 36
def ext_by_format(format)
  EXTENSIONS.fetch(format.to_sym, format)
end
output_path(name, format) click to toggle source
# File lib/hanmoto/task.rb, line 32
def output_path(name, format)
  Rails.public_path.join([name, ext_by_format(format)].join('.'))
end