class Shaf::Generator::Base

Attributes

args[R]
options[R]

Public Class Methods

identifier(*ids) click to toggle source
# File lib/shaf/generator/base.rb, line 18
def identifier(*ids)
  @identifiers = ids.flatten
end
inherited(child) click to toggle source
# File lib/shaf/generator/base.rb, line 14
def inherited(child)
  Factory.register(child)
end
new(*args, **options) click to toggle source
# File lib/shaf/generator/base.rb, line 29
def initialize(*args, **options)
  @args = args
  @options = options
end
options(option_parser, options) click to toggle source
# File lib/shaf/generator/base.rb, line 26
def options(option_parser, options); end
usage(str = nil, &block) click to toggle source
# File lib/shaf/generator/base.rb, line 22
def usage(str = nil, &block)
  @usage = str || block
end

Public Instance Methods

call() click to toggle source
# File lib/shaf/generator/base.rb, line 34
def call; end
read_template(file, directory = nil) click to toggle source
# File lib/shaf/generator/base.rb, line 40
def read_template(file, directory = nil)
  directory ||= template_dir
  filename = File.join(directory, file)
  filename << '.erb' unless filename.end_with?('.erb')
  File.read(filename)
end
render(template, locals = {}) click to toggle source
# File lib/shaf/generator/base.rb, line 47
def render(template, locals = {})
  str = read_template(template)
  locals[:changes] ||= []
  b = Helper.new(locals).binding

  return ERB.new(str, 0, '%-<>').result(b) if RUBY_VERSION < '2.6.0'
  ERB.new(str, trim_mode: '-<>').result(b)
rescue SystemCallError => e
  puts "Failed to render template #{template}: #{e.message}"
  raise
end
template_dir() click to toggle source
# File lib/shaf/generator/base.rb, line 36
def template_dir
  File.expand_path('templates', __dir__)
end
write_output(file, content) click to toggle source
# File lib/shaf/generator/base.rb, line 59
def write_output(file, content)
  FileTransactions::CreateFileCommand.execute(file) { content }
  puts "Added:      #{file}"
end